vb.net - Removing or disable ComboBox item which is selected in another list -


i have 3 combo boxes on winform following:

                  ___________________________ combobox list 1: |_______________________|___|                   ___________________________ combobox list 2: |_______________________|___|                   ___________________________ combobox list 3: |_______________________|___| 

each of these combo boxes have, @ design-time, list of "a", "b", "c".

by default, list 1 1 active on form , list 2 , list 3 become active when predecessor given selection.

what do, if user choose option c, have option c no longer available list 2 , 3.

i know involve .selectedindexchanged event of combobox not know started coding.

i found following answer on stackoverflow, how situation doesn't apply since i'm supplying items @ design time , not via import of file: vb.net change combobox options depending on selected item in 2 previous comboboxes

this similar steve's answer, uses datasource. also, there no real need enable/disable each cbo. make changes, previous selections filtered source. duplicate "previous" changes undone.

cbo1 has full list, while others omit previous selections. cant end duplicate picks because re-picking #2 same #3, changes contents of #3 replacing selection whatever first in list.

' master list private optlist new list(of string) private ignore boolean  ' initialize somewhere: optlist.addrange(new string() {"red", "green", "blue", "violet", "orange"}) ' set cbos ' ignore changes while setting up: ignore = true cb1.datasource = optlist.toarray cb1.selectedindex = -1 cb2.datasource = optlist.toarray cb2.selectedindex = -1 cb3.datasource = optlist.toarray cb3.selectedindex = -1 ignore = false 

with them enabled, can pick them in order.

private sub cbo_selectedindexchanged(sender object, e eventargs) _              handles cb1.selectedindexchanged, cb2.selectedindexchanged     if ignore exit sub      dim cbo = trycast(sender, combobox)     if cbo isnot nothing andalso cbo cb1         cb2.datasource = getfilteredlist(new string() {cb1.items(cb1.selectedindex)})     else         cb3.datasource = getfilteredlist(new string() {cb1.selecteditem.tostring,                                                    cb2.selecteditem.tostring})     end if end sub  private function getfilteredlist(items string()) string()     return optlist.except(items).toarray() end function 

since cbo3 limited items not picked in #1 or #2 (a slave) dont have when selection changes.


could expanded 9 cbo continuing multiple if/else statements , how keep first option. if wanted include "none" option available.

what many, more abstractly. code may harder read/follow, there less of (i using 4 cbos). may need tweaking off top of head follow up revised form:

' additional array hold cbos involved private tgtcbos combobox() ... ' initialization: optlist.addrange(new string() {"(none)", "red", "green", "blue", "violet",                         "orange", "mauve", "white"}) tgtcbos = new combobox() {cb1, cb2, cb3, cb4}  ' set initial index 0 default item  private sub cb2_selectedindexchanged(sender object, e eventargs) handles _          cb2.selectedindexchanged, cb1.selectedindexchanged, cb3.selectedindexchanged      if ignore exit sub      dim cbo = trycast(sender, combobox)     ' identify 1     dim ndx = array.indexof(tgtcbos, cbo)     ' selections 0 here     dim exclude = getexclusionlist(ndx)     ' remove excludes next cbo     dim newlist new list(of string)     if ndx + 1 < tgtcbos.length         newlist = optlist.except(exclude).tolist()         if newlist(0) <> optlist(0) newlist.insert(0, "(none)")         tgtcbos(ndx + 1).datasource = newlist.toarray()     end if end sub  private function getexclusionlist(ndx int32) list(of string)     dim exclude new list(of string)     n int32 = 0 ndx         if tgtcbos(n).selectedindex <> -1             exclude.add(tgtcbos(n).items(tgtcbos(n).selectedindex).tostring())         end if     next     return exclude end function 

note last cbo not connected handler because there has no "slave" . connect own handler if need respond event.

also, act of resetting datasource "next" (or clearing items matter) cause event fire next/child/slave cbo. change cbo2 fire event 3, 4, 5..n. resets previous selection in child/slave/next ones.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -