如何从组合框中分配INT以添加它们并在textbox1中显示结果 [英] How do I assign an INT from comboboxes to add them and display result in textbox1

查看:134
本文介绍了如何从组合框中分配INT以添加它们并在textbox1中显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

(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:

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

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)
    {

    }


推荐答案

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

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();
    }
  }
}

此外,使用字典代替许多if语句大大简化了你的代码。

Additionally, using a dictionary instead of many if statements drastically simplifies your code.

这篇关于如何从组合框中分配INT以添加它们并在textbox1中显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆