Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

1 solution

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


[Update]
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:
C++
class CAboutDlg : public CDialog
{
  CButton myButton;
//...



C++
BOOL CAboutDlg::OnInitDialog()
{
  CDialog::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).
[/Update]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 8-Apr-13 10:14am    
Of course, a 5.
—SA
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900