RichardQY1 2022-08-22 11:49 采纳率: 100%
浏览 81
已结题

C# 动态添加ComboBox遇到的问题

问题遇到的现象和发生背景

点击按钮,添加三个ComboBox和一个TextBox。添加完成后,如图所示,修改其中一个ComboBox以后,会导致另外几个新创建的ComboBox也会跟着修改,TextBox没有这个问题,主要是ComboBox有这个问题

问题相关代码,请勿粘贴截图
public void button_Add_Click(object sender, EventArgs e)
        {
            //创建新的荷载工况和加载方式的ComboBox和荷载值的TextBox
            num_ClickButtonAdd++;

            ComboBox comboBox_LoadCase_Add = new ComboBox();
            ComboBox comboBox_LoadType_Add = new ComboBox();
            ComboBox comboBox_Dir_Add = new ComboBox();
            TextBox textBox_LoadNum_Add = new TextBox();

            comboBox_LoadCase_Add.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            comboBox_LoadCase_Add.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            comboBox_LoadCase_Add.FormattingEnabled = true;
            comboBox_LoadCase_Add.Location = new System.Drawing.Point(8, (int)comboBox_LoadCase0.Tag + num_ClickButtonAdd * 40);
            comboBox_LoadCase_Add.Name = "comboBox_LoadCase" + num_ClickButtonAdd.ToString();
            comboBox_LoadCase_Add.Size = new System.Drawing.Size(100, 28);
            comboBox_LoadCase_Add.Tag = comboBox_LoadCase_Add.Location.Y;
            this.panel_LoadInfo.Controls.Add(comboBox_LoadCase_Add);
            comboBox_LoadCase_Add.DataSource = comboBox_LoadCase0.Items;
            comboBox_LoadCase_Add.SelectedIndex = 0;

            comboBox_LoadType_Add.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            comboBox_LoadType_Add.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            comboBox_LoadType_Add.FormattingEnabled = true;
            comboBox_LoadType_Add.Location = new System.Drawing.Point(114, (int)comboBox_LoadType0.Tag + num_ClickButtonAdd * 40);
            comboBox_LoadType_Add.Name = "comboBox_LoadType" + num_ClickButtonAdd.ToString();
            comboBox_LoadType_Add.Size = new System.Drawing.Size(140, 28);
            comboBox_LoadType_Add.Tag = comboBox_LoadType_Add.Location.Y;
            this.panel_LoadInfo.Controls.Add(comboBox_LoadType_Add);
            comboBox_LoadType_Add.DataSource = comboBox_LoadType0.Items;
            comboBox_LoadType_Add.SelectedIndex = 0;

            comboBox_Dir_Add.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            comboBox_Dir_Add.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            comboBox_Dir_Add.FormattingEnabled = true;
            comboBox_Dir_Add.Location = new System.Drawing.Point(260, (int)comboBox_Dir0.Tag + num_ClickButtonAdd * 40);
            comboBox_Dir_Add.Name = "comboBox_Dir" + num_ClickButtonAdd.ToString();
            comboBox_Dir_Add.Size = new System.Drawing.Size(130, 28);
            comboBox_Dir_Add.Tag = comboBox_Dir_Add.Location.Y;
            this.panel_LoadInfo.Controls.Add(comboBox_Dir_Add);
            comboBox_Dir_Add.DataSource = comboBox_Dir0.Items;
            comboBox_Dir_Add.SelectedIndex = 5;

            textBox_LoadNum_Add.Location = new System.Drawing.Point(396, (int)textBox_LoadNum0.Tag + num_ClickButtonAdd * 40);
            textBox_LoadNum_Add.Name = "textBox_LoadNum" + num_ClickButtonAdd.ToString();
            textBox_LoadNum_Add.Size = new System.Drawing.Size(80, 30);
            textBox_LoadNum_Add.Tag = textBox_LoadNum_Add.Location.Y;
            textBox_LoadNum_Add.Text = "0";
            this.panel_LoadInfo.Controls.Add(textBox_LoadNum_Add);
        }
运行结果及报错内容

img

img

img

我想要达到的结果

我想要的结果就是新添加的几个ComboBox能够不互相影响

  • 写回答

2条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 系统已结题 8月30日
      • 已采纳回答 8月22日
      • 修改了问题 8月22日
      • 修改了问题 8月22日
      • 展开全部

      悬赏问题

      • ¥30 AndroidBench&eMMC内存测试速度&Android
      • ¥15 W10 文件共享失败 怎么解决
      • ¥20 b站私信完整导出的方法
      • ¥15 边界图中的x、y轴解释
      • ¥30 如何用matlab求解复杂方程
      • ¥15 VUE TS中怎么调用第三方js
      • ¥15 Activiti如何将值传入textValue
      • ¥20 在linux里安装lummps时 在make mpi里找不到rpc
      • ¥25 python先到先得调度和轮询调度
      • ¥15 SDK Manager闪退