Placing a event handler in the base class

A

AliR \(VC++ MVP\)

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()
{
InitializeComponent();
}
};

In the InitializeComponent method there is a line

this.questionEdit.Enter += new System.EventHandler(this.EditCtrl_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
InitializeComponent and put it in the constructor of the ChildForm1

public partial class ChildForm1 : BaseFormClass
{
public ChildForm1()
{
InitializeComponent();
this.questionEdit.Enter += new
System.EventHandler(this.EditCtrl_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.
 
I

Ignacio Machin ( .NET/ C# MVP )

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()
    {
        InitializeComponent();
    }

};

In the InitializeComponent method there is a line

this.questionEdit.Enter += new System.EventHandler(this.EditCtrl_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
InitializeComponent and put it in the constructor of the ChildForm1

public partial class ChildForm1 : BaseFormClass
{
    public ChildForm1()
    {
        InitializeComponent();
        this.questionEdit.Enter += new
System.EventHandler(this.EditCtrl_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 InitializeComponent
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top