Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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