Programmatically select an MFC radio button
Question
When I'm initializing a dialog, I'd like to select one of the radio buttons on the form. I don't see a way to associate a Control variable using the Class Wizard, like you would typically do with CButtons, CComboBoxes, etc...
Further, it doesn't like a CRadioButton class even exists.
How can I select one of the several radio buttons?
Solution
Radio buttons and check buttons are just buttons. Use a CButton
control and use GetCheck
/SetCheck
.
OTHER TIPS
Use CWnd::CheckRadioButton to set select one button in a group and CWnd::GetCheckedRadioButton to retrieve the ID of the selected button. Be sure to call these methods on your dialog object, and not any of the radio button objects.
Going on what mos said, the following worked did the trick:
CButton* pButton = (CButton*)GetDlgItem(IDC_RADIOBUTTON);
pButton->SetCheck(true);
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
...
DDX_Radio(pDX, IDC_RADIO1, m_Radio);
...
}
but it is the same thing Wizard generates
You can use this one-liner:
::SendMessage(GetDlgItem(IDC_RADIO1)->m_hWnd, BM_SETCHECK, BST_CHECKED, NULL);