4

I have a MFC dialog derived from CMyDialog (is a CDialog) and try to add a control with the "add member variable wizard". But the checkbox "control variable" is grayed out. Any ideas what the problem is?

For example: I have a edit control and want to add a int variable with lower and upper border (min and max value). I need to check "control variable" and set "catagory" to value. But "control variable" is grayed out.

I tried also button or list control, but "control variable" is grayed out.

bzs
  • 499
  • 5
  • 16

4 Answers4

3

In order to add a control variable using the wizard, you must have an IDD enum defined in the class header and use this in the constructor. For example:

Header:

class CMyDialog : public CDialog 
{
public:
  // Dialog Data
  enum { IDD = IDD_MYDIALOG };
  ⋮
};

Source:

CMyDialog::CMyDialog(CWnd* pParent /*=NULL*/)
  : CDialog(CMyDialog::IDD, pParent)
{
    ⋮
}

On the other hand, if you use the resource ID directly in the constructor's initialization list (instead of using the IDD enum in the header), then you can't add a member variable or use the "control variable" checkbox in the IDE.

Source:

CMyDialog::CMyDialog(CWnd* pParent /*=NULL*/)
  : CDialog(IDD_MYDIALOG, pParent)
{
    ⋮
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
bzs
  • 499
  • 5
  • 16
1

Maybe some bug in VS2008. I faced similar issue once.

Try adding a control and double click it. You'll have an handler generated. Then click on the control and add control variable.

This worked for me.

N3Xg3N
  • 97
  • 1
  • 12
0

I got the same problem here, but due to performance reasons we had disabled the Intellisense update ('Disable Database Auto Updates = true'). I suppose the MFC wizzard uses Intellisense. Just re-enable it or rescan your solution and it works.

gast128
  • 1,223
  • 12
  • 22
0

Andreas Belke is right. We were facing the same issue. Also note that if you have a typedef for your dialog class somewhere, you will have the same problem.

The following example does not work (VS2015):

typedef CDialog         CBaseDialog;

class CMyDialog : public CBaseDialog
{
public:
  // Dialog Data
  enum { IDD = IDD_MYDIALOG };
  ⋮
};

CMyDialog::CMyDialog(CWnd* pParent /*=NULL*/)
  : CBaseDialog(IDD_MYDIALOG, pParent)
{
    ⋮
}
Bucket
  • 7,415
  • 9
  • 35
  • 45
Michael
  • 408
  • 3
  • 13