1st let me apologize for creating new thread but using Windows Mail and old
one is gone all read.
What I was asking if there was a "generic" method of capturing control
events and doing the equivalent of removing the handler while some code is
executed and then restoring it when the code is finished.
The best example would be a simple app with a navigation area and a details
area. I have in the navigation two listboxes. The first is high level and
the second a more detailed view of the first selection. When the second
listbox selection is changed then I can change the details area. If I
change the selection of the first (upper listbox) I don't want to change the
selection of the second listbox so I would like to do this generally rather
than set a boolean. The listboxes are bound so there is no way to stop the
selection changed from happening.
Finally I found (thanks to BobPowell.net for the start) a routine to
GetEventSubscri bers. I have "linq"ed his code since I am using 3.5. See
below. I added a SetEventSubscri bers which restores the event handler.
CODE:
Public Function GetEventSubscri bers(ByVal target As Object, ByVal eventName
As String) As [Delegate]()
Dim t As Type = target.GetType
Dim fia As FieldInfo() = t.GetFields((Bi ndingFlags.NonP ublic Or
(BindingFlags.S tatic Or BindingFlags.In stance)))
Dim q() As FieldInfo = (From fif In fia Where fif.Name.Contai ns(eventName)
Select fif).ToArray
If q.Count 0 Then
Dim fi As FieldInfo
Dim d As [Delegate]
fi = q(0)
d = TryCast(fi.GetV alue(target), [Delegate])
If (Not d Is Nothing) Then
Return d.GetInvocation List
End If
End If
Return New [Delegate](0 - 1) {}
End Function
Public Sub SetEventSubscri bers(ByVal target As Object, ByVal eventName As
String, ByVal del As [Delegate])
Dim WinFormsEventNa me As String = ("Event" & eventName)
Dim t As Type = target.GetType
' Do
Dim fia As FieldInfo() = t.GetFields((Bi ndingFlags.NonP ublic Or
(BindingFlags.S tatic Or BindingFlags.In stance)))
Dim q() As FieldInfo = (From fif In fia Where fif.Name.Contai ns(eventName)
Select fif).ToArray
If q.Count 0 Then
Dim fi As FieldInfo = q(0)
fi.SetValue(tar get, del)
End If
End Sub
To get the delegate for the SelectedIndexCh anged event would be:
dim dels as [delegate]() = dels = GetEventSubscri bers(listboxcon trol,
"SelectedIndexC hanged")
To simulate the RemoveHandler:
SetEventSubscri bers(listboxcon trol, "SelectedIndexC hanged", Nothing)
To simulate the AddHandler:
SetEventSubscri bers(listboxcon trol, "SelectedIndexC hanged", dels(0))
By the way I know what I show is a partial implementation since I am only
dealing with one event handler on the set but I do have the full list.
Thanks to Bob Powell and those who responded.
Lloyd Sheen
one is gone all read.
What I was asking if there was a "generic" method of capturing control
events and doing the equivalent of removing the handler while some code is
executed and then restoring it when the code is finished.
The best example would be a simple app with a navigation area and a details
area. I have in the navigation two listboxes. The first is high level and
the second a more detailed view of the first selection. When the second
listbox selection is changed then I can change the details area. If I
change the selection of the first (upper listbox) I don't want to change the
selection of the second listbox so I would like to do this generally rather
than set a boolean. The listboxes are bound so there is no way to stop the
selection changed from happening.
Finally I found (thanks to BobPowell.net for the start) a routine to
GetEventSubscri bers. I have "linq"ed his code since I am using 3.5. See
below. I added a SetEventSubscri bers which restores the event handler.
CODE:
Public Function GetEventSubscri bers(ByVal target As Object, ByVal eventName
As String) As [Delegate]()
Dim t As Type = target.GetType
Dim fia As FieldInfo() = t.GetFields((Bi ndingFlags.NonP ublic Or
(BindingFlags.S tatic Or BindingFlags.In stance)))
Dim q() As FieldInfo = (From fif In fia Where fif.Name.Contai ns(eventName)
Select fif).ToArray
If q.Count 0 Then
Dim fi As FieldInfo
Dim d As [Delegate]
fi = q(0)
d = TryCast(fi.GetV alue(target), [Delegate])
If (Not d Is Nothing) Then
Return d.GetInvocation List
End If
End If
Return New [Delegate](0 - 1) {}
End Function
Public Sub SetEventSubscri bers(ByVal target As Object, ByVal eventName As
String, ByVal del As [Delegate])
Dim WinFormsEventNa me As String = ("Event" & eventName)
Dim t As Type = target.GetType
' Do
Dim fia As FieldInfo() = t.GetFields((Bi ndingFlags.NonP ublic Or
(BindingFlags.S tatic Or BindingFlags.In stance)))
Dim q() As FieldInfo = (From fif In fia Where fif.Name.Contai ns(eventName)
Select fif).ToArray
If q.Count 0 Then
Dim fi As FieldInfo = q(0)
fi.SetValue(tar get, del)
End If
End Sub
To get the delegate for the SelectedIndexCh anged event would be:
dim dels as [delegate]() = dels = GetEventSubscri bers(listboxcon trol,
"SelectedIndexC hanged")
To simulate the RemoveHandler:
SetEventSubscri bers(listboxcon trol, "SelectedIndexC hanged", Nothing)
To simulate the AddHandler:
SetEventSubscri bers(listboxcon trol, "SelectedIndexC hanged", dels(0))
By the way I know what I show is a partial implementation since I am only
dealing with one event handler on the set but I do have the full list.
Thanks to Bob Powell and those who responded.
Lloyd Sheen