繁体   English   中英

如何使用不同的单选按钮更改tabcontrol索引? C#

[英]How to change tabcontrol index with different radiobuttons? C#

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()更改带有单选按钮的标签页。 我在想,如何简化这种“否则”结构。 可能需要foreach(),但找不到答案。

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

radioButton1.Tag = 0;
radioButton2.Tag = 1;
...

RadioButton radioButton = sender as RadioButton;
if(radioButton.Checked)
    tabControl1.SelectedIndex = (int)radioButton.Tag;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM