Solved

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

Posted on 2004-09-30
4
245 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

860 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