PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Two different custom forms with the same custom event-VBForums
Results 1 to 7 of 7

Thread: Two different custom forms with the same custom event

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2004
    Location
    Kansas, USA
    Posts
    348

    Two different custom forms with the same custom event

    I have two forms with the exact same event. The event has the exact same name and the exact syntax - everything.

    The two forms have the same customControl in them. I need to add event handlers in the custom Controls that handle the form(s) events. One form is easy because the customControls are added to that form at design time and I just hard code them. If I use thermalImageControl1.Parent I do not have access the events so...

    1) I could cast the parentControl.Parent to Form1 and see if that succeeds. If not then try casting to Form2, etc. This seems messy as everything is hard coded.
    2) I could extend Form and add the custom event and then use my extended form as the base of the additional forms. The issue here is I still don't know how to "get access to the event unless I cast the parentControl.Parent without messy hand coding.

    What I want to have happen is when the customControl is added to a form at run time it automatically finds the event on the parent form, check for the event and then adds a handler (or removes the handler) automatically. How would you do this?
    Thanks,
    Eric
    --------------------------------------------------------------------------------------------------------------------
    VB.net/C# ... Visual Studio 2017

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2004
    Location
    Kansas, USA
    Posts
    348

    Re: Two different custom forms with the same custom event

    Thinking deeper about #2 above and using reflection I can get the event by doing the following:

    Code:
     
                var ev = thermalImageControl1.Parent.GetType().GetEvent("OnBroadcastMouseLocation");
                ev.AddEventHandler();
    Now my issue is that I do not understand what AddEventHandler() is looking for when it is looking for "Object target" and "Delegate handler". I have tried reading the docs and am still confused.
    Thanks,
    Eric
    --------------------------------------------------------------------------------------------------------------------
    VB.net/C# ... Visual Studio 2017

  3. #3
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,108

    Re: Two different custom forms with the same custom event

    How are you defining the event in the two forms?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2004
    Location
    Kansas, USA
    Posts
    348

    Re: Two different custom forms with the same custom event

    The current state is to define an Extended form that has the events and then inherit that with the forms:

    Code:
        public class ExtendedForm : Form
        {
            public delegate void BroadcastMouseLocation(double x, double y);
            public event BroadcastMouseLocation OnBroadcastMouseLocation;
    
    
            public void ReceivedMouseHover(double x, double y)
            {
                Console.WriteLine("ExtendedForm recd mouse event " + x + ":" + y);
                OnBroadcastMouseLocation(x, y);
            }
    
        }
    
    and then...
    
    
        public partial class Form1 : ExtendedForm
        {
         //Form definition stuff
        }
    
        public partial class Form2 : ExtendedForm
        {
         //Form definition stuff
        }
    Thanks,
    Eric
    --------------------------------------------------------------------------------------------------------------------
    VB.net/C# ... Visual Studio 2017

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,238

    Re: Two different custom forms with the same custom event

    The other option would be to define an interface with the event as a member, then simply have each form implement that interface.

    By the way, I strongly recommend that you stick to the standard pattern for custom events, which means an event handler with 'sender' and 'e' parameters. To learn how to implement that pattern, follow the Blog link in my signature and check out my post on Custom Events.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2004
    Location
    Kansas, USA
    Posts
    348

    Re: Two different custom forms with the same custom event

    jmcilhinney - Thanks for that.

    Question...what is the why behind keeping the standard pattern for events?

    *Edit*
    Quoting your blog:
    While you don’t have to, it’s a good idea to stick to that convention in your own code. Doing so means that your interface will be consistent with the rest of the Framework, and consistency is a good thing. Using your types will feel familiar to yourself and others because they will behave just like the types you’re used to using from the Framework.
    Last edited by flycast; Mar 15th, 2019 at 10:33 AM.
    Thanks,
    Eric
    --------------------------------------------------------------------------------------------------------------------
    VB.net/C# ... Visual Studio 2017

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,238

    Re: Two different custom forms with the same custom event

    Quote Originally Posted by flycast View Post
    Question...what is the why behind keeping the standard pattern for events?
    Consistency is always a good thing. If you do the same thing different ways at different times, there's more chance you will make an error doing it. If you always do it the same way, you can far more easily detect something done incorrectly. It is the same for people using the code. If I see something done the way I expect then I am confident in using it. If I see something done a different way for no apparent reason, I wonder whether I have done something wrong in using it or if the author has done something wrong in implementing it. Given how easy it is to do it "properly" in this case, you're expending more energy asking why you should do it that way than you are in just doing it.
    csharp Code:
    1. public class LocationEventArgs : EventArgs
    2. {
    3.     public int X { get; }
    4.     public int Y { get; }
    5.  
    6.     public LocationEventArgs(int x, int y)
    7.     {
    8.         X = x;
    9.         Y = y;
    10.     }
    11. }
    csharp Code:
    1. public class ExtendedForm : Form
    2. {
    3.     public event EventHandler<LocationEventArgs> BroadcastMouseLocation;
    4.  
    5.     protected virtual void OnBroadcastMouseLocation(LocationEventArgs e)
    6.     {
    7.         BroadcastMouseLocation?.Invoke(this, e);
    8.     }
    9. }
    If you wanted, you could add another method so that each derived form didn't have to create the LocationEventArgs:
    csharp Code:
    1. public class ExtendedForm : Form
    2. {
    3.     public event EventHandler<LocationEventArgs> BroadcastMouseLocation;
    4.  
    5.     protected virtual void OnBroadcastMouseLocation(LocationEventArgs e)
    6.     {
    7.         BroadcastMouseLocation?.Invoke(this, e);
    8.     }
    9.  
    10.     protected void RaiseBroadcastMouseLocation(int x, int y)
    11.     {
    12.         OnBroadcastMouseLocation(new LocationEventArgs(x, y));
    13.     }
    14. }
    There's also the fact that having a 'sender' parameter in your event handlers, you are able to use a common method to handle an event for multiple objects and then determine which object raised the event in the code. As well as that, by having every event handler receive the sender as an Object reference and data in an object that is EventArgs or is derived from it, you can even use the same method to handle different events for different types of objects.
    Last edited by jmcilhinney; Mar 15th, 2019 at 08:26 PM.

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width