There have been a number of posts related to this but I haven't seen any with the solution I'm looking for.
When loading a ComboBox the SelectedValueChanged event fires when you set the ValueMember. I have a method the loads almost all the ComboBoxes in the application and often they get reloaded based on things the users do. Thus, it isn't practical to AddHandler only after loading.
What I'd like to do is have the ComboBox loader method:
- figure out the event handler for the ComboBox it was just asked to load
- save the event handler
- load the ComboBox
- then restore the event handler to its previous value.
Something like this:
Public Sub LoadComboBox(ByVal cbo As ComboBox)
'Save Event Handler
Dim eHandler As [Delegate] = Something(cbo)
RemoveHandler cbo.SelectedValueChanged, eHandler
'Load ComboBox
'...
cbo.ValueMember = "Key"
cbo.DisplayMember = "Value"
'Restore Event Handler
AddHandler cbo.SelectedValueChanged, eHandler
End Sub
I've looked at one suggestion for retrieving an event handler at How to Attach the Events of an Original Object to a Deep Copied Clone but it didn't work for me as the GetField method always returned Nothing.
I am currently using Booleans everywhere to control whether the event handler code does anything but this is messy and error-prone.