INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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 Smileing

There are 10 kinds of people in the world, those who understand binary and those who don't.

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

(OP)
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 Smileing

There are 10 kinds of people in the world, those who understand binary and those who don't.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close