0

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:

  1. figure out the event handler for the ComboBox it was just asked to load
  2. save the event handler
  3. load the ComboBox
  4. 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.

Community
  • 1
  • 1
gcook17
  • 1
  • 3
  • What about leaving the bindings as is, and refreshing the dataset rather than changing the event handlers? – Rots Apr 21 '14 at 20:16
  • If what you're really interested in is selection changes initiated by the user via the UI and not changes made in code then simply handle the SelectionChangeCommitted event instead. That's not an option if you want to react to some changes made in code but not all. – jmcilhinney Apr 22 '14 at 05:53
  • The problem I'm trying to solve is that every time the ComboBox is being loaded, when it hits the statement cbo.ValueMember = "Key" the SelectedValueChanged event fires, so for every ComboBox in the system I have to code their event handlers in such a way to not do anything when the .ValueMember property is set. – gcook17 Apr 23 '14 at 14:24
  • Try again: The problem I'm trying to solve is that every time the ComboBox is being loaded, when it hits the statement cbo.ValueMember = "Key" the SelectedValueChanged event fires, so for every ComboBox in the system I have to code their event handlers in such a way to not do anything when the .ValueMember property is set. Rather than have code to do this in hundreds of places, I'd like the functions that load the ComboBoxes to somehow make the event handlers not to be called. I thought that somehow disabling the SelectedValueChanged event while the loading was taking place would work. – gcook17 Apr 23 '14 at 14:30
  • jmcilhinney - I misunderstood what you were saying, but now I see. The SelectionChangeCommitted event handler seems to work fine. Thanks a lot. – gcook17 Apr 23 '14 at 15:00
  • Sometimes, actually pretty often, we set the SelectedValue programmatically and expect the SelectedValueChanged event handler to do something about it. So, It would still be really helpful if I could find a way to disable the event handler while reloading the ComboBoxes. Since just a few functions load all the ComboBoxes, they don't know the names of the actual event handlers so they can't remove them and then add them back. – gcook17 Apr 23 '14 at 15:17

0 Answers0