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# 根据组合框选择启用/禁用多个文本框 的相关文章

  • 在 C 语言中,为什么数组的地址等于它的值?

    在下面的代码中 指针值和指针地址与预期不同 但数组值和地址则不然 怎么会这样 Output my array 0022FF00 my array 0022FF00 pointer to array 0022FF00 pointer to a
  • 无法继承形状

    为什么我不能使用继承 a 的类Shapes class http msdn microsoft com en us library ms604615 28v vs 90 29 我需要延长Rectangle具有一些方法的类 但我想以与使用相同
  • 在 C++ 代码中转换字符串

    我正在学习 C 并开发一个项目来练习 但现在我想在代码中转换一个变量 字符串 就像这样 用户有一个包含 C 代码的文件 但我希望我的程序读取该文件并插入将其写入代码中 如下所示 include
  • C# 中一次性对象克隆会导致内存泄漏吗?

    检查这个代码 class someclass IDisposable private Bitmap imageObject public void ImageCrop int X int Y int W int H imageObject
  • 防止控制台应用程序中的内存工作集最小化?

    我想防止控制台应用程序中的内存工作集最小化 在Windows应用程序中 我可以这样做覆盖 SC MINIMIZE 消息 http support microsoft com kb 293215 en us fr 1 但是 如何在控制台应用程
  • Android NDK 代码中的 SIGILL

    我在市场上有一个 NDK 应用程序 并获得了有关以下内容的本机崩溃报告 SIGILL信号 我使用 Google Breakpad 生成本机崩溃报告 以下是详细信息 我的应用程序是为armeabi v7a with霓虹灯支持 它在 NVIDI
  • 来自嵌入图像的 BitmapSource

    我的目标是在 WPF 窗口上重写 OnRender 方法中绘制图像 someImage png 它是嵌入资源 protected override void OnRender System Windows Media DrawingCont
  • 保证复制省略是否适用于函数参数?

    如果我理解正确的话 从 C 17 开始 这段代码现在要求不进行任何复制 Foo myfunc void return Foo auto foo myfunc no copy 函数参数也是如此吗 下面的代码中的副本会被优化掉吗 Foo myf
  • LinkLabel 无下划线 - Compact Framework

    我正在使用 Microsoft Compact Framework 开发 Windows CE 应用程序 我必须使用 LinkLabel 它必须是白色且没有下划线 因此 在设计器中 我将字体颜色修改为白色 并在字体对话框中取消选中 下划线
  • wordexp 失败时我们需要调用 wordfree 吗?

    wordexp 失败时我们需要调用 wordfree 吗 在某些情况下 调用 wordfree 似乎会出现段错误 例如 当 wordfree 返回字符串为 foo bar 的错误代码时 这在手册页中并不清楚 我已经看到在某些错误情况下使用了
  • 如何防止 Blazor NavLink 组件的默认导航

    从 Blazor 3 1 Preview 2 开始 应该可以防止默认导航行为 https devblogs microsoft com aspnet asp net core updates in net core 3 1 preview
  • 让网络摄像头在 OpenCV 中工作

    我正在尝试让我的网络摄像头在 Windows 7 64 位中的 OpenCV 版本 2 2 中捕获视频 但是 我遇到了一些困难 OpenCV 附带的示例二进制文件都无法检测到我的网络摄像头 最近我发现这篇文章表明答案在于重新编译一个文件 o
  • 对于 C# Express 用户来说,有哪些好的工具可以识别可能重复的代码? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 也可以看看 有什么工具可以检查重复的 VB NET 代码吗 https stackoverflow c
  • ASP.NET Core 中间件与过滤器

    在阅读了 ASP NET Core 中间件之后 我对何时应该使用过滤器以及何时应该使用中间件感到困惑 因为它们似乎实现了相同的目标 什么时候应该使用中间件而不是过滤器 9频道有一个关于此的视频 ASP NET 怪物 91 中间件与过滤器 h
  • 使用 gcc 时在头文件中查找定义的好方法是什么?

    在使用 gcc 时 有人有推荐的方法在头文件中查找定义吗 使用 MSVC 时 我只需右键单击并选择 转到定义 这非常好 我使用过 netbeans gcc 它确实有代码帮助 包括到定义的超链接 所以这是一种选择 但是 我想知道是否有任何其他
  • C++ 指针引用混淆

    struct leaf int data leaf l leaf r struct leaf p void tree findparent int n int found leaf parent 这是 BST 的一段代码 我想问一下 为什么
  • 如何从 Windows Phone 7 模拟器获取数据

    我有一个 WP7 的单元测试框架 它在手机上运行 结果相当难以阅读 因此我将它们写入 XDocument 我的问题是 如何才能将这个 XML 文件从手机上移到我的桌面上 以便我可以实际分析结果 到目前为止 我所做的是将 Debugger B
  • 如果将变量设置为等于新对象,旧对象会发生什么?

    假设我们有一个 X 类not有一个超载的operator 功能 class X int n X n 0 X int n n n int main X a 1 an object gets constructed here more code
  • 声明一个负长度的数组

    当创建负长度数组时 C 中会发生什么 例如 int n 35 int testArray n for int i 0 i lt 10 i testArray i i 1 这段代码将编译 并且启用 Wall 时不会出现警告 并且似乎您可以分配
  • 如果找不到指定的图像文件,显示默认图像的最佳方式?

    我有一个普通的电子商务应用程序 我将 ITEM IMAGE NAME 存储在数据库中 有时经理会拼错图像名称 为了避免 丢失图像 IE 中的红色 X 每次显示产品列表时 我都会检查服务器中是否有与该产品相关的图像 如果该文件不存在 我会将其

随机推荐

  • 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