【问题标题】:Populating a Combo Box based on selection from Combo Box? [duplicate]根据从组合框中的选择填充组合框? [复制]
【发布时间】:2016-11-15 17:59:09
【问题描述】:

我正在构建一个窗口窗体应用程序。我想根据用户从第一个组合框中选择的内容来填充组合框。我所有的记录都存储在一个数据库表中。

【问题讨论】:

  • 你的问题是?
  • 您需要一个在框一上的事件处理程序,该事件处理程序在其选定项目更改时被调用。使用它来填充第二个组合

标签: c# winforms


【解决方案1】:

就像 pm100 建议的那样,您需要注册到第一个组合框的 SelectedIndexChanged 事件,当它被触发时,您将根据第一个组合框 SelectedTextSelectedItem 检索第二个组合框的值属性。

例如,假设您将在表单加载事件中注册该事件:

cbx1.SelectedIndexChanged += Cbx1_SelectedIndexChanged;

那么当事件被触发时:

private void Cbx1_SelectedIndexChanged(object sender, EventArgs e)
{
     cbx2.Items.Clear(); // Clear to add new retreived items

     if (cbx1.SelectedIndex != -1)
     {
         // Retrieve the items based on cbx1's selected item
         var items = Repository.RetreiveItems(cbx1.SelectedText);
         cbx2.Items.AddRange(items);
     }
}

基本上就是这样。 您可能需要考虑异步检索项目,以便 UI 空闲。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2012-09-27
    • 1970-01-01
    相关资源
    最近更新 更多