none
Assign a variable to a radio button

    Question

  • I'm working on a Visual C++ 2005 MFC dialogue application with some controls in the window. It's only static labels, text boxes, check buttons and now also two radio buttons. Assigning member variables to text and check controls wasn't a problem, but I just can't figure out how to do it for the radio buttons. The tab order is correct, the first of the two radio buttons has the attributes "Group" and "Tabstop" set, the second has none of the attributes set. I could add a variable of type BOOL to the first radio button but cannot add anything for the second one. And whatever I assign to the BOOL variable, the second radio button will be checked when running the programme.

    Is there a documentation of how to use radio buttons in MFC applications? It's not obvious from the Visual Studio UI itself.
    Tuesday, September 12, 2006 3:42 PM

Answers

  • For group of radio buttons you need to assign int variable. Variable is assigned to the first radio button in the group, it's value is equal to 0-based number of selected button in the group.

    Tuesday, September 12, 2006 4:53 PM

All replies

  • For group of radio buttons you need to assign int variable. Variable is assigned to the first radio button in the group, it's value is equal to 0-based number of selected button in the group.

    Tuesday, September 12, 2006 4:53 PM
  •  

    in 2005, the only choice for mapping a value variable to a control is BOOL.  this is definately different than in MFC 1.0, 1.1, ... 6.0 where all of my experience lies.  anyone know what's going on???

     

    Wednesday, February 28, 2007 11:46 PM
  • you can do this with SubclassDlgItem function. where's what you do

    in class exampleDlg.h declare CButton m_radio_button;

    then for the class declared in exampleDlg.h override OnInitDialog() function

    in this function paste following code

    this->m_radiobutton.SubclassDlgItem(IDC_ID_OF_RADIO_BUTTON,this);

    to check if radio button is 1 or 0 you use

    UINT CButton::GetCheck() function

    to set radio you can use

    CButton::SetCheck(UINT) // or click with a mouse.. ;p

    BST_CHECKED, or BST_UNCHECKED are returned by CButton::GetCheck.CButton::SetCheck takes BST_CHECKED or BST_UNCHECKED

    hope it's clear

    Thursday, March 1, 2007 12:04 AM
  • Alternative method:

     

    It seems like MS Visual Studio 2005 has a bug and is not adding the required DDX statement in the DoDataExchange method.

    I have the same probem and here is a workarround:

     

    1) Assign properties to the radio buttons as you did.

    2) Assign an int variable to the radio button with the group property on

    3) Add the missign statement (I think is a Visual Studio assistant bug)

     

    void CMFCEncrypterDlg:Big SmileoDataExchange(CDataExchange* pDX)

    {

    CDialog:Big SmileoDataExchange(pDX);

    DDX_Text(pDX, IDC_EDIT_CLAVE, m_strClave); // Added By VS

    DDX_Radio(pDX, IDC_ENCRIPTAR, m_tipoAccion); // Added Manually

    // 1st control , variable to bind to

    }

     

     

    Hope it helps

    Tuesday, July 31, 2007 7:29 AM
  • NB: In my previous post I pasted a code sniplet without realising it contained an "smiley" between : and D

     

     

    void CMFCEncrypterDlg:: DoDataExchange(CDataExchange* pDX)

    {

    CDialog:: DoDataExchange(pDX);

    DDX_Text(pDX, IDC_EDIT_CLAVE, m_strClave); // Added By VS

    DDX_Radio(pDX, IDC_ENCRIPTAR, m_tipoAccion); // Added Manually

    // 1st control , variable to bind to

    }

     

     

    Cheers

    Tuesday, July 31, 2007 7:39 AM
  • I think the way it handle DDX for Radio button has been changed since VC++ 6.

    Grouping Radio Buttons on a Dialog Box
    Tuesday, August 11, 2009 8:07 PM
  •  

    in 2005, the only choice for mapping a value variable to a control is BOOL.  this is definately different than in MFC 1.0, 1.1, ... 6.0 where all of my experience lies.  anyone know what's going on???

     


    yeah,  it is a bug in vs2005. but you can change BOOL to int manully , since we can edit the droplist of BOOL. it works.   :)
    Monday, January 4, 2010 4:06 AM