nunu
nunu

Reputation: 3252

Override 'KeyDown' event in .Net Winform Textbox

I have created a 'CustomTextBox' class to nevigate focus by 'Enter' and 'Up' key in the .Net Winform development.

I am using given below code to do so.

public class CustomTextBox : System.Windows.Forms.TextBox
    {
        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Return:
                    this.FindForm().GetNextControl(this, true);
                    break;
                case Keys.Up:
                    this.FindForm().GetNextControl(this, false);
                    break;
            }
        }
    }

Here, I am using 'FindForm()' method to get form container for the current TextBox, because I am thinking that 'FindForm()' method could have it's own heaviness to access it.

So my question is, Can I have different method or code to access current textbox's container form?

OR Do you have any idea to do the same by other ways ?

Thanks in advance.

(Could anybody having idea for this? Looking for solution.)

Upvotes: 1

Views: 3603

Answers (2)

benPearce
benPearce

Reputation: 38333

I assume you are using the taborder to find the next control, the taborder is specific to the container holding the control, if the control is the last one in its container, you would need to step one more parent up the control tree to get the next control.

As GvS mentioned use the parent property to get the container to start your search.

Upvotes: 1

GvS
GvS

Reputation: 52518

The Parent property gives you the container the Textbox is in directly.

Be aware that the Parent is not the same as the form. The parent is the control that contains the Control directly. And can be a Panel, GroupBox, other UserControl, Form, etc...

Upvotes: 2

Related Questions