1

I have 2 radio button for gender. one male (named radMale) one female (name radFemale). lets say a user clicks the "male" button. how do i display what he chose on a MessageBox?

MessageBox.Show(radMale = "Male");

I know this doesn't work, does anyone know which code will?

default
  • 11,485
  • 9
  • 66
  • 102
user1722515
  • 11
  • 1
  • 2
  • 1
    for winforms you have the [RadioButton.CheckedChanged](http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.checkedchanged.aspx) event that you can listen to. But it is as Daniel says, it depends on the technology you use – default Oct 05 '12 at 09:16
  • its simple, on radMale.click event show messagebox with "Male", and radFemale.click event show messagebox with "Female"... – Sathish Raja Oct 05 '12 at 09:17
  • Its very basic question. You should read a book first or search for tutorial [Heres one](http://www.functionx.com/vcsharp2003/applications/calc.htm) – Renatas M. Oct 05 '12 at 09:17
  • Take a look at this http://stackoverflow.com/questions/1797907/which-radio-button-in-the-group-is-checked – alfoks Oct 05 '12 at 09:23

2 Answers2

3

I tried executing the following code which might solve your problem.

public partial class Form1: Form
{
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
            radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
        }

        void radioButton_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = (RadioButton)sender;
            if(rb.Checked)
                MessageBox.Show(rb.Text); //Shows whatever Text your radiobutton has
        }

        void Form1_Load(object sender, EventArgs e)
        {

        }
}
mihirj
  • 1,199
  • 9
  • 15
1

in WinForms, use this:

MessageBox.Show(radMale.Checked?"Male":"Female");
george.zakaryan
  • 960
  • 1
  • 6
  • 18