What Determines AddHandler Action

RogerSchlueter-7899 1,736 Reputation points
2026-06-29T14:45:42.08+00:00

I have the following in my Window.Loaded event:


Dim cvsPeople As CollectionViewSource = CType(Resources("cvsPeople"), CollectionViewSource)
cvsPeople.Source = ocPeople
....
ComboTextBox = DirectCast(cbxPeople.Template.FindName("PART_EditableTextBox", cbxPeople), TextBox)
AddHandler ComboTextBox.TextChanged, AddressOf EnterSearchText
AddHandler cvsPeople.Filter, AddressOf RePopulateSource
End Sub

Nothing happens when the first line executes while the second line causes execution to immediately jump to the RePopulateSource subroutine. Why? And, of course, how can prevent this behavior?

Developer technologies | VB

2 answers

Sort by: Most helpful
  1. Nancy Vo (WICLOUD CORPORATION) 6,595 Reputation points Microsoft External Staff Moderator
    2026-06-30T06:53:12.8433333+00:00

    Hello @RogerSchlueter-7899 ,

    Thanks for your question.

    When you use AddHandler for ComboTextBox.TextChanged, the program just sets up a listener. Since no one is typing in the text box at that exact moment, it waits quietly.

    However, the cvsPeople.Filter event works differently because of the line of code you ran just above it: cvsPeople.Source = ocPeople.

    Because your CollectionViewSource already has a list of data assigned to it, the moment you attach the filter handler, the system realizes, "I already have data, and I just received a new filtering rule. I need to apply this filter right now so the list displays correctly." Attaching a filter to a list that already contains data forces an immediate refresh.

    You can refer to following steps:

    1. Create a flag at the top of your Window class:
    Private isInitializing As Boolean = True
    
    1. Turn the flag off at the very end of your Window.Loaded event:

    Leave your AddHandler lines exactly as they are, but add one line at the very end to signal that the setup is finished.

    ' ... existing code ...
    ComboTextBox = DirectCast(cbxPeople.Template.FindName("PART_EditableTextBox", cbxPeople), TextBox)
    AddHandler ComboTextBox.TextChanged, AddressOf EnterSearchText
    AddHandler cvsPeople.Filter, AddressOf RePopulateSource
    
    isInitializing = False
    End Sub
    
    1. Check the flag inside your RePopulateSource subroutine:
    Private Sub RePopulateSource(sender As Object, e As FilterEventArgs)
        If isInitializing Then Exit Sub
    
        ' ... your normal filtering code goes here ...
    End Sub
    

    I hope the above information is helpful. If it addressed your concern, please consider using the following guidance to share your feedback.

    Was this answer helpful?


  2. AI answer

    2026-06-29T14:45:54.8666667+00:00

    AddHandler itself never causes the handler to run; it only associates the handler with the event. The handler runs when the event is raised.

    In the code:

    AddHandler ComboTextBox.TextChanged, AddressOf EnterSearchText
    AddHandler cvsPeople.Filter, AddressOf RePopulateSource
    

    these two events behave differently:

    1. TextBox.TextChanged
      • This is a standard CLR/WPF event.
      • AddHandler ComboTextBox.TextChanged, ... only wires the handler.
      • EnterSearchText will run later, when the user changes the text or when code assigns a different Text value.
      • So “nothing happens” at the moment AddHandler executes because TextChanged is not raised at that time.
    2. CollectionViewSource.Filter
      • This is an event that is raised whenever the view needs to (re)apply its filter.
      • As soon as a handler is attached with:
             AddHandler listingDataView.Filter, AddressOf ShowOnlyBargainsFilter
        
        (same pattern as your cvsPeople.Filter line), the view may immediately re-evaluate its contents and raise Filter.
      • That is why execution “immediately jumps” to RePopulateSource: the CollectionViewSource raises the Filter event right after the handler is attached, so your handler is invoked.

    This is expected behavior for CollectionViewSource.Filter: attaching the handler can trigger an immediate filter pass.

    To prevent the handler from running at that moment, typical approaches are:

    • Guard the handler with a flag so it does nothing during initialization:
        Private _isInitializing As Boolean = True
        
        Private Sub Window_Loaded(...) Handles Me.Loaded
            Dim cvsPeople As CollectionViewSource = CType(Resources("cvsPeople"), CollectionViewSource)
            cvsPeople.Source = ocPeople
        
            ComboTextBox = DirectCast(cbxPeople.Template.FindName("PART_EditableTextBox", cbxPeople), TextBox)
            AddHandler ComboTextBox.TextChanged, AddressOf EnterSearchText
            AddHandler cvsPeople.Filter, AddressOf RePopulateSource
        
            _isInitializing = False
        End Sub
        
        Private Sub RePopulateSource(sender As Object, e As System.Windows.Data.FilterEventArgs)
            If _isInitializing Then
                Return
            End If
            ' Normal filtering logic here
        End Sub
      
    • Or attach the Filter handler later, after initial setup is complete, if the design allows it.

    The key point is that AddHandler only wires the handler; whether it runs immediately depends entirely on the event’s own raising behavior.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.