dcoggin1
asked on
Problem with hidden radio button causing crash
I have an MFC application with a dialog that uses radio buttons. In the OnInitDialog for the dialog with the radio buttons I programmatically choose to either EnableWindow(FALSE) or ShowWindow (SW_HIDE) for two of the four radio buttons in a group if those buttons aren't appropriate for the current state. Either of these options causes the application to crash when UpdateData(TRUE) is called in the overridden OnOK function. The crash occurs at the ASSERT shown below in MFC dlgdata.cpp. Is there a way to make this work? If I leave all radio buttons enabled, it all works fine.
// walk all children in group
int iButton = 0;
do
{
if (::SendMessage(hWndCtrl, WM_GETDLGCODE, 0, 0L) & DLGC_RADIOBUTTON)
{
// control in group is a radio button
if (pDX->m_bSaveAndValidate)
{
if (::SendMessage(hWndCtrl, BM_GETCHECK, 0, 0L) != 0)
{
ASSERT(value == -1); // only set once
value = iButton;
}
}
else
{
// select button
::SendMessage(hWndCtrl, BM_SETCHECK, (iButton == value), 0L);
}
iButton++;
}
else
{
TRACE(traceAppMsg, 0, "Warning: skipping non-radio button in group.\n");
}
hWndCtrl = ::GetWindow(hWndCtrl, GW_HWNDNEXT);
} while (hWndCtrl != NULL &&
!(GetWindowLong(hWndCtrl, GWL_STYLE) & WS_GROUP));
Thanks for your help.
David
// walk all children in group
int iButton = 0;
do
{
if (::SendMessage(hWndCtrl, WM_GETDLGCODE, 0, 0L) & DLGC_RADIOBUTTON)
{
// control in group is a radio button
if (pDX->m_bSaveAndValidate)
{
if (::SendMessage(hWndCtrl, BM_GETCHECK, 0, 0L) != 0)
{
ASSERT(value == -1); // only set once
value = iButton;
}
}
else
{
// select button
::SendMessage(hWndCtrl, BM_SETCHECK, (iButton == value), 0L);
}
iButton++;
}
else
{
TRACE(traceAppMsg, 0, "Warning: skipping non-radio button in group.\n");
}
hWndCtrl = ::GetWindow(hWndCtrl, GW_HWNDNEXT);
} while (hWndCtrl != NULL &&
!(GetWindowLong(hWndCtrl, GWL_STYLE) & WS_GROUP));
Thanks for your help.
David
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well, I believe I figured out the problem. I thought that the MFC framework called UpdateData(FALSE) upon return from my OnInitDialog, but apparently it calls it during the CDialog::OnInitDialog call at the first of my OnInitDialog. The radio button being hidden or disabled was selected at the time it was hidden or disabled. Even though I could still select another enabled button normally, the selected stated of the hidden/disabled button was apparently kept. When I added an UpdateData(FALSE) call at the end of the OnInitDialog function, it all works as expected with the hidden and disabled buttons. Thanks again for your help.
David
David
I'd say, you are right. I also remember something like that.
You are welcome
You are welcome
ASKER
David