How can I select a radio button programmatically in MFC?

Can I use SendDlgItemMessage()?

I see there is a CButton::SetCheck() method - but I don't see how to reference the button object created by the Resource editor. How can I ue SetCheck?
jobremsAsked:
Who is Participating?
 
ZoppoConnect With a Mentor Commented:
Hi jobrems,

the resource editor creates a resource ID for the button which you can use:

(I assume it's in a dialog)

...
CButton* pButton = (CButton*)GetDlgItem( IDC_RADIO1 ); // that's the ID the resource editor creates - you can change it for better readability
pButton->SetCheck( TRUE );
...

Hope that helps,

ZOPPO
0
 
ZoppoCommented:
In addition: This can anly be called within a member function of the dialog and only after the CDialog::OnInitialUpdate function is called ...

BTW, there's another option too: You can generate a CButton member with 'ClassWizard->Variables->Add variable' for that resource ID of type button ...
0
 
ZoppoCommented:
... or for a group of radio buttons you just create (the same way as above with 'AddVariable') an UINT member variable. You then can set this to be 0 for the first radio button, 1 for the second a.s.o. and call 'UpdateData(FALSE)' to set it - call 'UpdateData(TRUE)' later to recieve in that UINT variable which button was checked by the user.

ZOPPO
0
 
jobremsAuthor Commented:
Quick and easy solution - thanks!
0
 
alb66Commented:
Add to ZOPPO's comment.
The member variable must be associated to the first radio button in the group.
0
All Courses

From novice to tech pro — start learning today.