简体   繁体   English

C# 基于组合框选择启用/禁用多个文本框

[英]C# Enable/Disable multiple textbox based on combobox selection

I am just an ordinary guy who is learning how to code.我只是一个正在学习如何编码的普通人。 I don't like when code looks sloppy and can be consolidated.我不喜欢代码看起来很草率并且可以合并。 With that being said I need to consolidate some code, but I have failed to find information on how to do it.话虽如此,我需要整合一些代码,但我未能找到有关如何执行此操作的信息。

I am trying to enable/disable multiple textbox based of combobox selection.我正在尝试基于组合框选择启用/禁用多个文本框。

Here is the long and ugly code I am trying to consolidate这是我试图整合的又长又丑的代码

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;
        }

    }

Okay, I just have to say thank you very much h8red .好的,我只能说非常感谢h8red I went from a massive amount of junk code to less than 10 lines of code.我从大量的垃圾代码变成了不到 10 行代码。

Here is the resulting consolidation of code by placing the textboxes in a panel and then enabling/disabling them based on the combo box user input.这是通过将文本框放置在面板中然后根据组合框用户输入启用/禁用它们来合并代码的结果。

        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++;
        }
    }

You need a container control class to have all the textboxes in it.您需要一个容器控件类来包含其中的所有文本框。 The simplest one is Panel class.最简单的一种是Panel类。 Just add all the texboxes to panel.Controls and you will be able to access them in a loop.只需将所有文本panel.Controls添加到panel.Controls ,您就可以循环访问它们。 You will also have to manually set a tab in order to be able to enable only some of them.您还必须手动设置一个选项卡才能仅启用其中的一些选项卡。

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

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