Link to home
Start Free TrialLog in
Avatar of dcoggin1
dcoggin1Flag for United States of America

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
ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dcoggin1

ASKER

Yes, I should have said I'm getting an assertion message.  I don't understand your response.  Are you saying that I will always get the assertion message if I selectively enable and disable radio buttons in a group?  If I only disable one button with EnableWindow(FALSE) or hide one button with ShowWindow(SW_HIDE), I will get the assertion message.  I have tried it with the disabled/hidden button being the last in the group and I still get an assertion message.  If I remove the calls, it works fine.  Thanks for your help.

David
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
I'd say, you are right. I also remember something like that.
You are welcome