locked
Event override in custom control RRS feed

  • Question

  • Hello.

    I created a custom control called MultiCheckCombo that inherits from a combobox. This control has the possibility of having the checkbox in each element. In the class that defines the control I have called the event DropDownClosed that sets some of the label control. Now in another part of my code I have a user control that I've placed on my control xaml I called cmbCheck and inside me code works correctly. in this user control I again called the event DropDownClosed cmbCheck there because I need to intercept.In a way I wanted to override an event. I tested the code and it works is called before the DropDownClosed class MultiCheckCombo and then the same event cmbCheck.

    I would understand if this is the right way and is a good programming practice or you can do it another way.

    Thanks to those who will help me

    www.Photoballot.net


    • Edited by first100 Monday, March 23, 2015 11:58 AM
    Monday, March 23, 2015 10:16 AM

Answers

  • >>how should I do in my user control to override base event?

    You cannot override any method that is not defined in the base class of the user control.

    And you cannot affect or trying to "override" any other subscriber that subscribes to the same event. As I mentioned, an event may have multiple subscribers (event handlers) and these are totally unaware of each other. Both MultiCheckCombo_DropDownClosed and cmbHelper_DropDownClosed will always be invoked one after another when the DropDownClosed event is raised if these event handlers have been hooked up somewhere.

    If you want to remove the MultiCheckCombo_DropDownClosed event handler you could add a method to the MultiCheckCombo that does this:

    MultiCheckCombo:

    public void UnHook()
    {
    DropDownClosed -= MultiCheckCombo_DropDownClosed;
    }

    UC:

    this.cmbHelper.UnHook();
    this.cmbHelper.DropDownClosed += cmbHelper_DropDownClosed;
    //add the event handler back here...


    But you cannot "override" another subscriber's event handler. This is not how events nor object oriented programming works.


    Please remember to close your threads by marking all helpful posts as answer and please start a new thread if you have a new question.

     

    • Proposed as answer by Barry Wang Tuesday, March 24, 2015 8:53 AM
    • Marked as answer by first100 Tuesday, March 24, 2015 3:21 PM
    Monday, March 23, 2015 11:33 AM

All replies

  • If you take a look at override:

    https://msdn.microsoft.com/en-us/library/ebca9ah3.aspx?f=255&MSPPError=-2147217396

    You see:

    "The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event."

    You could therefore make your eventhandler virtual in your custom control thingummy.

    Inherit from that and override where required.

    A bit like:

    http://stackoverflow.com/questions/1127851/c-what-are-virtual-events-and-how-can-they-be-used

    using System;
    
    class Base
    {
        public virtual event EventHandler Foo
        {
            add
            {
                Console.WriteLine("Base Foo.add called");
            }
            remove
            {
                Console.WriteLine("Base Foo.remove called");
            }
        }
    }
    
    class Derived : Base
    {
        public override event EventHandler Foo
        {
            add
            {
                Console.WriteLine("Derived Foo.add called");
            }
            remove
            {
                Console.WriteLine("Derived Foo.remove called");
            }
        }
    }
    
    class Test
    {
        static void Main()
        {
            Base x = new Derived();
    
            x.Foo += (sender, args) => {};
        }
    }


    Hope that helps.
    Recent Technet articles: Property List Editing; Dynamic XAML

    • Proposed as answer by Barry Wang Tuesday, March 24, 2015 8:53 AM
    Monday, March 23, 2015 10:26 AM
  • You can certainly hook up several event handlers to the same event. Each handler will be called when the event is raised. This is how an event is supposed to work.

    If you want to "override" the DropDownClosed event, you should override the OnDropDownClosed of the ComboBox class:

    public class MultiCheckCombo : ComboBox
        {
            protected override void OnDropDownClosed(EventArgs e)
            {
                base.OnDropDownClosed(e);
            }
        }

    This method will be called whenever the DropDownClosed event is raised by the base class.

    Please remember to close your threads by marking all helpful posts as answer and please start a new thread if you have a new question.

     

    Monday, March 23, 2015 10:32 AM
  • I have MultiCheckCombo class , in this class i have defined

     public virtual void MultiCheckCombo_DropDownClosed(object sender, EventArgs e)
            {
                SetItems();
            }


    into a user control i have :

      MultiCheckCombo cmbHelper;
    
    this.cmbHelper.DropDownClosed += cmbHelper_DropDownClosed;
    
     public void cmbHelper_DropDownClosed(object sender, EventArgs e)
            {
                SomeCode();
            }
    
    

    how should I do in my user control to override base event?

    Maybe something similar to :

    protected override void DropDownClosed(object sender, EventArgs e)
            {
               base.DropDownClosed(e);
    
                
            }

    Many thanks guys :)


    www.Photoballot.net

    Monday, March 23, 2015 11:23 AM
  • >>how should I do in my user control to override base event?

    You cannot override any method that is not defined in the base class of the user control.

    And you cannot affect or trying to "override" any other subscriber that subscribes to the same event. As I mentioned, an event may have multiple subscribers (event handlers) and these are totally unaware of each other. Both MultiCheckCombo_DropDownClosed and cmbHelper_DropDownClosed will always be invoked one after another when the DropDownClosed event is raised if these event handlers have been hooked up somewhere.

    If you want to remove the MultiCheckCombo_DropDownClosed event handler you could add a method to the MultiCheckCombo that does this:

    MultiCheckCombo:

    public void UnHook()
    {
    DropDownClosed -= MultiCheckCombo_DropDownClosed;
    }

    UC:

    this.cmbHelper.UnHook();
    this.cmbHelper.DropDownClosed += cmbHelper_DropDownClosed;
    //add the event handler back here...


    But you cannot "override" another subscriber's event handler. This is not how events nor object oriented programming works.


    Please remember to close your threads by marking all helpful posts as answer and please start a new thread if you have a new question.

     

    • Proposed as answer by Barry Wang Tuesday, March 24, 2015 8:53 AM
    • Marked as answer by first100 Tuesday, March 24, 2015 3:21 PM
    Monday, March 23, 2015 11:33 AM
  • Thanks to all, i now have now the ideas a little clearer. From what I understand I can leave my code as it is? I wanted to know if it was a bad programming practice. Very useful solution Magnus 

    www.Photoballot.net

    Monday, March 23, 2015 2:37 PM
  • I don't really understand your explanation well enough.

    As you say

    "In a way I wanted to override an event"

    My reply above is intended to explain how one would override an event.

    I can't tell if this is what you want to do or not.


    Hope that helps.
    Recent Technet articles: Property List Editing; Dynamic XAML

    Tuesday, March 24, 2015 9:45 AM
  • Thanks Andy, I realized if you want to override event how I can do it, but I was also asked if as I had done in my code was a bad programming practice. Since my code  works as I did leave it as it is :)

    www.Photoballot.net


    • Edited by first100 Tuesday, March 24, 2015 12:04 PM
    Tuesday, March 24, 2015 12:02 PM

  • Top of my list of things too watch out for is working.

    Next is how easy is it to maintain.

    If it works but is a nightmare to maintain "good practice" then I would actually prefer "bad practice".  

    It's therefore quite difficult to define what is good and bad.  

    I think your approach could well be in a sort of grey area.


    It sounds like it'd be easy to maintain and it works though.

    Depending on time complexity etc etc you might want to consider refactoring that common code into a base method and out the base handler.  Then you avoid one handler calling another handler.


    Hope that helps.
    Recent Technet articles: Property List Editing; Dynamic XAML

    Tuesday, March 24, 2015 1:22 PM
  • Thanks for the further clarification.  I have done as you has written above , defining the method UnHook and doing the rest.

    Thank you very much :)


    www.Photoballot.net

    Tuesday, March 24, 2015 3:24 PM