如何从组合框中分配一个 INT 以添加它们并在 textbox1 中显示结果
How do I assign an INT from comboboxes to add them and display result in textbox1
我正在尝试根据最后一个 selection 更改一组动态组合框。但是,我希望他们为每个可能的结果创建一个自定义的数字字符串。当我想要的是让每个人都添加 1s、10s、100s 和 1000s 的值。
(例如 selection 每个组合框中的第一个选项将在文本框 2 中显示 1111,或者如果它们 select combobox1 中的第二个选项,那么其余的第一个选项将显示 1112)这是下面的代码:
本质上,我只想添加基于 textbox2 中每个组合框的 selection 创建的值并显示它们。
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)
{
}
通常,这些项目会由一些外部来源填充,例如数据库。但是,由于您对每个下拉列表中的值进行硬编码,我建议您像这样填充文本框:
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();
}
}
}
此外,使用字典而不是许多 if 语句可以大大简化您的代码。
我正在尝试根据最后一个 selection 更改一组动态组合框。但是,我希望他们为每个可能的结果创建一个自定义的数字字符串。当我想要的是让每个人都添加 1s、10s、100s 和 1000s 的值。
(例如 selection 每个组合框中的第一个选项将在文本框 2 中显示 1111,或者如果它们 select combobox1 中的第二个选项,那么其余的第一个选项将显示 1112)这是下面的代码:
本质上,我只想添加基于 textbox2 中每个组合框的 selection 创建的值并显示它们。
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)
{
}
通常,这些项目会由一些外部来源填充,例如数据库。但是,由于您对每个下拉列表中的值进行硬编码,我建议您像这样填充文本框:
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();
}
}
}
此外,使用字典而不是许多 if 语句可以大大简化您的代码。