我正在制作一个Windows窗体,其中每次在组合框中选择一个项目时,都会将另一个组合框动态添加到该窗体中。这工作得很好,但是我的问题是,我希望组合框的所选项目不出现在其他任何项目中。所有组合框都从数组comborange
中提取,因此我将基于该索引删除项目。
我的代码几乎可以工作,但是变得非常复杂,所以我知道必须有一种更简单的方法。而且,通过几乎可行的方式,我的意思是添加第一个组合框后,它无法完美运行。
我还应该注意,这都是有条件的,基于复选框,搜索栏也有条件地出现,但是这些与该问题不是特别相关。
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
// Determine the CheckState of the check box.
if (checkBox1.CheckState == CheckState.Checked)
{
// Add second combobox to first groupbox if this is checked
groupBox1.Controls.Add(combo);
//combo.Items.AddRange(comborange);
combo.Location = new System.Drawing.Point(19, 123);
combo.Name = "combo";
combo.Size = new System.Drawing.Size(121, 21);
combo.TabIndex = 0;
combo.SelectedIndexChanged += new System.EventHandler(this.combo_SelectedIndexChanged);
combo.BringToFront();
this.AllowDrop = false;
}
if (checkBox1.CheckState == CheckState.Unchecked)
{
// Remove combobox from groupbox if this is unchecked
groupBox1.Controls.Remove(combo);
Controls.Remove(combo);
combo.Items.Clear();
// Remove search bars other than main one
// Remove first search bar and button
groupBox3.Controls.Remove(search2);
Controls.Remove(search2);
groupBox3.Controls.Remove(sbutton1);
Controls.Remove(sbutton1);
}
}
private void checkBox1_Click(object sender, System.EventArgs e)
{
switch (checkBox1.CheckState)
{
case CheckState.Checked:
// Add second combo box if this is checked
ComboBox combo = new ComboBox();
Controls.Add(combo);
break;
case CheckState.Unchecked:
break;
case CheckState.Indeterminate:
break;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// Clear second combo box if the selection is changed
var combocount = combo.Items.Count;
string combobox1str = (string) comboBox1.SelectedItem;
if (combocount < comborange.Length)
{
combo.Items.Clear();
combo.Items.AddRange(comborange);
combo2.Items.Clear();
combo2.Items.AddRange(comborange);
}
int currentcomboBox1Index = comboBox1.SelectedIndex;
combo.Items.RemoveAt(currentcomboBox1Index);
// Add text box
TextBox search1 = new TextBox();
groupBox3.Controls.Add(search1);
Controls.Add(search1);
search1.Location = new System.Drawing.Point(519, 137);
search1.Size = new System.Drawing.Size(323, 21);
search1.Text = combobox1str;
search1.AcceptsReturn = false;
search1.AcceptsTab = false;
search1.BringToFront();
// Add search button
Button sbutton1 = new Button();
groupBox3.Controls.Add(sbutton1);
Controls.Add(sbutton1);
sbutton1.Location = new System.Drawing.Point(850, 137);
sbutton1.Size = new System.Drawing.Size(63, 21);
sbutton1.Text = "Search";
sbutton1.BringToFront();
}
private void combo_SelectedIndexChanged(object sender, EventArgs e)
{
string combostr = (string)combo.SelectedItem;
// Add text box
TextBox search1 = new TextBox();
groupBox3.Controls.Add(search2);
Controls.Add(search2);
search2.Location = new System.Drawing.Point(519, 187);
search2.Size = new System.Drawing.Size(323, 21);
search2.Text = combostr;
search2.AcceptsReturn = false;
search2.AcceptsTab = false;
search2.BringToFront();
// Add search button
Button sbutton1 = new Button();
groupBox3.Controls.Add(sbutton2);
Controls.Add(sbutton2);
sbutton2.Location = new System.Drawing.Point(850, 187);
sbutton2.Size = new System.Drawing.Size(63, 21);
sbutton2.Text = "Search";
sbutton2.BringToFront();
// Add next combobox
ComboBox combo2 = new ComboBox();
Controls.Add(combo2);
groupBox1.Controls.Add(combo2);
combo2.Location = new System.Drawing.Point(19, 173);
combo2.Name = "combo2";
combo2.Size = new System.Drawing.Size(121, 21);
combo2.TabIndex = 0;
combo2.SelectedIndexChanged += new System.EventHandler(this.combo2_SelectedIndexChanged);
combo2.BringToFront();
this.AllowDrop = false;
// Clear second combo box if the selection is changed
var combo2count = combo2.Items.Count;
if (combo2count < comborange.Length)
{
combo2.Items.Clear();
combo2.Items.AddRange(comborange);
}
int currentcomboIndex = combo.SelectedIndex;
combo2.Items.RemoveAt(currentcomboIndex);
combo2.Items.RemoveAt(currentcomboBox1Index + 1);
}
private void combo2_SelectedIndexChanged(object sender, EventArgs e)
{
}