C#根据组合框选择启用/禁用多个文本框
我只是一个普通人,正在学习如何编码。我不喜欢代码看起来邋遢而且可以合并。说到这里,我需要整合一些代码,但是我没有找到关于如何整合的信息 我正在尝试启用/禁用基于组合框选择的多个文本框 下面是我试图整合的冗长而难看的代码C#根据组合框选择启用/禁用多个文本框,c#,combobox,textbox,enable-if,consolidation,C#,Combobox,Textbox,Enable If,Consolidation,我只是一个普通人,正在学习如何编码。我不喜欢代码看起来邋遢而且可以合并。说到这里,我需要整合一些代码,但是我没有找到关于如何整合的信息 我正在尝试启用/禁用基于组合框选择的多个文本框 下面是我试图整合的冗长而难看的代码 private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { int Combobox_Process_Selected_Index = comboBox2.Select
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
int Combobox_Process_Selected_Index = comboBox2.SelectedIndex;
if (Combobox_Process_Selected_Index == 0)
{
textBox2.Enabled = false;
textBox3.Enabled = false;
textBox4.Enabled = false;
textBox5.Enabled = false;
textBox6.Enabled = false;
textBox7.Enabled = false;
textBox8.Enabled = false;
textBox9.Enabled = false;
textBox10.Enabled = false;
textBox11.Enabled = false;
textBox12.Enabled = false;
textBox13.Enabled = false;
}
if (Combobox_Process_Selected_Index == 1)
{
textBox2.Enabled = true;
textBox3.Enabled = false;
textBox4.Enabled = false;
textBox5.Enabled = false;
textBox6.Enabled = false;
textBox7.Enabled = false;
textBox8.Enabled = false;
textBox9.Enabled = false;
textBox10.Enabled = false;
textBox11.Enabled = false;
textBox12.Enabled = false;
textBox13.Enabled = false;
}
if (Combobox_Process_Selected_Index == 2)
{
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = false;
textBox5.Enabled = false;
textBox6.Enabled = false;
textBox7.Enabled = false;
textBox8.Enabled = false;
textBox9.Enabled = false;
textBox10.Enabled = false;
textBox11.Enabled = false;
textBox12.Enabled = false;
textBox13.Enabled = false;
}
if (Combobox_Process_Selected_Index == 3)
{
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = false;
textBox6.Enabled = false;
textBox7.Enabled = false;
textBox8.Enabled = false;
textBox9.Enabled = false;
textBox10.Enabled = false;
textBox11.Enabled = false;
textBox12.Enabled = false;
textBox13.Enabled = false;
}
if (Combobox_Process_Selected_Index == 4)
{
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = true;
textBox6.Enabled = false;
textBox7.Enabled = false;
textBox8.Enabled = false;
textBox9.Enabled = false;
textBox10.Enabled = false;
textBox11.Enabled = false;
textBox12.Enabled = false;
textBox13.Enabled = false;
}
if (Combobox_Process_Selected_Index == 5)
{
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = true;
textBox6.Enabled = true;
textBox7.Enabled = false;
textBox8.Enabled = false;
textBox9.Enabled = false;
textBox10.Enabled = false;
textBox11.Enabled = false;
textBox12.Enabled = false;
textBox13.Enabled = false;
}
if (Combobox_Process_Selected_Index == 6)
{
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = true;
textBox6.Enabled = true;
textBox7.Enabled = true;
textBox8.Enabled = false;
textBox9.Enabled = false;
textBox10.Enabled = false;
textBox11.Enabled = false;
textBox12.Enabled = false;
textBox13.Enabled = false;
}
if (Combobox_Process_Selected_Index == 7)
{
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = true;
textBox6.Enabled = true;
textBox7.Enabled = true;
textBox8.Enabled = true;
textBox9.Enabled = false;
textBox10.Enabled = false;
textBox11.Enabled = false;
textBox12.Enabled = false;
textBox13.Enabled = false;
}
if (Combobox_Process_Selected_Index == 8)
{
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = true;
textBox6.Enabled = true;
textBox7.Enabled = true;
textBox8.Enabled = true;
textBox9.Enabled = true;
textBox10.Enabled = false;
textBox11.Enabled = false;
textBox12.Enabled = false;
textBox13.Enabled = false;
}
if (Combobox_Process_Selected_Index == 9)
{
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = true;
textBox6.Enabled = true;
textBox7.Enabled = true;
textBox8.Enabled = true;
textBox9.Enabled = true;
textBox10.Enabled = true;
textBox11.Enabled = false;
textBox12.Enabled = false;
textBox13.Enabled = false;
}
if (Combobox_Process_Selected_Index == 10)
{
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = true;
textBox6.Enabled = true;
textBox7.Enabled = true;
textBox8.Enabled = true;
textBox9.Enabled = true;
textBox10.Enabled = true;
textBox11.Enabled = true;
textBox12.Enabled = false;
textBox13.Enabled = false;
}
if (Combobox_Process_Selected_Index == 11)
{
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = true;
textBox6.Enabled = true;
textBox7.Enabled = true;
textBox8.Enabled = true;
textBox9.Enabled = true;
textBox10.Enabled = true;
textBox11.Enabled = true;
textBox12.Enabled = true;
textBox13.Enabled = false;
}
if (Combobox_Process_Selected_Index == 12)
{
textBox2.Enabled = true;
textBox3.Enabled = true;
textBox4.Enabled = true;
textBox5.Enabled = true;
textBox6.Enabled = true;
textBox7.Enabled = true;
textBox8.Enabled = true;
textBox9.Enabled = true;
textBox10.Enabled = true;
textBox11.Enabled = true;
textBox12.Enabled = true;
textBox13.Enabled = true;
}
}
您需要一个容器控件类来包含所有文本框。最简单的是类。只需将所有文本框添加到
面板。控件
,您就可以在循环中访问它们。您还必须手动设置一个选项卡,以便只启用其中的一部分。好的,我要说的是非常感谢。我从大量的垃圾代码变成了不到10行的代码
下面是通过将文本框放置在面板中,然后根据组合框用户输入启用/禁用它们而得到的代码合并
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
//set a counter to track textbox number and compare with combobox selection
int countTB = 0;
//grab combobox selection and place as an integer
int Combobox_Process_Selected_Index = comboBox2.SelectedIndex;
//this looks at each child control in my panel (named panel1)
foreach (Control child in panel1.Controls)
{
//this looks at each textbox individually and enables them until combo selection is reached and disables the rest
TextBox textBox = child as TextBox;
if (countTB < Combobox_Process_Selected_Index)
{
textBox.Enabled = true;
}
else
textBox.Enabled = false;
countTB++;
}
}
private void组合框2\u SelectedIndexChanged(对象发送方,事件参数e)
{
//设置计数器以跟踪文本框编号并与组合框选择进行比较
int countTB=0;
//抓取组合框选择并作为整数放置
int Combobox\u Process\u Selected\u Index=comboBox2.SelectedIndex;
//这将查看我的面板(名为panel1)中的每个子控件
foreach(面板1.控件中的控件子项)
{
//这将单独查看每个文本框,并启用它们,直到到达组合选择,并禁用其余文本框
TextBox TextBox=作为TextBox的子项;
if(countTB<组合框\u进程\u选定\u索引)
{
textBox.Enabled=true;
}
其他的
textBox.Enabled=false;
countTB++;
}
}
如果您能正式接受我的回答,我们将不胜感激。