【问题标题】:How to change tabcontrol index with different radiobuttons? C#如何使用不同的单选按钮更改 tabcontrol 索引? C#
【发布时间】:2019-03-21 13:07:22
【问题描述】:
private void radioButtons_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton radioButton = sender as RadioButton;
        if (radioButton1.Checked) tabControl1.SelectedIndex = 0;
        else if (radioButton2.Checked) tabControl1.SelectedIndex = 1;
        else if (radioButton3.Checked) tabControl1.SelectedIndex = 2;
        else if (radioButton4.Checked) tabControl1.SelectedIndex = 3;
        else if (radioButton5.Checked) tabControl1.SelectedIndex = 4;
        else if (radioButton6.Checked) tabControl1.SelectedIndex = 5;
        else if (radioButton7.Checked) tabControl1.SelectedIndex = 6;
        else if (radioButton8.Checked) tabControl1.SelectedIndex = 7;
    }

如您所见,我正在使用 EventHandler() 更改带有单选按钮的标签页。我想知道,我怎样才能简化这个“else if”结构。可能我需要一个 foreach(),但我找不到答案。

【问题讨论】:

    标签: c# foreach radio-button tabcontrol


    【解决方案1】:

    您可以为每个与索引绑定的单选按钮设置Tag

    radioButton1.Tag = 0;
    radioButton2.Tag = 1;
    ...
    
    RadioButton radioButton = sender as RadioButton;
    if(radioButton.Checked)
        tabControl1.SelectedIndex = (int)radioButton.Tag;
    

    【讨论】:

    • 谢谢!我还用“foreach”标记了它们,所以现在代码看起来更令人愉快。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    相关资源
    最近更新 更多