如何使用不同的单选按钮更改 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() 更改带有单选按钮的标签页。我想知道,如何简化这个 "else if" 结构。可能我需要一个 foreach(),但我找不到答案。
您可以为每个单选按钮设置 Tag
绑定索引。
radioButton1.Tag = 0;
radioButton2.Tag = 1;
...
RadioButton radioButton = sender as RadioButton;
if(radioButton.Checked)
tabControl1.SelectedIndex = (int)radioButton.Tag;
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(),但我找不到答案。
您可以为每个单选按钮设置 Tag
绑定索引。
radioButton1.Tag = 0;
radioButton2.Tag = 1;
...
RadioButton radioButton = sender as RadioButton;
if(radioButton.Checked)
tabControl1.SelectedIndex = (int)radioButton.Tag;