Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: VS2008 MFC VC++
I am creating MFC Dialog based application.
I have three radio buttons in a dialog using VS2008. I have tried the following code and it always returns 0 even though the first radio button is checked while I Have set Group(WS_Group) property true for all buttons. What am I doing wrong?
int CtrlID = GetCheckedRadioButton(IDC_RADIO_1, IDC_RADIO_3);
Posted 15-Jan-13 20:43pm
Edited 24-Jan-13 22:23pm
SoMad at 25-Jan-13 4:21am
As I recall, you should only set the Group property to true on the first button.
Soren Madsen
Jochen Arndt at 25-Jan-13 4:23am
Is IDC_RADIO_1 smaller than IDC_RADIO_3? If not, the call will always return 0. Are the IDs in consecutive order? This is not really necessary but recommended.
Member 7909353 at 25-Jan-13 8:11am
H.Brydon at 25-Jan-13 22:31pm
Not correct - the IDs do not need to be in consecutive order (although this is a good idea for maintenance purposes).
The controls need to be in consecutive tab order to work correctly.
Jochen Arndt at 26-Jan-13 4:19am
What is not correct?
GetCheckedRadioButtons() uses a for loop. So the first ID must be the smallest one and the last the largest one. The tab order does not care for the function.
H.Brydon at 26-Jan-13 14:07pm
[wait wait read all of it before replying]
Here are some IDs that are a mix of contiguous and non-contiguous (in resource.h or equiv):
#define IDC_RADIO_1 42
#define IDC_RADIO_22 74
#define IDC_RADIO_12 55
#define IDC_RADIO_23 56
Here are controls that are not in consecutive tab order (in app.rc or equiv):
CONTROL "ABC2",IDC_xx2,"Button",BS_AUTORADIOBUTTON,21,340,110,10
CONTROL "ABC3",IDC_xxx3,"Button",BS_AUTORADIOBUTTON,21,355,110,10
CONTROL "ABC4",IDC_xxx4,"Button",BS_AUTORADIOBUTTON,21,368,110,10
CONTROL "ABC5",IDC_xxx5,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,21,382,110,10
Controls ABC1/2/3 are in contiguous tab order. ABC4/5 are not (with 1/2/3). The ID values of IDC_xxx1/2/3/4/5 do not matter. The tab order does matter.
[update] Jochen Arndt I are both right, on 2 points. Jochen is correct that the IDs that need to be contiguous. My example above with IDC_RADIO_1 through IDC_RADIO_23 is how NOT to do it. If you use GetCheckedRadioButtons() the IDs need to be contiguous. To be properly recognized as a group, the controls need to be in contiguous tab order (as with ABC1/2/3 in my resource file example).
See for more details on the ID problem.
Member 7909353 at 25-Jan-13 4:59am
Yes,Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 554
1 Gihan Liyanage 338
2 RyanDev 268
3 Sergey Alexandrovich Kryukov 263
4 ChauhanAjay 220
0 Sergey Alexandrovich Kryukov 8,936
1 OriginalGriff 7,891
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928

Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100