简体   繁体   English

当另一个下拉列表中的选定项目更改时,需要更改下拉列表中的选定项目

[英]Need to change selected item in dropdown list when selected item in another dropdown list changes

I have a windows form application with two Combo boxes (Dropdownlist style). 我有一个带有两个组合框的Windows窗体应用程序(Dropdownlist样式)。 The first combo box gets the data from a List of AppTable object, like so: 第一个组合框从AppTable对象的List获取数据,如下所示:

foreach (AppTable table in appTableList)
            cbxSelectName.Items.Add(table.App);

I have set up a trigger for when the selected item in this dropdown is changed: 我设置了一个触发器,用于更改此下拉列表中的所选项目:

this.cbxSelectName.SelectedIndexChanged +=new EventHandler(cbxSelectName_SelectedIndexChanged);

And finally, here is the definition of the method the trigger calls. 最后,这是触发器调用的方法的定义。 Please note, the value in tbxNewWikiWord textbox changes as the selected item is changed. 请注意,tbxNewWikiWord文本框中的值会随着所选项目的更改而改变。 However, the same does not happen in the second dropdown list ( cbxUpdateAppType): 但是,第二个下拉列表(cbxUpdateAppType)中不会发生相同的情况:

private void cbxSelectName_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (AppTable table in appTableList)
        {
            if (table.App == cbxSelectName.SelectedItem.ToString())
            {
                this.tbxNewWikiWord.Text = table.WikiWord;
                this.cbxUpdateAppType.SelectedItem = table.Type;
                break;
            }
        }

    }

This is how AppTable looks: 这是AppTable的外观:

class AppTable
{
    public string App { get; set; }
    public string Type { get; set; }
    public string WikiWord { get; set; }

}

Am I missing something? 我想念什么吗?

Adding values to cbxUpdateAppType from the AppTable object fixed it. 从AppTable对象向cbxUpdateAppType添加值可修复该问题。 I'm not sure why since in either case, I was adding Strings. 我不确定为什么在这两种情况下都添加了字符串。

I just needed to check for duplicates so I don't end up with multiple instances of the same value in my dropdown box. 我只需要检查重复项,这样我就不会在下拉框中遇到多个具有相同值的实例。

foreach (AppTable table in appTableList)
        {
            if (!cbxUpdateAppType.Items.Contains(table.Type))
                cbxUpdateAppType.Items.Add(table.Type);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM