Solved

How to jump to the next control by using the ENTER key

Posted on 2004-09-30
4
244 Views
Last Modified: 2010-04-15
Let's have a form with several text boxes. The tab order has been set as needed and is working as expected. After the user fills each text box, the tab key moves the focus to the next text box and so on. I need suggestions on what would be the most convenient method to allow the user to go to the next text box by using the ENTER key as an alternate key?
0
Comment
Question by:Alberto071397
  • 2
4 Comments
 
LVL 10

Expert Comment

by:123654789987
ID: 12197365
0
 
LVL 12

Accepted Solution

by:
sumix earned 125 total points
ID: 12197515
Hi,
A more complete approach is to override the ProcessCmdKey method of the form
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
            {
                  if      (msg.WParam.ToInt32() == (int) Keys.Enter)  
                  {
                        SendKeys.SendWait("{Tab}");
                        return true;
                  }
                  return base.ProcessCmdKey (ref msg, keyData);
            }

You can also can test if the numeric keypad Enter was pressed by checking the 25th bit of msg.LParam property:
 ... &&  (((msg.LParam.ToInt32()>> 24) & 1) ==1)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12202167
I like the SendKeys.SendWait with the {Tab} key the best, then you don't need to specify a control to focus to.

Bob
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 12202238
This is what I use:

  private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Enter)
      {
        SendKeys.SendWait("{Tab}");
        e.Handled = true;
      }
    }

Bob
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question