Solved

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

Posted on 2004-08-04
4
42,278 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
  • 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

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

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!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now