问题
I am trying to have a dynamic set of comboboxes that change based on the last selection. However, I want them to create a custom string of numbers for each possible outcome. When I would like is to have each one add a value of 1s, 10s, 100s, and 1000s.
(for example selection the first option in each combobox would display 1111 in the textbox2 or if they select the second option in combobox1 then the first for the rest it would display 1112) here is the code below:
Essentially I just want to add values that are created based on the selection from each combobox in textbox2 and have them display.
private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
{
comboBox2.Items.Clear();
comboBox2.Items.Add("1");
comboBox2.Items.Add("2");
comboBox2.Items.Add("3");
comboBox2.Items.Add("4");
}
else if (comboBox1.SelectedIndex == 0)
{
comboBox2.Items.Clear();
comboBox2.Items.Add("1");
comboBox2.Items.Add("2");
comboBox2.Items.Add("3");
comboBox2.Items.Add("4");
}
else if (comboBox1.SelectedIndex == 1)
{
comboBox2.Items.Clear();
comboBox2.Items.Add("5");
comboBox2.Items.Add("6");
comboBox2.Items.Add("7");
comboBox2.Items.Add("8");
}
else if (comboBox1.SelectedIndex == 2)
{
comboBox2.Items.Clear();
comboBox2.Items.Add("Corp Over 250k");
comboBox2.Items.Add("Corp Under 250k");
comboBox2.Items.Add("Hybrid Over 250k");
comboBox2.Items.Add("Hybrid Under 250k");
}
if (comboBox1.SelectedIndex == 0)
{
comboBox2.Items.Clear();
comboBox2.Items.Add("1");
comboBox2.Items.Add("2");
comboBox2.Items.Add("3");
comboBox2.Items.Add("4");
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedIndex == 0)
{
comboBox3.Items.Clear();
comboBox3.Items.Add("Move Amendment Override");
}
else if (comboBox2.SelectedIndex == 1)
{
comboBox3.Items.Clear();
comboBox3.Items.Add("Move Ammendment Override INTERNAL");
}
else if (comboBox2.SelectedIndex == 2)
{
comboBox3.Items.Clear();
comboBox3.Items.Add("250 - 399");
comboBox3.Items.Add("400 - 599");
comboBox3.Items.Add("600 - 799");
comboBox3.Items.Add("800 - 999");
comboBox3.Items.Add("1000 - 1499");
comboBox3.Items.Add("1500 - 1999");
comboBox3.Items.Add("2000+");
}
else if (comboBox2.SelectedIndex == 3)
{
comboBox3.Items.Clear();
comboBox3.Items.Add("Move Hybrid Documents");
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
回答1:
Normally, these items would be populated by some external source, like a database. However, as you are hard-coding the values in each drop down, I suggest you populate the textbox like this:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Dictionary<int, string[]> comboBox1Options = new Dictionary<int, string[]>();
comboBox1Options.Add(0, new[] { "1", "2", "3", "4" });
comboBox1.Items.Clear();
comboBox1.Items.AddRange(comboBox1Options[0]);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Dictionary<int, string[]> comboBox2Options = new Dictionary<int, string[]>();
comboBox2Options.Add(0, new[] { "1", "2", "3", "4" });
comboBox2Options.Add(1, new[] { "5", "6", "7", "8" });
comboBox2Options.Add(2, new[] { "Corp Over 250k", "Corp Under 250k", "Hybrid Over 250k", "Hybrid Under 250k" });
comboBox2.Items.Clear();
comboBox2.Items.AddRange(comboBox2Options[comboBox1.SelectedIndex]);
textBox2.Text = comboBox1.SelectedIndex.ToString();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
Dictionary<int, string[]> comboBox3Options = new Dictionary<int, string[]>();
comboBox3Options.Add(0, new[] { "Move Amendment Override" });
comboBox3Options.Add(1, new[] { "Move Ammendment Override INTERNAL" });
comboBox3Options.Add(2, new[] { "250 - 399", "400 - 599", "600 - 799", "800 - 999" });
comboBox3Options.Add(3, new[] { "Move Hybrid Documents" });
comboBox3.Items.Clear();
comboBox3.Items.AddRange(comboBox3Options[comboBox2.SelectedIndex]);
textBox2.Text += comboBox2.SelectedIndex.ToString();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
textBox2.Text += comboBox3.SelectedIndex.ToString();
}
}
}
Additionally, using a dictionary instead of many if statements drastically simplifies your code.
来源:https://stackoverflow.com/questions/28730637/how-do-i-assign-an-int-from-comboboxes-to-add-them-and-display-result-in-textbox