Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Radio Button Help

Status
Not open for further replies.

abp

Programmer
Sep 14, 2000
134
FR
Hi

I am creating a dialg box which uses Radio Button controls. I have no previos experience in Radio Buttons. What I want to know is ...

1. How to preselect a radio button when first displaying the dialog ?

2. How to find(in code) that a certain radio button is checked ?

3. How to gray out(inactivate) other controls based on the value of a radio button ?

Thanks to all!

Anand ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
On the assumption you are using MFC (always good to say what tools you are using!) you can do the following.

Assume a Dialogbox with radio1, radio2 and Button1. You must ensure that the radio button properties are correctly set. Within their group the first control should have group and tab and auto checked, others should have tab and auto. Also ensure the tab order is correct. Then add member variables, say m_radio1 and m_button1 using Class Wizard. Note there is only one (integer) variable for the radio buttons in each group.

You can set initial values in the AFX_DATA_INIT section

//{{AFX_DATA_INIT(CMfcDlg)
m_radio1 = 0;
//}}AFX_DATA_INIT

The value is the index of the control in the group, starting at zero.

Or alternatively add init code to OnInitDialog() before CDialog::OnInitDialog();

m_radio1 = 0; // Select first radio button

Then doubleclick on radio1 and radio2 and add code to the event routines:

void CMfcDlg::OnRadio1()
{
setRadio1();
}

void CMfcDlg::OnRadio2()
{
setRadio1();
}

void CMfcDlg::setRadio1()
{
UpdateData(TRUE);
if (m_radio1 == 0)
m_button1.EnableWindow(TRUE);
else
m_button1.EnableWindow(FALSE);
}

This gets the current value of radio1 and sets button1 enabled or disabled corresponding to the settings of radio1/radio2.
 
Hi DavesTips,

Sorry .. but I am using MFC. I tried with what you said, but I am not getting the effect I want. I have 3 radio buttons in a row in a group one of which I want to be enabled when the dialog starts. If I want an edit box to contain a value upon start I do this:

// my edit box id is IDC_EDIT1 and my string associated
// with it is m_strEdit1

in the DoDataExchange() method I do this;

void CV5SFrontEndDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);

DDX_Text(pDX, IDC_EDIT1, m_strEdit1);
}

// In the constructor I do this

CV5SFrontEndDlg::CV5SFrontEndDlg(CWnd* pParent /*=NULL*/)
: CDialog(CV5SFrontEndDlg::IDD, pParent)
{

m_strEdit1 = "Choose";
}

I assume that I have to do something similar for the radio button, but when I do the similar one, the text next to the radio button is getting changed with the value I input, not the status(clicked/not clicked) of the button.

What am I doing wrong ?

Thanks

Anand
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Hmm, I hate to say this, but if you've really done as I said it would work! Using the Class Wizard makes things very easy, especially when one is learning. If you do a DDX_Text to a radio button it changes the text, not the value (sort of logical really).

The approriate entry in DoDataExchange() is

DDX_Radio(pDX, IDC_RADIO1, m_radio1);

where m_radio1 is an int as I already described taking the base 0 index of the checked control in the group. You only need the one entry for the first (group option set) button in the group.

You can init this in the constructor (Class Wizard puts its AFX_DATA_INIT section here), or in OnInitDialog(). Setting the value to -1 makes all controls unchecked BTW.

For this to work properly you have to ensure that the radio button attributes are correctly set as I've already described.
 
DavesTips,

What I wanted was DDX_Radio(...) instead of DDX_Text(...) I was using. Thanks a lot for the help :)

Anand ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top