An object-oriented programming language developed by Microsoft that can be used in .NET.
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:
-
TextBox.TextChanged- This is a standard CLR/WPF event.
-
AddHandler ComboTextBox.TextChanged, ...only wires the handler. -
EnterSearchTextwill run later, when the user changes the text or when code assigns a differentTextvalue. - So “nothing happens” at the moment
AddHandlerexecutes becauseTextChangedis not raised at that time.
-
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:
(same pattern as yourAddHandler listingDataView.Filter, AddressOf ShowOnlyBargainsFiltercvsPeople.Filterline), the view may immediately re-evaluate its contents and raiseFilter. - That is why execution “immediately jumps” to
RePopulateSource: theCollectionViewSourceraises theFilterevent 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
Filterhandler 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: