-
Jan 14th, 2015, 01:53 PM
#1
Thread Starter
Junior Member
Event handling problem
i need to Invoke an event from one of my class .what i am trying to do is for each RaiseEvent the listbox should add an item .
here is the code.
PHP Code:
Public Class Example
Public Event StatusLog(ByVal actionName As String)
Sub test()
RaiseEvent StatusLog("TEST1.")
RaiseEvent StatusLog("TEST2.")
RaiseEvent StatusLog("TEST3.")
End Sub
End Class
PHP Code:
Public Class Form1
Private T As New Example
Private WithEvents Log As Example
Private Sub Log_StatusUpdate(ByVal actionName As String) Handles Log.StatusLog
UPdatelog(actionName)
End Sub
Delegate Sub _UPdatelog(ByVal actionName As String)
Private Sub UPdatelog(ByVal actionName As String)
If InvokeRequired Then
Invoke(New _UPdatelog(AddressOf UPdatelog), actionName)
Else
ListBox1.Items.Add(actionName)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
T.test()
End Sub
-
Jan 14th, 2015, 02:24 PM
#2
Hyperactive Member
Re: Event handling problem
Why are you doing this using events not subs or functions ? And what isn't working /do you need help with?
-
Jan 14th, 2015, 03:27 PM
#3
Re: Event handling problem
1) I don't think the above code is live code, but an illustration of the problem - besides, how else should should a class report back? No one questions when the Changed event happens in a text box, but add a ReportStatus event to a class and everyone thinks you're bonkers.
2) I don't see a question, so I'm not sure what the problem is.
3) I don't see where Test is called, so the events will NEVER get raised. In fact you don't create an instance except for T at the top, but it never gets used either.
-tg
-
Jan 14th, 2015, 03:30 PM
#4
Hyperactive Member
Re: Event handling problem
Originally Posted by techgnome
1) I don't think the above code is live code, but an illustration of the problem - besides, how else should should a class report back? No one questions when the Changed event happens in a text box, but add a ReportStatus event to a class and everyone thinks you're bonkers.
that's a fair point I read the thread too fast and on a iphone and missed that it was a separate class
-
Jan 14th, 2015, 03:33 PM
#5
Re: Event handling problem
-
Jan 14th, 2015, 09:46 PM
#6
Thread Starter
Junior Member
Re: Event handling problem
what i am trying to do this is when ever i RaiseEvent StatusLog in a sub or function it should Pass that Log and update the listbox like in the example Test1, test2,test3 .
-
Jan 15th, 2015, 08:45 AM
#7
Re: Event handling problem
But you never call the method "Test" ... so since you never call it, it doesn't raise the event...
-tg
-
Jan 15th, 2015, 09:49 AM
#8
Re: Event handling problem
You've got two fields typed as Example in your Form, T which you assign a new instance of Example to in its initialiser, and Log. You call the Test() method on T, but the Log_UpdateStatus event handler method is subscribed to the StatusLog event from the Log field.
What you want is to have only the one field:
vbnet Code:
Public Class Form1 Private WithEvents Log As New Example Private Sub Log_StatusUpdate(ByVal actionName As String) Handles Log.StatusLog UPdatelog(actionName) End Sub ' ... Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Log.test() End Sub
Now you should be okay, because the instance that is having its test() method called is the same instance that the event handler method is listening to.
-
Jan 15th, 2015, 10:22 AM
#9
Thread Starter
Junior Member
Re: Event handling problem
thanks M8 , now my problem is solved .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|