Populating combo box in MFC dialog box - c++

I've added a combo box to my MFC dialog box. And I've added both a control variable (m_wndImportMode) and a value variable (m_nImportMode).
I'm able to use both variables just fine. And I can use the control variable to populate the control just fine. My problem is where is the correct location to populate my control?
If I populate the combo box before DoDataExchange() then my control variable has not yet been initialized. If I populate the combo box after DoDataExchange(), then it populates fine but the value does not get set.
BOOL COptionsDlg::OnInitDialog()
{
// If I populate my combo box here,
// my control variable is not yet available
// This will ultimately call DoDataExchange()
CDialog::OnInitDialog();
// If I populate my combo box here,
// DoDataExchange() has already been called and
// so it will not have selected the correct item
// before there were any items
return TRUE; // return TRUE unless you set the focus to a control
}
Playing with this a little more, I can see I can populate the control before calling CDialog::OnInitDialog() if I use GetDlgItem() instead of my control variable (which has not yet been initialized), and then the default item will correctly be set as I want. But doesn't the MFC framework provide for populating list controls in a dialog box and still using DoDataExchange()?

I resolved this by using GetDlgItem() to retrieve the combo box, and then populating it before CDialog::OnInitDialog() is called. This works as intended.
I'm not sure what I might be doing differently if this is not an issue for anyone else.

Related

MFC: How to set the focus of CEdit boxes?

I'm working on my first simple MFC project, but I'm struggling with one problem: want to set the focus of all CEdit boxes in one of the dialogs. My idea is when open the dialog, the focus to be on the first edit box and then to swap between them with 'tab'.
I saw the method SetFocus(), but I couldn't apply it correctly. Also I couldn't find a solution for implementing the order of the focus with a specific key.
Thanks in advance to everybody who takes time to help me!
You can set the focus to a given control when your dialog box is first shown by calling SetFocus in your OnInitDialog() function. However, if you do so, your OnInitDialog() must return FALSE:
BOOL MyDialog::OnInitDialog() {
CDialog::OnInitDialog(); // Call base class member
GetDlgItem(IDC_MYEDIT)->SetFocus();
//..
return FALSE; // Otherwise, the framework will reset the focus to its default
}
From the M/S documentation:
Return Value
Specifies whether the application has set the input focus to one of
the controls in the dialog box. If OnInitDialog returns nonzero,
Windows sets the input focus to the default location, the first
control in the dialog box. The application can return 0 only if it has
explicitly set the input focus to one of the controls in the dialog
box.

OnInitDialog: reset dialog content

I have a PropertySheet containing 6 PropertyPages. During OnInitDialog of each page, a setting structure assigns initial values to the dialog controls.
However at some point, I want to change my setting structure and each dialog must show newly assigned values for each control it has. Like resseting every dialog.
To achive this, I tried removing and adding all 6 pages but it didn't work. How can I reset every dialog control without manually setting new values to controls.
If I can call OnInitDialog function it would work. However I cannot call OnInitDialog twice.

How do I connect a slider to a variable in MFC

What's the right way to connect a slider to a member variable in MFC? What I've done so far:
Dragged a slider in to my dialog in the resource view and given it an ID
Right clicked on the slider and chosen Add Variable
Set the variable to Control variable and the Category to Value and also set the Min value and Max value to desired values.
This creates a member variable of type int in my dialog class and also adds some stuff to the DoDataExchange method.
However, the DoDataExchange method is only called once when my dialog is created, what do I have to do to make my member variable update when I drag the slider? Should I have set the Category to Ctrl instead and manually managed the range, and intercepted the WM_HSCROLL messages?
(This is a non modal dialog by the way)
Call UpdateData to do the DataExchange passing TRUE to save and validate the data to your variable and FALSE to set the control value based on your variable, ie. passing data into the control. More information about UpdateData:
http://msdn.microsoft.com/en-us/library/t9fb9hww(v=vs.80).aspx
and for general data exchange mechanism:
http://msdn.microsoft.com/en-us/library/xwz5tb1x(v=vs.80).aspx
Yes, you call UpdateData() from within the handler for the control's WM_HSCROLL (or WM_VSCROLL) if it's a vertical slider). See http://msdn.microsoft.com/en-us/library/ekx9yz55%28v=vs.80%29.aspx .

Changing UI state depending on a checkbox value

I have the following situation.
There is a dialog with a check-box and a text-box. Check-box's click is bound to a function that toggles if the text-box is enabled or grayed out. It works fine but I also need to preset some values to the dialog before creating and displaying it. If the variable that is connected to the check-box is set to ture I want to disable the text-box.
I tried to accomplish this in different ways, but it all boils down to the fact that I can not change the GUI of the dialog before calling DoModal (I get assertion falure when I try).
This is probably a common problem, but I could bot find a solution online. Am I completely off track?
MyDialog d(this);
d.bFlag = TRUE; // Because it is true, I want the text-box to be disabled
// I could call a function of d here that would set the state of the text-box correctly,
// but an assertion falure would happen.
if (d.DoModal() == IDOK){
...
}
You need to override OnInitDialog function in your dialog class MyDialog and have the code to check the check box value and enable/disable the text box.

CMFCPropertyGridCtrl last item not drawn?

I have a CMFCPropertyGridCtrl that I'm using in an options dialog box. I have a method in my options dialog class called InitPropertyGrid(). This method clears any properties and populates the CMFCPropertyGrid objects (using a custom Settings object for the property values) and appends them to the grid.
When I open my dialog box the first time all the properties show correctly. However, if I then close my dialog box and reopen it, the very last property is not drawn on the screen. All other properties are drawn normally:
First time:
All subsequent times:
As you can see, the plus/minus icon is showing minus in both cases to indicated the section is expanded. When the last item is not showing, clicking on the +/- icon once to contract and once to expand causes the last item to be correctly shown.
Note when I close the dialog box, I do not destroy it but just reshow it. However immediately before calling ShowWindow on the dialog I call the InitPropertyGrid() (called by UpdateToCurrentSettings) method.
if(optionsDialog_ == NULL)
{
optionsDialog_ = new OptionsDialog(settings_, this);
optionsDialog_->Create(OptionsDialog::IDD, this);
}
optionsDialog_->UpdateToCurrentSettings();
optionsDialog_->ShowWindow(SW_SHOW);
I found I can eliminate this problem simply by calling myPropertyGrid.ExpandAll(TRUE) at the end of the code where I initialize the property grid (InitPropertyGrid() for me). This seems to force all the properties to expand.