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