Click here to Skip to main content
15,881,687 members
Articles / Programming Languages / C++

Radio Buttons in MFC (Visual Studio 2008 / C++)

Rate me:
Please Sign up or sign in to vote.
4.83/5 (11 votes)
25 Aug 2010CPOL1 min read 93K   8   8
A quick and dirty description of how to use radio buttons in MFC

This is a quick and dirty description of how to use radio buttons in MFC, written because I could not find this information in a single place on the web.

In the dialog editor:

  • Create a new group with the group box control and set a meaningful caption.
  • Add radio button controls inside the group.
  • The radio buttons must have ascending tab order.
  • The first radio button (in tab order) must have the property "Group" set to True.
  • All other radio buttons must have the property "Group" set to False.

It should look similar to this:

In the header file defining the dialog class:

  • Add a member variable of type int that will store which radio button is selected (none: -1, first: 0, second: 1, and so on).
    Example:
    C++
    int m_nLEDType;

In the CPP file implementing the dialog class:

  • In the constructor, initialize the member variable (with 0)
    Example:
    C++
    CDialogConfig::CDialogConfig(CMainFrame* pMainFrame) : 
    	CDialog(CDialogConfig::IDD, pMainFrame), m_nLEDType(0)
  • In DoDataExchange, add a call to DDX_Radio similar to the following (where IDC_RADIO_LEDTYPE1 is the ID of the first radio button):
    C++
    DDX_Radio(pDX, IDC_RADIO_LEDTYPE1, m_nLEDType);

When you want to read which radio button is selected:

  • Call UpdateData with a parameter of true (indicates reading):
    C++
    UpdateData (TRUE);
  • After UpdateData has been called, the member variable (m_nLEDType in this example) has the current state of the buttons.

When you want to select one of the buttons programmatically:

  • Set the member variable to the correct value (none selected: -1, first button selected: 0, second button selected: 1, and so on)
  • Call UpdateData with a parameter of false (indicates writing):
    C++
    UpdateData (FALSE);

License

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


Written By
Helge Klein GmbH
Germany Germany
Helge Klein is an independent consultant and developer. As a consultant, he has worked in Windows and Citrix projects for various larger German corporations. As a developer, he architected sepago's user profile management product sepagoPROFILE whose successor is now available as Citrix Profile Management. In 2009 Helge received the Citrix Technology Professional (CTP) Award, in 2011 he was nominated a Microsoft Most Valuable Professional (MVP).

Helge's professional interests are focused on Microsoft server technologies, various Citrix products and programming in several languages. He publishes his knowledge in English in his blog at http://helgeklein.com/blog. Helge can also be found on Twitter as @HelgeKlein. He has presented on many occasions, e.g. Citrix TechEdge Munich 2009, ice Lingen (2009 and 2011), PubForum (2010 and 2011), Microsoft TechDay Online 2010, Citrix Synergy 2011 and 2012.

Helge is the author of SetACL, a powerful tool for managing Windows permissions from the command line or from scripts and programs. SetACL is open source and has been downloaded more than 500,000 times. SetACL's modern cousin SetACL Studio comes with an intuitive graphical user interface and is available for a small fee. Another popular tool, Delprof2, automates the deletion of user profiles.

Helge lives in Cologne, Germany.

Comments and Discussions

 
QuestionVC++ Radio Buttons Pin
VidyasagarSTGM19-Sep-14 0:38
VidyasagarSTGM19-Sep-14 0:38 
GeneralDoes not work with CMfcButton Pin
Michel Wassink29-Jul-14 0:34
Michel Wassink29-Jul-14 0:34 
Questionnice summary Pin
dr.al13-Apr-14 11:26
dr.al13-Apr-14 11:26 
QuestionI recommend avoiding UpdateData at all cost. Pin
Sunny127013-Jan-13 21:23
Sunny127013-Jan-13 21:23 
GeneralMy vote of 4 Pin
pereillo15-Jun-12 6:31
pereillo15-Jun-12 6:31 
It's everything rigth but it's hard to hunderstand for a beginer. I think a little bit more concise explanation at the begining it would be better. Ex. where must I Add a member variable? (At the end I got it) Thanks anyway. It helped me.
QuestionRadio Pin
ITISAG16-May-12 4:29
ITISAG16-May-12 4:29 
AnswerRe: Radio Pin
HelgeKlein16-May-12 4:33
HelgeKlein16-May-12 4:33 
GeneralAlternative PinPopular
David Crow30-Aug-10 16:47
David Crow30-Aug-10 16:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.