如何从组合框中分配INT以添加它们并在textbox1中显示结果

时间:2015-02-25 22:10:31

标签: c# combobox int var

我正在尝试使用一组动态组合框,这些组合框会根据最后的选择进行更改。但是,我希望他们为每个可能的结果创建一个自定义数字字符串。我希望每个人都添加1s,10s,100s和1000s的值。

(例如选择每个组合框中的第一个选项将在textbox2中显示1111,或者如果他们在combobox1中选择第二个选项,那么第一个选项将显示为1112)这里的代码如下:

基本上我只想添加基于textbox2中每个组合框的选择而创建的值并显示它们。

    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 个答案:

答案 0 :(得分:0)

通常,这些项目将由某些外部源(如数据库)填充。但是,由于您对每个下拉列表中的值进行了硬编码,我建议您填充文本框,如下所示:

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语句可以大大简化您的代码。