Click here to Skip to main content
13,052,696 members (68,299 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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).
Of course, a 5.
CHill60 8-Apr-13 10:22am
And my 5
gurunbr1 9-Apr-13 2:11am
This code was accepted my compiler, but i couldn't visible button on dialog at runtime?
gurunbr1 9-Apr-13 4:46am
i use dialog forms, not SDI..
CPallini 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 9 Apr 2013
Copyright © CodeProject, 1999-2017
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