简体   繁体   中英

Change another Combobox Item list when selecting from another Combobox

I have 3 string arrays as a Data source for my Combobox.

        public string[] monitoringDays =
                         new[] { "Sunday", "Tuesday", "Wednesday",
                        "Thursday", "Friday", "Saturday",
                        "Sunday" };
        public string[] specialtemplates = new[] {
            "GENSET PAD & CANOPY CONSTR",
            "ATS INSTALLATION NSA",
            "GENSET CANOPY PAT",
            "TWR CON SKOM AND MOB",
            "SITE SURVEY ACCESS PASS"
        };

and

        public string[] templates = new[] {
            "IMPLEMENTATION",
            "SKOM ACESS PASS",
            "LOSR SURVEY ACCESS PASS",
            "SOIL TEST ACCESS PASS",
            "IMPLEM ACU INSTALLATION",
            "SOIL TEST",
            "Site Survey AC Upgrade NSA",
            "AC UPGRADE IMPLEM SA",
            "AC UPGRADE EXT SA",
            "GENSET PAD & CANOPY CONSTR",
            "ATS INSTALLATION NSA",
            "GENSET CANOPY PAT",
            "TWR CON SKOM AND MOB",
            "SITE SURVEY ACCESS PASS"
        };

Each of these string arrays are data sources for Combobox A and B.

What should I do to make Combobox B's Item list to change when selecting specific Combobox A's Items (eg when I select Monday the Combobox Items would change to specialtemplates )

I have tried

            if (cbMonitor.SelectedIndex == 1) {
                cboxTemplate.DataSource = specialtemplates;
            }

but it doesn't work. (It's totally wrong)

Cheers!

You need to call Refresh method so that it updates the combo box data and reflects on the UI as well:

cboxTemplate.DataSource = specialtemplates;
cboxTemplate.Refresh(); // call this method

Missing Monday in your monitoringDays...

You need to create a SelectedValueChanged event handler on cbMonitor with this code behind:

  private void cbMonitor_SelectedValueChanged(object sender, EventArgs e) { if (cbMonitor.SelectedIndex == 1) cboxTemplate.DataSource = specialtemplates; } 

That works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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