Radio Buttons Question

photo123

Member
Joined
Feb 11, 2020
Messages
18
Programming Experience
Beginner
I have a form with a group box that contains 3 radio buttons. When I run the form none of them are selected. I want the first radio button to always be selected when the form is run, but I'm not sure which property to change for that. I tried setting the Checked property to True but that didn't work. Does anyone know any other property I can change to always have it selected as the default? Thanks!
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,280
Location
Sydney, Australia
Programming Experience
10+
Setting the Checked property to True is indeed the way to do it. You should do so in the designer. If you're not seeing that control checked then either you're doing something wrong or something on your system is broken. Maybe you're handling an event that gets raised on creation of the form and clearing the selection. As you shouldALWAYS do in such situations, I suggest creating a new test project and isolating this specific functionality to see whether it behaves as expected. If it does then it's something specific to your other project.

Note that focusing a RadioButton checks it and the first control in the Tab order is focused on load, so you may want to make sure that your RadioButton is not first in the Tab order in a test project. Otherwise, you might get a false positive.
 

photo123

Member
Joined
Feb 11, 2020
Messages
18
Programming Experience
Beginner
Setting the Checked property to True is indeed the way to do it. You should do so in the designer. If you're not seeing that control checked then either you're doing something wrong or something on your system is broken. Maybe you're handling an event that gets raised on creation of the form and clearing the selection. As you shouldALWAYS do in such situations, I suggest creating a new test project and isolating this specific functionality to see whether it behaves as expected. If it does then it's something specific to your other project.

Note that focusing a RadioButton checks it and the first control in the Tab order is focused on load, so you may want to make sure that your RadioButton is not first in the Tab order in a test project. Otherwise, you might get a false positive.
So do the tab indexes inside a group box start from 1? Like what ever is in the group has tab index 1,2,3 etc and what ever is outside the group box also starts from 1,2,3..etc? I checked the tab index for the radio button I want selected when I run the form and its 0. I tried 1 but that still doesn't work.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,280
Location
Sydney, Australia
Programming Experience
10+
Here's what I did to test:
  1. Create a new WinForms project.
  2. Add a TextBox to the form.
  3. Add a GroupBox to the form.
  4. Add three RadioButtons to the form.
  5. Set the Checked property of the second RadioButton to True.
  6. Run the project.
On step 5, the second RadioButton is visibly checked in the designer. On step 6, the second RadioButton is visibly checked in the running app.
 

photo123

Member
Joined
Feb 11, 2020
Messages
18
Programming Experience
Beginner
Here's what I did to test:
  1. Create a new WinForms project.
  2. Add a TextBox to the form.
  3. Add a GroupBox to the form.
  4. Add three RadioButtons to the form.
  5. Set the Checked property of the second RadioButton to True.
  6. Run the project.
On step 5, the second RadioButton is visibly checked in the designer. On step 6, the second RadioButton is visibly checked in the running app.
Thanks! I'll give this a try!
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,280
Location
Sydney, Australia
Programming Experience
10+
So do the tab indexes inside a group box start from 1? Like what ever is in the group has tab index 1,2,3 etc and what ever is outside the group box also starts from 1,2,3..etc? I checked the tab index for the radio button I want selected when I run the form and its 0. I tried 1 but that still doesn't work.
You can set Tab indexes to whatever you want, as long as it is not negative. Generally speaking, Tab indexes should start at zero and increment sequentially within a container, although you can have multiple controls in the same container with the same value and you can have non-sequential values. Controls in a container use the Tab index of that container first, then there own Tab index. For instance, if you have a TextBox with Tab index 0, a GroupBox with Tab index 1 and a Button with Tab index 2, then three RadioButtons with Tab indexes 0, 1 and 2 inside the GroupBox, the overall Tab order will be the TextBox first, then the RadioButtons in order, then the Button.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,280
Location
Sydney, Australia
Programming Experience
10+
The simplest way to see and set the Tab order is to select the form in the designer and then select View -> Tab Order from the main menu. You can then see parents and siblings clearly and you can simply click the controls in the order you want, then hit Esacpe when you're done. You can set the TabIndex property in the Properties window if you like, but that can make it harder to keep track of every control.
 

photo123

Member
Joined
Feb 11, 2020
Messages
18
Programming Experience
Beginner
You can set Tab indexes to whatever you want, as long as it is not negative. Generally speaking, Tab indexes should start at zero and increment sequentially within a container, although you can have multiple controls in the same container with the same value and you can have non-sequential values. Controls in a container use the Tab index of that container first, then there own Tab index. For instance, if you have a TextBox with Tab index 0, a GroupBox with Tab index 1 and a Button with Tab index 2, then three RadioButtons with Tab indexes 0, 1 and 2 inside the GroupBox, the overall Tab order will be the TextBox first, then the RadioButtons in order, then the Button.
I tried out your steps and it worked, the 2nd radio button was selected, not sure why it's not working in my other form
 

Sheepings

Retired Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,828
Location
UK
Programming Experience
10+
I tried setting the Checked property to True but that didn't work.
Define didn't work? Show what you actually did by posting your code here.

This works fine for me :
C#:
        private void Form1_Load(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            MessageBox.Show("Check state changed", "RadioCheck Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Screeny here : Screenshot
 

photo123

Member
Joined
Feb 11, 2020
Messages
18
Programming Experience
Beginner
Define didn't work? Show what you actually did by posting your code here.

This works fine for me :
C#:
        private void Form1_Load(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            MessageBox.Show("Check state changed", "RadioCheck Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Screeny here : Screenshot
I realized I was doing everything correct but my event handler said radioButton2 the whole time and I missed that. Everything else was similar to yours but I kept overlooking the name. I'll try to make sure I name them specific names next time so I don't make the same mistake again! Thanks for your help!
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,280
Location
Sydney, Australia
Programming Experience
10+
I realized I was doing everything correct but my event handler said radioButton2 the whole time and I missed that. Everything else was similar to yours but I kept overlooking the name. I'll try to make sure I name them specific names next time so I don't make the same mistake again! Thanks for your help!
This is a fine example of why you should not accept those default names. Such names are all but meaningless. If you use descriptive names for everything then it is far less likely that you will confuse two variables. I might use default names in quick and dirty demos or examples, where the controls don't actually represent anything particular, but not otherwise.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
2,152
Location
Chesapeake, VA
Programming Experience
10+
LOL! And how appropriate this thread was opened today:
 
Top Bottom