dialogs and DDX_Radio()
I need help getting from a radio button to a cstring. I need to find out the sex of the user but am not sure how to use the DDX_Radio function to find out which button is checked. Here is what I have so far but it crashes at run time when I try to open the dialogbox.
In case it isnt clear I have a compeitior object which contains a CString which needs to contain the competitors gender.
void NewCompetitorDlg::DoDataExchange( CDataExchange* pDX )
CDialog :: DoDataExchange( pDX );
DDX_Text( pDX, IDC_EDIT_FIRST, newCompetitor->m_firstname );
DDV_MaxChars( pDX, newCompetitor->m_firstname, 20 );
DDX_Text( pDX, IDC_EDIT_LAST, newCompetitor->m_lastname );
DDV_MaxChars( pDX, newCompetitor->m_lastname, 40 );
DDX_Text( pDX, IDC_EDIT_SCHOOL, newCompetitor->m_school );
DDV_MaxChars( pDX, newCompetitor->m_school, 40 );
DDX_Radio( pDX, IDC_MALE, newCompetitor->m_sex );
DDX_Radio( pDX, IDC_FEMALE, newCompetitor->m_sex );
Add the radio button to the class member variable. Call GetCheck() to determine the status of the button.
ok I've sorted that now, I used the aptly named function IsDlgButtonChecked(...)
The problem now is that I need to ensure that at least 1 radio button is checked. The best way for me to do this would be to displaya messagebox when the user hits ok, but I dont know where to put the code for that. That way a number of different things could be checked, like other buttons and stuff. This is the code that calls the dialog box:
So where do I put the messageboxes so that when ok is clicked the dialog does not actually dissapear but tells the user to complete the dialog?
bool TkdWin::addCompetitor( )
NewCompetitorDlg dlg( empty );
if( IDOK == dlg.DoModal( ) )
//add a new record!
MessageBox( empty.m_sex );
One solution is an event handler. Add an event handler for the radio buttons. When there is a change, traverse through them and determine their status. If none of the radio buttons is checked, then prompt the user.
Isn't there a simpler way? All I want is to check that 1 of 2 bools has been set to true, and other similar things with other buttons. I was hoping I could just check the variables and prompt the user to finish off the input before the dialog closes?
Btw, I've made this dialog with the visual editor so what is the easiest way to access the different components, like radio buttons?