Reputation: 21
I am having a problem with the TabControl from the Containers toolbox.
I can get the tabs set up as pages and the layouts are fine, all was working well until I place 4 RadioButtons on each page. If I click on a RadioButton it is checked, if I click on another that becomes checked and the first is cleared, so far so good. The problem starts 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?
Upvotes: 0
Views: 1765
Reputation: 116
Old thread but I'll put my two cents solution in here.
Deriving on your solution I just made it un-check all radio buttons on all the tab pages apart of a tab control that aren't on the active tab page at the time of checking.
/// <summary> Unchecks all the radio buttons on each tab page except the active one </summary>
/// <param name="activePage"> The active tab page when the radio button was clicked</param>
private void UncheckLayouts(TabPage activePage)
{
foreach (TabPage tabPage in tabLayouts.TabPages)
{
if (tabPage == activePage) continue;
foreach (RadioButton rb in tabPage.Controls)
{
rb.Checked = false;
}
}
}
and then each CheckedChanged event:
UncheckLayouts(tabControl.SelectedTab);
Upvotes: 0
Reputation: 21
Thanks for your input. I have 7 tab control pages which represents 7 days of a week. each page has 4 DGVs. items are selected from a central list DB and placed into a DGV for the day required. the DGV that is required is either selected by checking a radioButton or by clicking on the DGV which selects the radioButton. where the item is placed in dependant on only one radioButton being checked. As there are only 28 radioButtons, I will give each a checkChanged event as below.
public int selectedDay{ get; set;}
public void clearOtherDays(int day)
{
if ( day != 1)
{ rbB1.Checked = false; rbL1.Checked = false; rbD1.Checked = false; rbS1.Checked = false;}
if (day != 2)
{ rbB2.Checked = false; rbL2.Checked = false; rbD2.Checked = false; rbS2.Checked = false;}
if (day != 3)
{ rbB3.Checked = false; rbL3.Checked = false; rbD3.Checked = false; rbS3.Checked = false;}
if (day != 4)
{ rbB4.Checked = false; rbL4.Checked = false; rbD4.Checked = false; rbS4.Checked = false;}
if (day != 5)
{ rbB5.Checked = false; rbL5.Checked = false; rbD5.Checked = false; rbS5.Checked = false;}
if (day != 6)
{ rbB6.Checked = false; rbL6.Checked = false; rbD6.Checked = false; rbS6.Checked = false;}
if (day != 7)
{ rbB7.Checked = false; rbL7.Checked = false; rbD7.Checked = false; rbS7.Checked = false;}
}
private void rbL1_CheckedChanged(object sender, EventArgs e)
{
if (rbL1.Checked) {clearOtherDays(1); selectedDay = 1; }
}
this seems to do the job, but if there is a better solution I am open to ideas, learning is good.
Many Thanks.
Upvotes: 0
Reputation: 1435
I'm with Hans Passant. First off, this sounds like a design you should really reconsider, making any control interact with hidden controls that don't display with the other controls that interact with it?!? I caution you to reconsider your design.
with that disclaimer, i'm still annoyed when people don't answer my question and all they do is criticize my question and really ask me 'why do that', or you shouldn't do that. my attempt is to give you positive criticism to rethink your design. BUT i'll still give you the best answer i can to your question...
so i would simply code it into the checked event on each radio button. it's a little quirky, but i think your design and what you are trying to do is (very) odd too.
So inside each checked event, you need to first: test that the current (new) checked value is true and not false, because the checked events fire when both things happen: when the radio button becomes checked and when it becomes unchecked. this is a bit odd if you don't look for this or know to look for this.
second, if it is checked, then i would call a separate 'UncheckRadioButtons' method with a parameter that passes the radio button that just got checked. in that UncheckRadioButtons method, i'd have an array or list of radio buttons that you code which contains all the radio buttons that you only want one of them checked at once, and then a for each loop to iterate over every item in your RadioButton list or array. inside your loop, you simple test whether that radio button is the same radio button that was passed to your UncheckRadioButtons method, and if it is not the same radio button and it is checked, then uncheck that radio button. so your UncheckRadioButtons method will uncheck all the RadioButtons except for the one RadioButton that is passed to it as a parameter.
sorry that i don't have the time to code such a method for you to even better improve my answer for you. but i wanted to actually give you a real answer at least to help you on your way. :)
Upvotes: 1