Results 1 to 4 of 4

Thread: [2005] Raise Event

  1. #1

    Thread Starter
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    [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:
    1. Public Class EventTest
    2. ' Declare the event
    3. Private Event NewProductEvent As MouseEventHandler
    4.  
    5. Public Sub Product()
    6.    Dim IsNewProduct As Boolean = CheckProduct()
    7.    If IsNewProduct Then
    8.             OnNewProduct(New MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0))
    9.    End If
    10. End Sub
    11.  
    12. Protected Overridable Sub OnNewProduct(ByVal e As MouseEventArgs)
    13.      ' This needs to somehow perform the Main.NotifyIcon  notifyIcon_MouseClick event.
    14.         RaiseEvent NewProductEvent(Me, e)
    15.     End Sub
    16. End Class

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  2. #2
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    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:
    1. Private Sub MyGenericMouseFunction(byval sender as object, byval e as MouseEventArgs) _
    2. handles button1.click, button2.click, notifyicon.click
    3.   '... do stuff, you can see what triggered the function using the sender object
    4. 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:
    1. Private Sub MyGenericMouseFunction(byval sender as object, byval e as MouseEventArgs) _
    2. handles button1.click, button2.click, notifyicon.click
    3.   MyDelegateFunction()
    4. End Sub
    5.  
    6. Private Sub MyDelegateFunction()
    7.   '... Do stuff
    8. End Sub
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    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.
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width