jason2li
Member
Hi, I've been developing for years, and have been using my own events just as long, but this one has me stumped. I could have sworn this used to work.
(I'm using VS2005)
This is a simple example class to demonstrate. I instantiate the class, and run a loop from the constructor, which adds several items to the collection. Once something is added, it calls the RaiseEvent. This is not handled, and I don't know why. I'm assuming it has to do with the fact that the class object doesn't technically exist on our main form yet (since we're in the middle of constructing it), but I thought I've done this before.
Anyway, once the constructor is complete, I add another item from the main form by calling the "Add" procedure. This one is handled correctly.
So the only thing that is written in the console window is
Is there any way to make this work? I know I can just not do the work from within the constructor, and everything will work fine. But I'm curious if there is a workaround besides that.
(I'm using VS2005)
VB.NET:
Public Class Form1
Private WithEvents Test As TestClass
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Test = New TestClass
Test.Add(100)
End Sub
Private Sub Test_Added(ByVal Index As Integer) Handles Test.Added
Console.WriteLine(Index)
End Sub
End Class
Public Class TestClass
Inherits CollectionBase
Public Event Added(ByVal Index As Integer)
Public Sub New()
For i As Integer = 0 To 99
Add(i)
Next
End Sub
Public Sub Add(ByVal Index As Integer)
RaiseEvent Added(Index)
End Sub
End Class
This is a simple example class to demonstrate. I instantiate the class, and run a loop from the constructor, which adds several items to the collection. Once something is added, it calls the RaiseEvent. This is not handled, and I don't know why. I'm assuming it has to do with the fact that the class object doesn't technically exist on our main form yet (since we're in the middle of constructing it), but I thought I've done this before.
Anyway, once the constructor is complete, I add another item from the main form by calling the "Add" procedure. This one is handled correctly.
So the only thing that is written in the console window is
VB.NET:
100
Is there any way to make this work? I know I can just not do the work from within the constructor, and everything will work fine. But I'm curious if there is a workaround besides that.