locked
how to overide base class event method in the child class for a control RRS feed

  • Question

  • User1049200748 posted

    Hi The  problem in my application  the base form is having the public virtual void disablecontrol method There is a foreach loop for each control and if the control is of text box then they are calling the parsedit value event and attaching the memo_parseeditvalue method.so in the child form when iam using any textbox it is taking the base form defined event and unable to change the some values in the textbox which I defined .so how can I overide this event in the Child form

    Monday, August 5, 2013 1:32 PM

Answers

  • User1143442848 posted

    Hi jagadeesh,

    Here is an example about overriding the base control event. Please try to refer to the following code:

    Base control code

      public class BaseControl : Control
            {
                private object _selected;
    
                public object Selected
                {
                    get { return _selected; }
                    set
                    {
                        if (!Equals(_selected, value))
                        {
                            _selected = value;
                            OnSelectedChanged(EventArgs.Empty);
                        }
                    }
                }
    
                public event EventHandler SelectedChanged;
    
                protected virtual void OnSelectedChanged(EventArgs e)
                {
                    if (SelectedChanged != null)
                        SelectedChanged(this, e);
                }
            }
    

    With this example, you may override OnSelectedChanged event in the child class like this:

     public class MyControl : BaseControl
            {
                protected override void OnSelectedChanged(EventArgs e)
                {
                    base.OnSelectedChanged(e);
    
                    // My own logic.
                }
            }
    

    For more information, please try to refer to the following links:

    # How to override method and event in winform usercontrol in C#

    http://stackoverflow.com/questions/4194782/how-to-override-method-and-event-in-winform-usercontrol-in-c

    # How to override custom control event

    http://stackoverflow.com/questions/14934900/how-to-override-custom-control-event 

    Best Regards,

    Lisa Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 6, 2013 8:14 AM