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

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.

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

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 for saravind84

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;
}

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.

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?

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............

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.

commented: Great! Cave ab homine unius libri. but I can't understand.. +10

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

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?

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

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

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.

@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. :)

@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?

@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 :|

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

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

Try this

private void Form1_Load(object sender, EventArgs e)
        {
            this.BeginInvoke(new ClearRBDelegate(ClearRBs));
        }

        delegate void ClearRBDelegate();

        private void ClearRBs()
        {
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            radioButton3.Checked = false;
        }

@ddanbe, i'm running VS 2008 on Vista 64bit. I'd love to know whats causing this too, seems odd that the controls should behave differently without any obvious setting changes : /

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

is all your radio buttons inside a panel

thnaxxx

Try this

private void Form1_Load(object sender, EventArgs e)
        {
            this.BeginInvoke(new ClearRBDelegate(ClearRBs));
        }

        delegate void ClearRBDelegate();

        private void ClearRBs()
        {
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            radioButton3.Checked = false;
        }

hey nick.crane
can you explain the code
it works
but not sure how it will behaviour when i add code

thanxx

commented: always find out WHY something works...well done :) +2

hey nick.crane
can you explain the code
it works
but not sure how it will behaviour when i add code

thanxx

Control.Invoke and Control.BeginInvoke execute methods on the controls thread.
The method to run must be passed as a delegate (i.e. function pointer) which is why we need the ClearRBDelegate.
BeginInvoke executes asynchronously; which means that it pushes the execution of the delegate method on to a new thread to run later and continues.
The result is that the ClearRBs method is executed after the form has finished all of its loading.

Just had another thought.
Try running the ClearRBs in Form_Shown instead (without the BeginInvoke).
That may also work.
Let us know if it does.

Control.Invoke and Control.BeginInvoke execute methods on the controls thread.
The method to run must be passed as a delegate (i.e. function pointer) which is why we need the ClearRBDelegate.
BeginInvoke executes asynchronously; which means that it pushes the execution of the delegate method on to a new thread to run later and continues.
The result is that the ClearRBs method is executed after the form has finished all of its loading.

Just had another thought.
Try running the ClearRBs in Form_Shown instead (without the BeginInvoke).
That may also work.
Let us know if it does.

hey do you mean instead of this line "this.BeginInvoke(new ClearRBDelegate(ClearRBs));" add "this.Form_Shown(new ClearRBDelegate(ClearRBs));"
is it??
there is no such thing

No, I mean instead of changing the RBs in the Load event handler

private void Form1_Load(object sender, EventArgs e)
        {
            this.BeginInvoke(new ClearRBDelegate(ClearRBs));
        }

Add a Shown event handler and change them there

private void Form1_Shown(object sender, EventArgs e)
        {
            ClearRBs();
        }

Or add a OnShown override and use that instead

protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            ClearRBs();
        }

[Edit] This is a test to see if you need to use the BeginInvoke or not.

No, I mean instead of changing the RBs in the Load event handler

private void Form1_Load(object sender, EventArgs e)
        {
            this.BeginInvoke(new ClearRBDelegate(ClearRBs));
        }

Add a Shown event handler and change them there

private void Form1_Shown(object sender, EventArgs e)
        {
            ClearRBs();
        }

Or add a OnShown override and use that instead

protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            ClearRBs();
        }

[Edit] This is a test to see if you need to use the BeginInvoke or not.

hey
yeah it's working

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.