Placing a event handler in the base class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AliR \(VC++ MVP\)

    Placing a event handler in the base class

    Hi Everyone,

    I have a few form classes that inherit from the same base class. The main
    reason that this is done is that some event handlers are common between
    these classes and I was trying to save time and not have to put a event
    handler in every form class.

    The base class has an event handler for Edit.Enter and Edit.Leave so that it
    can turn the Edit menu items on and off.

    So here is the class

    public class BaseFormClass : Form
    {
    private Object curEditCtrl;
    public BaseFormClass()
    {
    curEditCtrl = null;
    }

    protected void EditCtrl_Enter( object sender, EventArgs e)
    {
    curEditCtrl = sender;
    //turn on the edit menu items (copy, cut, paste...)
    }

    protected void EditCtrl_Leave( object sender, EventArgs e)
    {
    curEditCtrl = null;
    //turn off the edit menu items (copy, cut, paste...)
    }
    }

    then I have a class ChildForm1 that inherits from BaseFormClass

    public partial class ChildForm1 : BaseFormClass
    {
    public ChildForm1()
    {
    InitializeCompo nent();
    }
    };

    In the InitializeCompo nent method there is a line

    this.questionEd it.Enter += new System.EventHan dler(this.EditC trl_Enter);

    where EditCtrl_Enter is defined in BaseFormClass.

    Every this compiling fine and running fine, but I could no longer open
    ChildForm1 in Design View because I would get this error:
    "The Method 'EditCtrl_Enter ' cannot be the method for an event because a
    class this class derives from already defines the method."

    So I solved the problem by moving the event handler assignment line out of
    InitializeCompo nent and put it in the constructor of the ChildForm1

    public partial class ChildForm1 : BaseFormClass
    {
    public ChildForm1()
    {
    InitializeCompo nent();
    this.questionEd it.Enter += new
    System.EventHan dler(this.EditC trl_Enter);
    }
    };


    This lets me open ChildForm1 in design view.

    Can anyone think of a better solution for this problem?

    With my solution the Events Property window does not show that Enter is
    being handled.

    Thanks
    AliR.


  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: Placing a event handler in the base class

    On Jul 15, 11:09 am, "AliR \(VC++ MVP\)" <A...@online.no spamwrote:
    Hi Everyone,
    >
    I have a few form classes that inherit from the same base class.  The main
    reason that this is done is that some event handlers are common between
    these classes and I was trying to save time and not have to put a event
    handler in every form class.
    >
    The base class has an event handler for Edit.Enter and Edit.Leave so thatit
    can turn the Edit menu items on and off.
    >
    So here is the class
    >
    public class BaseFormClass : Form
    {
        private Object curEditCtrl;
        public BaseFormClass()
        {
            curEditCtrl = null;
        }
    >
        protected void EditCtrl_Enter( object sender, EventArgs e)
        {
            curEditCtrl = sender;
            //turn on the edit menu items (copy, cut, paste...)
        }
    >
       protected void EditCtrl_Leave( object sender, EventArgs e)
       {
            curEditCtrl = null;
            //turn off the edit menu items (copy, cut, paste...)
        }
    >
    }
    >
    then I have a class ChildForm1 that inherits from BaseFormClass
    >
    public partial class ChildForm1 : BaseFormClass
    {
        public ChildForm1()
        {
            InitializeCompo nent();
        }
    >
    };
    >
    In the InitializeCompo nent method there is a line
    >
    this.questionEd it.Enter += new System.EventHan dler(this.EditC trl_Enter);
    >
    where EditCtrl_Enter is defined in BaseFormClass.
    >
    Every this compiling fine and running fine, but I could no longer open
    ChildForm1 in Design View because I would get this error:
    "The Method 'EditCtrl_Enter ' cannot be the method for an event because a
    class this class derives from already defines the method."
    >
    So I solved the problem by moving the event handler assignment line out of
    InitializeCompo nent and put it in the constructor of the ChildForm1
    >
    public partial class ChildForm1 : BaseFormClass
    {
        public ChildForm1()
        {
            InitializeCompo nent();
            this.questionEd it.Enter += new
    System.EventHan dler(this.EditC trl_Enter);
        }
    >
    };
    >
    This lets me open ChildForm1 in design view.
    >
    Can anyone think of a better solution for this problem?
    >
    With my solution the Events Property window does not show that Enter is
    being handled.
    >
    Thanks
    AliR.
    Hi,

    I think this could be an issue with the IDE, it expect to find the
    method defined in the class itselft, not in a parent class.
    You could move the handler assignation from the InitializeCompo nent
    to the constructor. there the IDE will not see it and will not
    complain.
    Of corse you will not see the event being handle in the IDE

    Comment

    Working...