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 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.