|
-
Sep 4th, 2006, 10:34 PM
#1
Thread Starter
Fanatic Member
[2005] Raise Event
I have been searching and reading for a while now but cannot get my head around events. What I want to do is if some class function returns true I want to raise an event from my main form. I want to raise the same mouse click event as if they clicked on the notify icon.
Do I need to use a delagate?
I am problem doing this all wrong but here is what I have tried.
VB Code:
Public Class EventTest
' Declare the event
Private Event NewProductEvent As MouseEventHandler
Public Sub Product()
Dim IsNewProduct As Boolean = CheckProduct()
If IsNewProduct Then
OnNewProduct(New MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0))
End If
End Sub
Protected Overridable Sub OnNewProduct(ByVal e As MouseEventArgs)
' This needs to somehow perform the Main.NotifyIcon notifyIcon_MouseClick event.
RaiseEvent NewProductEvent(Me, e)
End Sub
End Class
-
Sep 4th, 2006, 10:40 PM
#2
Hyperactive Member
Re: [2005] Raise Event
Well first, your function isn't actually handling anything yet, so it won't do anything. If you wanted to handle Button1.Click, you'd add "handles Button1.Click" to the end of the function prototype (eg private sub myfunc(...) handles *). To handle those events, you'll also need a "sender as object" as a parameter if it is going to handle a mouseclick.
The important thing here, though, is that you can have one function handle more than one event. eg
VB Code:
Private Sub MyGenericMouseFunction(byval sender as object, byval e as MouseEventArgs) _
handles button1.click, button2.click, notifyicon.click
'... do stuff, you can see what triggered the function using the sender object
End Sub
EDIT: I re-read you code, and I'm not sure if you'll need a delegate... you probably will if I read it correctly. With that being the case, I'll reuse my previous code to show this:
VB Code:
Private Sub MyGenericMouseFunction(byval sender as object, byval e as MouseEventArgs) _
handles button1.click, button2.click, notifyicon.click
MyDelegateFunction()
End Sub
Private Sub MyDelegateFunction()
'... Do stuff
End Sub
-
Sep 4th, 2006, 11:19 PM
#3
Re: [2005] Raise Event
Here's how events work. You declare an event in your class as a delegate type. In your case you're delegate type is MouseEventHandler. When instances of your class are used in code, the Handles and AddHandler key words add instances of that delegate type containing references to methods to your event. When you call RaiseEvent in your type you are invoking each of those delegates, which then proceed to execute the methods they have references to. The Handles, AddHandler and RaiseEvent key words are things that VB does to hide some complexity from you to make things easier. Unfortunately, as is often the case, by hiding things from you VB reduces your understanding of what is happening. For instance, this VB code:
VB Code:
AddHandler myObject.SomeEvent, AddressOf SomeMethod
registers the SomeMethod method as a handler for the SomeEvent event of the object referred to by the myObject variable. That's all well and good, but what does that really mean? In C# the equivalent code would be:
Code:
myObject.SomeEvent += new EventHandler(SomeMethod);
This makes it a bit plainer what's going on. You are creating an instance of the delegate type that matches your event and passing it a reference to the method that you want executed when the event is raised. If you understand delegates then this is quite clear.
-
Sep 5th, 2006, 01:25 AM
#4
Hyperactive Member
Re: [2005] Raise Event
I think I used delegate in the wrong sense in my earlier post, so if it doesn't make sense, replace it with "another function" when reading my post.
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
|