1,105,585 Community Members

radio button in C#

Member Avatar
judithSampathwa
Posting Pro in Training
453 posts since May 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi i have a 6 radio buttons in an interface. each two radio buttons are separated in a panel. when i load the form one radio button in the interface is selected. how can i make it so that all the radio buttons are deselected when they are loaded.

how can i do this.

thankxxxxxxx

Member Avatar
nick.crane
Veteran Poster
1,195 posts since Feb 2010
Reputation Points: 342 [?]
Q&As Helped to Solve: 190 [?]
Skill Endorsements: 5 [?]
 
0
 

If by selected you mean Checked then just set them all unchecked (i.e. rb.Checked = false; ).
[Edit] Also, make sure the they are not set by the form designer. Set them all false there too.

Member Avatar
judithSampathwa
Posting Pro in Training
453 posts since May 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

If by selected you mean Checked then just set them all unchecked (i.e. rb.Checked = false; ).
[Edit] Also, make sure the they are not set by the form designer. Set them all false there too.

i did this in the form loading but still one radio button is checked,

why is that??


thankxxxxxx

Member Avatar
nick.crane
Veteran Poster
1,195 posts since Feb 2010
Reputation Points: 342 [?]
Q&As Helped to Solve: 190 [?]
Skill Endorsements: 5 [?]
 
0
 

I just added three RBs to my test form and they all remain unchecked when the form opens.
The default Checked value for a RadioButton is false.
It must be being set somewhere in your code.

Member Avatar
saravind84
Newbie Poster
23 posts since Aug 2010
Reputation Points: 2 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
-1
 

When you place radio buttons in a panel, only one can be selected at a time. Also by default one of the radio buttons will be selected in during page load. Try adding the following code in the Panel_Paint Event. This will solve the issue.

private void pnlTest_Paint(object sender, PaintEventArgs e)
{
      rbOne.Checked = false;
      rbTwo.Checked = false;
}
Member Avatar
nick.crane
Veteran Poster
1,195 posts since Feb 2010
Reputation Points: 342 [?]
Q&As Helped to Solve: 190 [?]
Skill Endorsements: 5 [?]
 
0
 

Also by default one of the radio buttons will be selected in during page load.

If so, then this is a new behaviour add from VS2008!
My VS2005 does not do that.

Also, the paint event is not a good place to do what you suggest. It will constantly be executed when the object is painted (Eg after re-size).

If it something that is done by the form during its load event, then
a better solution is to override the forms load event and put the code after the base call.
(Note: This is not the same as attaching to the Load event.)

protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            rbOne.Checked = false;
            rbTwo.Checked = false;
        }

However, I do not think that this is the problem.
I think the RB Checked state is being set somewhere else by OPs code.

@judithSampathwa: If you are still having problems with this, then I recommend that you comment out all your code that sets the RBs.
If the problem goes away (i.e. RBs all off), then add them back in one at a time to find out which one is giving you the problem.

Member Avatar
Ryshad
Nearly a Posting Virtuoso
1,263 posts since Aug 2009
Reputation Points: 480 [?]
Q&As Helped to Solve: 247 [?]
Skill Endorsements: 14 [?]
 
0
 

I'm with nick on this one, if radiobuttons in panels automatically have one checked then this is new behaviour for 2010 because vs2005 and vs2008 have not exhibited it.
Likewise, the Paint event is a bad place to put that code, it will clear the users selection every time the control repaints itself. Not a good practice; i know i'd be annoyed, as a user, if my form selections were cleared every time i minimised or covered the program.

judithSampathwa , are you creating a WPF, Winform or ASP.net project?

Member Avatar
judithSampathwa
Posting Pro in Training
453 posts since May 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

When you place radio buttons in a panel, only one can be selected at a time. Also by default one of the radio buttons will be selected in during page load. Try adding the following code in the Panel_Paint Event. This will solve the issue.

private void pnlTest_Paint(object sender, PaintEventArgs e)
{
      rbOne.Checked = false;
      rbTwo.Checked = false;
}

hey i didn't notice that putting a panel would make it checked everytime. so that was the problem............

Member Avatar
ddanbe
Posting Expert
5,355 posts since Oct 2008
Reputation Points: 2,066 [?]
Q&As Helped to Solve: 908 [?]
Skill Endorsements: 46 [?]
Featured
 
1
 

I'm with nick on this one, if radiobuttons in panels automatically have one checked then this is new behaviour for 2010 because vs2005 and vs2008 have not exhibited it.

Have to contradict you Ryshad. Because in a strange way I can use VS 2005,2008 and 2010 for the moment:icon_lol:
The behaviour of a radiobutton is the same on all 3. That is logic, because that is exactly what a radiobutton is supposed to do in a user interface.(I hate the word GUI)
Read for example this article,if you want to learn more. I think the OP is busy with a somewhat wrong design.

Member Avatar
judithSampathwa
Posting Pro in Training
453 posts since May 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Have to contradict you Ryshad. Because in a strange way I can use VS 2005,2008 and 2010 for the moment:icon_lol:
The behaviour of a radiobutton is the same on all 3. That is logic, because that is exactly what a radiobutton is supposed to do in a user interface.(I hate the word GUI)
Read for example this article,if you want to learn more. I think the OP is busy with a somewhat wrong design.

hey i am not contradicting any one sorry.
nick.crane- i got it working

thnakx everyone

Member Avatar
Ryshad
Nearly a Posting Virtuoso
1,263 posts since Aug 2009
Reputation Points: 480 [?]
Q&As Helped to Solve: 247 [?]
Skill Endorsements: 14 [?]
 
0
 

Have to contradict you Ryshad. Because in a strange way I can use VS 2005,2008 and 2010 for the moment:icon_lol:
The behaviour of a radiobutton is the same on all 3. That is logic, because that is exactly what a radiobutton is supposed to do in a user interface.(I hate the word GUI)
Read for example this article,if you want to learn more. I think the OP is busy with a somewhat wrong design.

Really?? I havent used 2005 in a while, but i dont recall this behaviour. I tested the behaviour in 2008 and it didnt match what the OP described. I added 2 radio buttons to form, 2 radio buttons to a panel and 2 radio buttons to a groupbox. When i ran it, none of the radio buttons were checked when the program started. Is there perhaps a setting somewhere in VS that determines default behaviour of the radiobuttons?

Member Avatar
judithSampathwa
Posting Pro in Training
453 posts since May 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

When you place radio buttons in a panel, only one can be selected at a time. Also by default one of the radio buttons will be selected in during page load. Try adding the following code in the Panel_Paint Event. This will solve the issue.

private void pnlTest_Paint(object sender, PaintEventArgs e)
{
      rbOne.Checked = false;
      rbTwo.Checked = false;
}

hey this isn't a good solution i guess,
i realsed it when i try to code on it.

because it i try to click on the radio button the paint method runs everytime. so this is not a good solution,

is there any other good solution for this
thanxxxxx

Member Avatar
nick.crane
Veteran Poster
1,195 posts since Feb 2010
Reputation Points: 342 [?]
Q&As Helped to Solve: 190 [?]
Skill Endorsements: 5 [?]
 
0
 

Read this post.

Member Avatar
judithSampathwa
Posting Pro in Training
453 posts since May 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Read this post.

where id on the onLoad method is it in the form or the radio button or is it the form load event
thnx

Member Avatar
nick.crane
Veteran Poster
1,195 posts since Feb 2010
Reputation Points: 342 [?]
Q&As Helped to Solve: 190 [?]
Skill Endorsements: 5 [?]
 
0
 

The OnLoad override is just a more direct way to run code that normally runs in Form_Load.
This method is called before the Form_Load event (which is executed by base.OnLoad(e);)
To add the override to a form, start typing "override" then when you press the spacebar you should get a list of possible overrides; Scroll to select OnLoad.

However, if you have already tried addingrb.Checked = false; to the Form_Load then doing it in OnLoad is no different.

I suggest you try this bit first.

I recommend that you comment out all your code that sets the RBs.
If the problem goes away (i.e. RBs all off), then add them back in one at a time to find out which one is giving you the problem.

If you still have problems post again and I'll try to think of another solution.

Member Avatar
ddanbe
Posting Expert
5,355 posts since Oct 2008
Reputation Points: 2,066 [?]
Q&As Helped to Solve: 908 [?]
Skill Endorsements: 46 [?]
Featured
 
0
 

@Ryshad
I did my tests in a form, I now repeated them 3 times in a GroupBox(wich is I think the preferred way) and the first radiobutton was always selected(checked).
I would very much like to know what is the difference between your settings and mine. :)

Member Avatar
nick.crane
Veteran Poster
1,195 posts since Feb 2010
Reputation Points: 342 [?]
Q&As Helped to Solve: 190 [?]
Skill Endorsements: 5 [?]
 
0
 

@ddanbe: Try trapping the CheckedChanged event to find out when/where it is being set.
[Edit] I just had a thought. Is this an OS related issue? What OS are you using?

Member Avatar
ddanbe
Posting Expert
5,355 posts since Oct 2008
Reputation Points: 2,066 [?]
Q&As Helped to Solve: 908 [?]
Skill Endorsements: 46 [?]
Featured
 
0
 

@nick.crane
Just to make matters worse...
I have VS2005 and WindowsXP at work, VS2008 and Vista and just new VS2010 and Windows 7 at home :|

Member Avatar
nick.crane
Veteran Poster
1,195 posts since Feb 2010
Reputation Points: 342 [?]
Q&As Helped to Solve: 190 [?]
Skill Endorsements: 5 [?]
 
0
 

I am using VS2005 on XP. My RBs all stay false!
Is the first RB on in the designer?

Member Avatar
judithSampathwa
Posting Pro in Training
453 posts since May 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

The OnLoad override is just a more direct way to run code that normally runs in Form_Load.
This method is called before the Form_Load event (which is executed by base.OnLoad(e);)[/NoParse] To add the override to a form, start typing "override" then when you press the spacebar you should get a list of possible overrides; Scroll to select OnLoad.

However, if you have already tried adding [icode] rb.Checked = false; [/icode] to the Form_Load then doing it in OnLoad is no different.

I suggest you try this bit first.

If you still have problems post again and I'll try to think of another solution.[/QUOTE]

hey yeah i did ths also i won't work, the radio button keeps on checked, ddanbe: i agree with you, it is checked eveytime the form loads

why is this happening, i am using microsoft windows xp service pack 3

why is this???

thanxxxxxx[NoParse];)[/NoParse]
To add the override to a form, start typing "override" then when you press the spacebar you should get a list of possible overrides; Scroll to select OnLoad.

However, if you have already tried adding rb.Checked = false; to the Form_Load then doing it in OnLoad is no different.

I suggest you try this bit first.


If you still have problems post again and I'll try to think of another solution.

hey yeah i did ths also
i won't work, the radio button keeps on checked,
ddanbe: i agree with you, it is checked eveytime the form loads

why is this happening, i am using microsoft windows xp service pack 3

why is this???

thanxxxxxx

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: