Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have a winforms app in c# witch has a tabControl with 3 pages each page has 4 radioButtons. The problem is when I change to a different tab page and click a RadioButton, it becomes checked but the RadioButton on the previous page remains checked as well.

Is there any way of grouping all the RadioButtons on all the pages together, so that only one RadioButton is checked at any one time?

I tried the code below but get an error selectin the radiobutton.
<pre lang="c#">You can not bind the object of type 'System.Windows.Forms.Label' to type 'System.Windows.Forms.RadioButton'.</pre>

What I have tried:

C#
private void UncheckLayouts(TabPage activePage)
       {
           foreach (Control control in tabControl1.Controls)
           {
               if (control is RadioButton)
               {
                   RadioButton rb = control as RadioButton;
                   rb.Checked = false;
               }
           }
       }
       private void radioButton1_Click(object sender, EventArgs e)
       {
           UncheckLayouts(tabControl1.SelectedTab);
       }

       private void radioButton2_Click(object sender, EventArgs e)
       {
           UncheckLayouts(tabControl1.SelectedTab);
       }



I get no errors but can switch tabs and the radiobuttons stay selected
Posted
Updated 30-Apr-19 23:34pm
v5
Comments
Philippe Mori 12-Dec-16 16:30pm    
This is a bad UI design. You should always see all related radio buttons at once.

And there is nothing difficult to uncheck a radio button as you just have to set its Checked property to false.

Every control inside the tab page will not be radiobutton type, you need to check first if it is radio button then cast it and change it's state :

C#
foreach (Control control in tabPage.Controls)
{
   if(control is RadioButton)
   {
       RadioButton rb =  control as RadioButton;  
       rb.Checked = false;
   }
}


another solution is to filter out the RadioButton type instances from the Controls collection like:

C#
foreach (RadioButton rb in tabPage.Controls.OfType<RadioButton>())
{  
   rb.Checked = false;
}
 
Share this answer
 
v2
Comments
Marc-IT 12-Dec-16 10:22am    
Hi,
Thanks for the tips.
I get no errors now but the radiobutton stays selected while moving to another tab :\
Ehsan Sajjad 12-Dec-16 10:25am    
do you have controls directly in the tabpage or you have put a user control inside the tabpage, if yes then you would need to iterate the controls inside the user control
Marc-IT 12-Dec-16 10:33am    
Sorry, i´m new at this.
What i did is add the tab control in a form and deveral radio buttons in the each page of the tabControl.
Can you help please?
In order to uncheck the radio buttons when switching tabs you will need to run the foreach loop code in the TabControl's Deselecting event.

The below effectively Unchecks all radio buttons on the tab page that you clicked out of.

C#
private void tabControl1_Deselecting(object sender, TabControlCancelEventArgs e) {
    foreach (var ctrl in e.TabPage.Controls) {
         if (ctrl is RadioButton) {
             RadioButton tb = ctrl as RadioButton;
             rb.Checked = false;
         }
    }
}
 
Share this answer
 
v2
Comments
Marc-IT 13-Dec-16 3:23am    
Thanks for the help,
However there must be another problem with my app.
It still does nothing, the radiobutton stays selected even when i change tabpage :\
What i did is simply put 4 radiobuttons on each of the 3 pages with the designer.
am i missing something?
Ramza360 13-Dec-16 9:58am    
Hi Marc you need to subscribe to the Deselecting Event on the TabControl.
Right click the TabControl and select Properties. In the Property Grid click the thunderbolt icon which will show you the events. Locate the Deselecting Event and type in the name of the method you would like. Copy/Paste the code into that method (only copy the foreach loop into the new method)
I would probably add a "Clicked" event to all of the radio buttons, and set the handler to use the same method. At that point, you can marshal what happens based on what button was clicked. If you want all of the radio buttons in a given group to be unchecked, you may need to set their TabStop property to false.
 
Share this answer
 
v2
Comments
Marc-IT 12-Dec-16 11:23am    
Hi,
Thanks for the help,
Can you please take a look at my post, i updated the code but still not working.
private void tabList_Deselecting(object sender, KryptonPageCancelEventArgs e)
{
foreach (var ctrl in e.Item.Controls)
{
if (ctrl is ComponentFactory.Krypton.Toolkit.KryptonRadioButton)
{
ComponentFactory.Krypton.Toolkit.KryptonRadioButton rb = ctrl as ComponentFactory.Krypton.Toolkit.KryptonRadioButton;
rb.Checked = false;
}
}
}
 
Share this answer
 
Comments
Richard MacCutchan 1-May-19 5:49am    
Reason for my vote of 1: this is nothing to do with the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900