Click here to Skip to main content
11,797,355 members (78,721 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: VC6 MFC
I need to create a button dynamically in mfc dialog form by using following code..

CButton myButton1, myButton2, myButton3, myButton4;

myButton1.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
CRect(10,10,100,30), pParentWnd, 1);

In this code i got an error pParentwnd as not a member.. how can i clear this error?
Posted 8-Apr-13 3:54am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You may just pass this (that is the pointer to the CDialog itself) instead of pParentWnd.

Please note myButton shouldn't be a local variable (it should be a class member).
I used the following code in the about dialog of a MFC SDI application:
class CAboutDlg : public CDialog
  CButton myButton;

BOOL CAboutDlg::OnInitDialog()
  myButton.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
  CRect(10,10,100,30), this, 1);
  return TRUE;  
It is working fine (it shows the button).
Sergey Alexandrovich Kryukov at 8-Apr-13 10:14am
Of course, a 5.
CHill60 at 8-Apr-13 10:22am
And my 5
gurunbr1 at 9-Apr-13 2:11am
This code was accepted my compiler, but i couldn't visible button on dialog at runtime?
gurunbr1 at 9-Apr-13 4:46am
i use dialog forms, not SDI..
CPallini at 9-Apr-13 5:02am
That is irrelevant: I used the code inside a dialog, after all. Make sure your button variable is a member of the (dialog) class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CPallini 475
1 Maciej Los 370
2 OriginalGriff 363
3 Abhinav S 344
4 Andy Lanng 296
0 OriginalGriff 2,092
1 Maciej Los 1,760
2 KrunalRohit 1,436
3 CPallini 1,245
4 ppolymorphe 893

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 9 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100