【问题标题】:How to update the values in a combobox triggered by another combobox?如何更新由另一个组合框触发的组合框中的值?
【发布时间】:2012-12-19 14:00:46
【问题描述】:

我在一个表单中有 2 个组合框。

我希望在combobox2 中的列表更新时更改combobox1 中的选定值。

例如:ComboBox1 包含移动公司的名称,ComboBox2 包含该公司所有手机的列表。

【问题讨论】:

标签: c# winforms combobox


【解决方案1】:

假设您有一本将手机型号与其制造商相关联的字典:

Dictionary<string, string[]> brandsAndModels = new Dictionary<string, string[]>();

public void Form_Load(object sender, EventArgs e)
{
    brandsAndModels["Samsung"] = new string[] { "Galaxy S", "Galaxy SII", "Galaxy SIII" };
    brandsAndModels["HTC"] = new string[] { "Hero", "Desire HD" };
}

你可以得到要在左侧组合框中显示的项目:

foreach (string brand in brandsAndModels.Keys)
    comboBox1.Items.Add(brand);

您只需执行一次此操作,例如在表单的 Load 事件中。注意:brandsAndModels 字典必须是实例变量,而不是局部变量,因为我们稍后需要访问它。

然后,您必须为 SelectedIndexChanged 事件分配一个事件处理程序,在其中您将第二个组合框中的项目替换为所选品牌的数组中的项目:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    comboBox2.Items.Clear();

    if (comboBox1.SelectedIndex > -1)
    {
        string brand = brandsAndModels.Keys.ElementAt(comboBox1.SelectedIndex);
        comboBox2.Items.AddRange(brandsAndModels[brand]);
    }
}

如果所有这些都来自数据库,那么使用数据绑定会更好,如我在评论中链接到您的问题的问题的答案中所述。

【讨论】:

  • 是的,先生。评论问题很好,但给出的答案不适合初学者,我不知道绑定概念和表格,它很复杂,你能为初学者简化吗?如果可以的话,请
  • 我在这个答案中做了 - 这个答案不使用绑定。
  • 变量仅在声明它们的代码块或当前块包含的任何块中有效。局部变量是在函数中声明的变量。之所以称为local,是因为它只在这个函数内有效,不能在其他函数中使用。实例变量(也称为“成员变量”)在类级别(在任何函数之外,但在类的代码内部)声明,并且在该类的所有函数(也称为方法)中有效。
  • 您不能将数组 (st2) 分配 到组合框 Items 集合中。但是,您可以使用AddRange 方法。我将更新我的代码,因为这实际上是一种简化。
  • 另外:请不要像现在这样开始硬编码 - 对于您想要实现的目标,这不是好习惯!我的回答中的方法是最短和最简单的,同时仍然是最灵活的。例如:如果另一个要求是对comboBox1 中的项目进行排序怎么办?然后你需要在中间插入另一个品牌吗?您想每次都更新if 语句吗?例如:假设您现在有 HTCSamsung。明天你需要插入 LG。使用我的方法没问题 - 使用你的方法做了大量工作,因为你需要更新所有 ifs。
【解决方案2】:

您必须处理组合框的SelectedIndexChanged 事件才能实现该目标

【讨论】:

  • 你能多介绍一些我怎么能做到这一点?我是初学者,很抱歉英语不好
  • @shariq_khan 选择所需的组合框并在“属性”窗口中切换到事件并双击 SelectedIndexChanged
  • 我知道但是要写什么?我不知道逻辑部分如何做到这一点
【解决方案3】:

当您看起来很新时,我将逐步向您解释。

  1. 右键单击 ComboBox1 并选择属性。
  2. 这将打开属性面板。
  3. 从属性面板顶部选择事件按钮。
  4. 将显示与组合框相关的所有事件列表。
  5. 从此列表中双击 SelectedIndexChanged。
  6. 将创建私有 void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {}

你可以在此之后使用以下代码。

Dictionary<string, string[]> models = new Dictionary<string, string[]>();
public Form1()
{
    InitializeComponent();

    //initializing combobox1
    comboBox1.Items.Add("Select Company");
    comboBox1.Items.Add("HTC");
    comboBox1.Items.Add("Nokia");
    comboBox1.Items.Add("Sony");

    //select the selected index of combobox1
    comboBox1.SelectedIndex = 0;

    //initializing model list for each brand
    models["Sony"] = new string[] { "Xperia S", "Xperia U", "Xperia P" };
    models["HTC"] = new string[] { "WildFire", "Desire HD" };
    models["Nokia"] = new string[] { "N97", "N97 Mini" };
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    comboBox2.Items.Clear();

    if (comboBox1.SelectedIndex > -1)
    {
        string brand = comboBox1.SelectedItem.ToString();
        if(brand != "" && comboBox1.SelectedIndex > 0)
            foreach (string model in models[brand])
                comboBox2.Items.Add(model);
    }
}

【讨论】:

  • 先生,步骤中的 > -1 是什么? comboBox1.SelectedIndex > -1
  • 先生这个错误正在出现在当前上下文中不存在名称'models' foreach(模型[品牌]中的字符串模型)
  • 你声明过这个Dictionary&lt;string, string[]&gt; models = new Dictionary&lt;string, string[]&gt;();吗?
  • > -1 表示如果comboxBox 的SelectedIndex 大于-1 表示它是0 = Select Company1 = HTC2 = Nokia3 = Sony
  • 您还可以将BreakpointVisual Studios 一起使用,看看代码是如何工作的以及即将到来的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
相关资源
最近更新 更多