C# 根据组合框选择启用/禁用多个文本框

2024-01-18

我只是一个正在学习如何编码的普通人。我不喜欢代码看起来很草率并且可以合并。话虽这么说,我需要合并一些代码,但我未能找到有关如何执行此操作的信息。

我正在尝试根据组合框选择启用/禁用多个文本框。

这是我试图整合的又长又丑的代码

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

    }

好吧,我只想说非常感谢h8red https://stackoverflow.com/users/2559094/h8red。我从大量的垃圾代码变成了不到 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++;
        }
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 根据组合框选择启用/禁用多个文本框 的相关文章

随机推荐

  • javax.el.E​​LException:无法将类型类 java.util.ArrayList 的 [light、amber、brown、dark] 转换为类 [Ljava.lang.String;

    我正在练习 Head First Servlets and JSP 书中的示例 我正在尝试创建一个简单的标签 其作用类似于html select标签 我根据书中给出的示例创建了 tld 标记处理程序和其余 servlet 以及 JSP 如需
  • 将 PartCover.NET 与 NAnt 集成

    我正在尝试将 PartCover NET 与 NAnt 和 CruiseControl NET 集成 我可以毫无问题地运行 PartCover NET 浏览器 但一旦我尝试在 NAnt 任务 在我的 CCNET 版本中 中运行它 它就无法工
  • 将子类转换为超类后调用方法的有趣行为

    class A int i 1 int f return i class B extends A int i 2 int Override f return i public class override test public stati
  • 在MySQL中存储图像[关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 请给我在 MySQL 数据库中插入图像的查询 我是 stackoverflow 的新手 所以如果我的问题不符合标准 请忽略我 如果映像位于您
  • 人工智能中与最佳优先搜索相关的问题有哪些?

    我知道一般问题包括局部最大值和高原 但是我很好奇是否还有与此特定搜索相关的更多问题 以及为了克服这些问题我的最佳行动方案是什么 有人还可以给我一个例子 说明该搜索适合用于哪种类型的问题吗 最佳优先搜索的问题 它很贪婪 在许多情况下 它会带来
  • MySQL 中 utf8mb4 和 utf8 字符集有什么区别?

    有什么区别utf8mb4 and utf8字符集在MySQL 我已经知道了ASCII UTF 8 UTF 16 and UTF 32编码 但我很想知道有什么区别utf8mb4具有其他编码类型定义的编码组MySQL服务器 使用有什么特别的好处
  • TypeScript 使用实例访问静态变量

    所以在大多数OOP语言中静态变量也可以被称为class变量 即它们的值为shared在该类的所有实例中 例如 在我的游戏中我有一堂课Bullet其扩展为GreenBullet and PinkBullet 我希望这些子类有一个名为 类 或
  • 带有 date_trunk 函数和时区的 Django ORM

    我想用日期截断SQL 函数 但它似乎不适用于时区 使用 Django 进行测试 1 from django db import connection cursor connection cursor cursor execute SELEC
  • SVN 1.3 VS 1.5(最新)

    我们的IT人员已经安装了SVN 1 3 当前版本是1 5 我们正在从 CVS 迁移到 SVN 那么继续使用 1 3 会损失很多吗 我喜欢运行 CVS2SVN 并转移到 SVN 而不需要编写任何脚本 另外 在 CVS 中 我们的项目中有多个目
  • 如果视口为 480 像素或更小,请更改为 FullCalendar 中的 basicDay 视图?

    有没有一种简单的方法可以根据 FullCalendar 中的当前视口大小更改用户的视图 我想做的是在桌面上显示月视图 如果使用 iPhone 或移动设备则显示日视图 目前 使用月视图时 所有事件都被压缩在移动设备上 当前代码 documen
  • scandir - 对数字文件名进行排序

    做了一些搜索 但似乎找不到我正在寻找的确切答案 我想使用 scandir dir 提取带有编号文件名的文件 但让它们正确排序 例如 文件名是 1 something ext 2 something else ext 3 a third na
  • 如何设置等于Android上另一个小部件的宽度

    我需要在文本字段下方绘制一条水平线 使线的宽度等于文本宽度 而不是全屏的宽度 在我的应用程序中 我在视图 水平线 下方有一个文本视图 这线视图的宽度应等于文本视图的宽度 我尝试了 android layout width wrap cont
  • 匹配有效命名空间名称的正则表达式

    我以为这个问题以前有人问过 但我尝试谷歌但没有找到答案 也许我使用了错误的关键字 是否可以使用正则表达式来匹配有效的 C 命名空间名称 Update 感谢大家的回答和研究 这个问题比我想象的要复杂得多 作为奥斯卡 梅德罗斯 https st
  • 从 IIS 上的命令行列出所有托管网站

    我们如何通过 IIS 命令行列出所有托管网站 您可能必须以管理员身份运行它 appcmd list site 直接来自http technet microsoft com en us library cc771280 v ws 10 asp
  • 将一种类型的同构 HList 映射到不同类型的异构 List

    我有一个字符串 HList val strings The Lorax Dr Suess HNil 在另一个特殊类型列表中 case class Title title String words List String case class
  • 在 DAG 中使用 boto3 时,Apache Airflow 无法找到 AWS 凭证

    我们正在使用 ECS Fargate 迁移到 Apache Airflow 我们面临的问题 很简单 我们有一个简单的 DAG 其任务之一是与 AWS 中的某些外部服务进行通信 比方说 从 S3 下载文件 这是 DAG 的脚本 from ai
  • 是否有一个不重复的 List 实现?

    我知道关于SortedSet https docs oracle com javase 9 docs api java util SortedSet html 但就我而言 我需要一些实现List 并不是Set 那么 API 或其他地方是否有
  • 在 C++ 中保存 QML 图像

    我正在尝试使用 qml 显示网络图像 然后使用 c 代码保存该图像 这是qml代码 import QtQuick 2 3 import QtQuick Window 2 2 import com login 1 0 Window visib
  • Android Facebook SDK - 共享对话框不响应取消回调

    我正在我的 Android 应用程序上使用 Facebook sdk 4 0 我发现了这个问题 当我分享帖子时 我可以看到facebook界面 我可以完美地发布和取消它 我注册了回调 但是如果按取消按钮 则不会调用 onCancel 回调
  • C# 根据组合框选择启用/禁用多个文本框

    我只是一个正在学习如何编码的普通人 我不喜欢代码看起来很草率并且可以合并 话虽这么说 我需要合并一些代码 但我未能找到有关如何执行此操作的信息 我正在尝试根据组合框选择启用 禁用多个文本框 这是我试图整合的又长又丑的代码 private v