OnInitDialog: reset dialog content - c++

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.

Related

MFC: Use a dialog's class for a second dialog

I have an dialog with several buttons and sliders on it and am adding another button that should close the dialog and open another that is essentially a mini version it.
I need access to the buttons in the first dialog in the second mini dialog (since they should have the same functionality).
Is there a way to use the class from the first dialog on the second so that I have access to those buttons? I've tried right clicking the dialog and adding a class, but it makes me make a new one instead of being able to use an existing one.
Thanks in advance!

Populating combo box in MFC dialog box

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.

C++ MFC create new dialog and add Combobox item

I'm using MFC to build an application with two dialogs.
When I press a button in the parent dialog a new window including a Combobox should apprear.
I created a first dialog with a button "New". This button will open the second dialog.
Therefor I created a second dialog with a Combobox. The Combobox has a linked variable variableCombobox. The second class is called CSecond.
Before I do anything in the new dialog I want to add an item to the Combobox.
In the first dialog class I create the new window like this:
void CFirstDlg::OnBnClickedNew()
{
CSecond dlg2 = new CSecond();
dlg2.variableCombobox.AddString(L"test");
dlg2.DoModal();
}
The program crashes in the line I want to add the test string to the Combobox showing an assertion error.
I noticed that the dlg2 object is null but I don't know why.
Can anyone tell me how to create a second window immediately adding an new item in the Combobox of the second window?
The problem is the second dialog is a modal dialog. The windows does not exist before the call to DoModal() and no longer exist after that function returns. Therefore calling AddString on the combobox is not all right, since the combo box does not exist at that time.
The solution is to initialize the dialog with your desired values (like in the constructor for instance, or other methods) and then in OnInitDialog() use those values to setup the controls (including this call to AddString for the combobox).

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 .

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.