【问题标题】:How do I assign an INT from comboboxes to add them and display result in textbox1如何从组合框中分配一个 INT 以添加它们并在 textbox1 中显示结果
【发布时间】:2015-02-25 22:10:31
【问题描述】:

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

(例如,选择每个组合框中的第一个选项将在 textbox2 中显示 1111,或者如果他们在组合框 1 中选择第二个选项,则其余选项中的第一个选项将显示 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)
    {

    }

【问题讨论】:

  • 我不确定我是否完全理解这个问题。上面的代码有错误吗?如果是这样呢?如果没有,哪个部分没有按预期工作?
  • 我没有收到错误是的。但是,如果我添加 else if (comboBox1.SelectedIndex == 0) { var sub1 = 1;我得到的东西告诉我 sub1 在上下文中不可用
  • 如果您在 if 语句中声明 sub1,则其范围仅在 if 语句中可用。不能在大括号外使用。
  • 所以我是否只创建一个超长的 if 语句,它首先确定显示的字符串(它现在这样做),然后在相同的规则下创建第二个 if 语句,将值添加到 textbox2?如果是这样,那会是什么样子?
  • 为什么你有 3 次 if (comboBox1.SelectedIndex == 0) 做同样的事情?

标签: c# combobox int var


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-09
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2021-09-30
    • 2020-06-03
    相关资源
    最近更新 更多