问题遇到的现象和发生背景
点击按钮,添加三个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);
}
运行结果及报错内容
我想要达到的结果
我想要的结果就是新添加的几个ComboBox能够不互相影响