Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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