Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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