Я делаю форму окна, в которой каждый раз, когда элемент в выпадающем списке выбирается, в форму динамически добавляется другой выпадающий список.Это работает хорошо, моя проблема, однако, в том, что я хочу, чтобы выбранный элемент комбинированного списка не появлялся ни в каких других.Все выпадающие списки извлекаются из массива 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)
{
}