Click here to Skip to main content
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 4: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 Sergey Alexandrovich Kryukov 424
1 OriginalGriff 375
2 CHill60 230
3 CPallini 220
4 Maciej Los 210
0 Sascha Lefévre 75
1 OriginalGriff 70
2 F-ES Sitecore 55
3 Peter Leow 50
4 Dave Kreskowiak 40

Advertise | Privacy | Mobile
Web04 | 2.8.150327.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