vb.net - Removing and adding event handlers for different ComboBoxes where the ComboBox is passed as a parameter -
i have method takes combobox parameter , adds data it. when data added, selectedindexchangedevent fires. there way that, in called method, can remove above event handler whatever combobox passed parameter , add @ end of method? know how remove , add specific handlers, can't figure out how based on combobox passed.
here's method..
private sub populatecombobox(byref cbobox combobox, byval itemsource string) 'remove handler cbobox 'do stuff otherwise cause event handler execute 'add handler cbobox end sub
i have 4 comboboxes - easier remove 4 event handlers , add them again @ end of code? know if possible can possibly apply re-usable code in future
the basic way go this:
private sub populatecombobox(byref cbobox combobox, byval itemsource string) removehandler cbobox.selectedindexchanged, addressof combobox1_selectedindexchanged 'do stuff otherwise cause event handler execute addhandler cbobox.selectedindexchanged, addressof combobox1_selectedindexchanged end sub private sub combobox1_selectedindexchanged(sender object, e eventargs) handles combobox1.selectedindexchanged end sub
another option, might better in circumstances, this:
private _ignorecombobox combobox = nothing private sub populatecombobox(byref cbobox combobox, byval itemsource string) _ignorecombobox = cbobox 'do stuff otherwise cause event handler execute _ignorecombobox = nothing end sub private sub combobox1_selectedindexchanged(sender object, e eventargs) handles combobox1.selectedindexchanged if sender not _ignorecombobox end if end sub
or, handle multiple combo boxes @ same time:
private _ignorecomboboxes list(of combobox) = new list(of combobox)() private sub populatecombobox(byref cbobox combobox, byval itemsource string) _ignorecomboboxes.add(cbobox) 'do stuff otherwise cause event handler execute _ignorecomboboxes.remove(cbobox) end sub private sub combobox1_selectedindexchanged(sender object, e eventargs) handles combobox1.selectedindexchanged if not _ignorecomboboxes.contains(directcast(sender, combobox)) end if end sub
Comments
Post a Comment