Results 1 to 4 of 4

Thread: [RESOLVED] usercontrol event

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved [RESOLVED] usercontrol event

    I understand how i create properties for a usercontrol but im having difficulty finding info on how you would create a "event" for user control so i can trigger code to run on the form and not in the control itself


    anyone have a example?

    thanks

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

    Re: usercontrol event

    The fact that it's a UserControl is irrelevant. It's just a class. You add events to any class the exact same way:
    CSharp Code:
    1. public event EventHandler MyEvent;
    2.  
    3. protected virtual void OnMyEvent(EventArgs e)
    4. {
    5.     if (this.MyEvent != null)
    6.     {
    7.         this.MyEvent(this, e);
    8.     }
    9. }
    To raise the event you call the OnMyEvent method:
    CSharp Code:
    1. this.OnMyEvent(EventArgs.Empty);
    If you need to pass data to the event handlers then you'd use the generic EventHandler delegate:
    CSharp Code:
    1. public event EventHandler<CancelEventArgs> MyEvent;
    2.  
    3. protected virtual void OnMyEvent(CancelEventArgs e)
    4. {
    5.     if (this.MyEvent != null)
    6.     {
    7.         this.MyEvent(this, e);
    8.     }
    9. }
    I've used CancelEventArgs there as an example but you can use any appropriate class that inherits EventArgs, either from the Framework or of your own creation. When you raise the event you obviously need to pass an object of that type to the OnMyEvent method.

    One final note. I've also just used "MyEvent" as an example. You'd name your event something more appropriate and then name the method that raises it to match. If you look through the MSDN documentation you'll see that almost all events are raised by a method with the same name prefixed with "On".
    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

  3. #3

  4. #4

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: usercontrol event

    Quote Originally Posted by jmcilhinney
    The fact that it's a UserControl is irrelevant. It's just a class. You add events to any class the exact same way:
    CSharp Code:
    1. public event EventHandler MyEvent;
    2.  
    3. protected virtual void OnMyEvent(EventArgs e)
    4. {
    5.     if (this.MyEvent != null)
    6.     {
    7.         this.MyEvent(this, e);
    8.     }
    9. }
    To raise the event you call the OnMyEvent method:
    CSharp Code:
    1. this.OnMyEvent(EventArgs.Empty);
    If you need to pass data to the event handlers then you'd use the generic EventHandler delegate:
    CSharp Code:
    1. public event EventHandler<CancelEventArgs> MyEvent;
    2.  
    3. protected virtual void OnMyEvent(CancelEventArgs e)
    4. {
    5.     if (this.MyEvent != null)
    6.     {
    7.         this.MyEvent(this, e);
    8.     }
    9. }
    I've used CancelEventArgs there as an example but you can use any appropriate class that inherits EventArgs, either from the Framework or of your own creation. When you raise the event you obviously need to pass an object of that type to the OnMyEvent method.

    One final note. I've also just used "MyEvent" as an example. You'd name your event something more appropriate and then name the method that raises it to match. If you look through the MSDN documentation you'll see that almost all events are raised by a method with the same name prefixed with "On".

    Thanks

    It looked a little complex at first but actually its pretty simple. Im still shakey on some of the more complex concepts of Object oriented programing in general but i think im getting there slowly.


    again thanks for the pointer.

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