
Signature
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
> ... except if event handlers are added inside the constructor using
> 'AddHandler', or an object is assigned to a 'WithEvents' variable. So,
> events can be raised, but in most scenarios event handlers are not yet
> connected.
Herfried,
Here is a really stripped down version of the code that tripped me up
before. Notice myClass1 "is" defined WithEvents, but the event doesn't get
raised during the constructor, only when I raise it from a method call.
Probably showing my stupidity here, but it was your post that helped work
around this problem before. Maybe you could shed some light on the issue
with this (contrived) code.
Thanks,
Greg
Public Class Form1
Private WithEvents myClass1 As Class1
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
myClass1 = New Class1 ' does NOT call MyEventHandler
myClass1.FooBar() ' does call MyEventHandler
End Sub
Private Sub MyEventHandler(ByVal o As Object, ByVal e As EventArgs)
Handles myClass1.SomeEvent
'do something
End Sub
End Class
Public Class Class1
Event SomeEvent(ByVal o As Object, ByVal e As EventArgs)
Public Sub New()
RaiseEvent SomeEvent(Me, New EventArgs)
End Sub
Public Sub FooBar()
RaiseEvent SomeEvent(Me, New EventArgs)
End Sub
End Class
Herfried K. Wagner [MVP] - 13 Dec 2005 21:24 GMT
"Greg Burns" <bluebunny@newsgroups.nospam> schrieb:
>> ... except if event handlers are added inside the constructor using
>> 'AddHandler', or an object is assigned to a 'WithEvents' variable. So,
[quoted text clipped - 24 lines]
>
> myClass1 = New Class1 ' does NOT call MyEventHandler
Yep. At this time the event handler ('WithEvents' /outside/ the class)
haven't been added yet.

Signature
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Armin Zingler - 13 Dec 2005 21:39 GMT
"Greg Burns" <bluebunny@newsgroups.nospam> schrieb
Public Class Class1
Event SomeEvent(ByVal o As Object, ByVal e As EventArgs)
Public Sub New(ByVal Handler As SomeEventEventHandler)
Addhandler SomeEvent, Handler
RaiseEvent SomeEvent(Me, New EventArgs)
End Sub
Public Sub FooBar()
RaiseEvent SomeEvent(Me, New EventArgs)
End Sub
End Class
dim c as class1
c = new class1(addressof OnEvent)
sub onevent(...)
end sub
Now you raise an event in the constructor.
Armin