Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I assign a method from a base form to the keypress event of a textbox in a derived form?

Status
Not open for further replies.

GriffMG

Programmer
Mar 4, 2002
6,309
FR
I'm trying to use a base class form to provide a simple function to all the forms in my project.

I want to be able to set the KeyPress event of any text boxes on the forms derived from my BaseForm
to be able to call that function something like this:

Code:
    public partial class BaseForm : Form
    {
        public BaseForm()
        {
            InitializeComponent();
        }

        public void txtBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                SelectNextControl(ActiveControl, true, true, true, true);
            }
        }
    }

When I try to use the designer to assign the txtBox_KeyDown method it won't allow me, it says it's
defined already in the base class, if I manually edit the derived forms InitializeComponent to look like this:

Code:
            this.mtxtWorkScope.KeyDown += new System.Windows.Forms.KeyEventHandler(base.txtBox_KeyDown);

I can compile and run the project, no errors shown, and it works as expected (allowing Enter to move to next box)
but I then get C# 'panic mode' when I later try and edit the form.

What am I missing?



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Well, it's a bit moot now, I have derived new classes for the main UI components, and put the {Enter}
handling in them, then done a find and replace to change all the instances of TextBox(), ComboBox() and CheckBox()
to use the new elements instead

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top