C#在tabcontrol中更改选项卡时如何清除radiobutton [英] C# how to clear radiobutton when changing tab in tabcontrol

查看:264
本文介绍了C#在tabcontrol中更改选项卡时如何清除radiobutton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在c#中有一个winforms应用程序有一个tabControl,每页3页有4个radioButtons。问题是,当我切换到另一个标签页并单击一个RadioButton时,它会被检查,但上一页的RadioButton也会被检查。



有没有将所有页面上的所有RadioButton分组在一起的方式,以便一次只检查一个RadioButton?



我尝试了下面的代码,但得到一个错误选择radiobutton。

< pre lang =c#>您不能将'System.Windows.Forms.Label'类型的对象绑定到'System.Windows.Forms.RadioButton'。 < / pre>



我尝试过:



< pre lang =C#> private void UncheckLayouts(TabPage activePage)
{
foreach (控制控件 in tabControl1.Controls)
{
if (con trol RadioButton)
{
RadioButton rb = control as RadioButton;
rb.Checked = false ;
}
}
}
私有 void radioButton1_Click ( object sender,EventArgs e)
{
UncheckLayouts(tabControl1.SelectedTab);
}

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







我没有错误,但可以切换标签,并且无线电按钮保持选择状态

解决方案

标签页中的每个控件都不是radiobutton类型,你需要首先检查它是否是单选按钮然后转换它并改变它的状态:



  foreach (控制控制 in  tabPage.Controls)
{
if (control RadioButton)
{
RadioButton rb = control as RadioButton;
rb.Checked = false ;
}
}





另一种解决方案是从Controls集合中过滤掉RadioButton类型实例,如: br $> b $ b

  foreach (RadioButton rb  in  tabPage.Controls.OfType< RadioButton>())
{
rb.Checked = false ;
}


要在切换标签时取消选中单选按钮,您需要在TabControl的取消选择事件中运行foreach循环代码。 />


以下有效取消选中您单击的标签页上的所有单选按钮。



  private   void  tabControl1_Deselecting( object  sender,TabControlCancelEventArgs e){
foreach var ctrl in e.TabPage.Controls){
if (ctrl RadioButton){
RadioButton tb = ctrl as RadioButton;
rb.Checked = false ;
}
}
}


我可能会向所有单选按钮添加Clicked事件,并且设置处理程序以使用相同的方法。此时,您可以根据点击的按钮编组发生的事情。如果您希望取消选中给定组中的所有单选按钮,则可能需要将其TabStop属性设置为false。


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:

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

解决方案

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 :

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:

foreach (RadioButton rb in tabPage.Controls.OfType<RadioButton>())
{  
   rb.Checked = false;
}


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.

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


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.


这篇关于C#在tabcontrol中更改选项卡时如何清除radiobutton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆