Solved

c#- SendKeys.Send("{tab}");

Posted on 2004-08-04
4
42,473 Views
Last Modified: 2011-08-18
i am using SendKeys.Send("{tab}"); in the key press event of a text box to move to the next textbox by pressing ENTER key

This is working fine, but i am getting an error beep. can anybody plz explain

the code is below:-
private void TextBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
  if (e.KeyChar == 13)
  {
     SendKeys.Send("{tab}");
  }
}
0
Comment
Question by:Dhanraj_k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
Ceiled earned 50 total points
ID: 11714636
The warning beep is the default behaviour when you press enter in a single-line textbox. To disable it, simply set e.Handled = true in the KeyPress event to short-circuit the default behaviour.

A few more notes...

First of all, I had to check for char-code 10 ('\r') to handle enter key-presses -- you may want to handle both 13 ('\n') and 10 ('\r'), just to be safe.

Secondly, SendKeys.Send("{tab}") really isn't a very robust way to do this. I would recommend using GetNextControl() and Focus() instead, like so:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
      if (e.KeyChar == '\r' || e.KeyChar == '\n')
      {
            GetNextControl(sender as Control, true).Focus();
            e.Handled = true;
      }
}

Of course, the char codes and GetNextControl() call are just suggestions -- adding the e.Handled = true statement in your if statement should be enough to fix the beeping.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11714647
The beep comes from the enter key, not from the tab. To fix it, just change the code to:

private void TextBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
  if (e.KeyChar == 13)
  {
     SendKeys.Send("{tab}");
     e.Handled = true;
  }
}
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 11714673
Sorry Ceiled, your post was not there when I started writing mine...
0
 
LVL 7

Expert Comment

by:Ceiled
ID: 11714733
No problem...two minutes isn't a very big gap.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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