Handling the beep in a Windows TextBox for Enter key. (C#)

Posted in C#, TextBox, Trapping Key Events, VB with tags on July 8, 2010 by christophermelvin

There are times when you want to find out when the user presses the enter key inside a specific text box, then perform some action.

If the text box is not multiline Windows sounds its annoying beep to tell the user they did something wrong.

And, if the text box is multiline, Windows does what you would expect and appends a carriage return/line feed to the end of the text in the text box.

But, what if you don’t want the carriage return/line feed characters and you also don’t want that annoying beep.

Here’s the solution:

1. Handle the KeyPress event for the text box.

2. Check the KeyChar for the Enter key.

3. Perform the code you want.

4. Set e.Handled to true before returning.

Here’s the code snippet:

private void textboxName_KeyPress(object sender, KeyPressEventArgs e)

{

if (e.KeyChar == (char)13) // 13 is Enter key.

{

// CODE YOU WANT TO EXECUTE GOES HERE.

e.Handled=true;   // NOTE: If you want the CRLF to be included in a multiline textbox, then leave e.Handled = false

}

}

The above code is simple, removes the annoying Windows beep and allows you to accept a user’s Enter key without appending a carriage return/line feed to the end of the text box’s text.

If you want to handle the Enter key, but also have the carriage return/line feed characters included in the text box’s text, then you just need to set the text box to multiline and leave e.Handled = false.

This works with VB.NET, C and C++ as well.

How to get rid of that annoying beep with Alt keys in Windows programs (C#)

Posted in Annoying Windows beep on Alt key., C#, Trapping Key Events, VB with tags , on July 6, 2010 by christophermelvin

If you’ve ever programmed Windows programs you have probably noticed that the menus are activated and handled by Windows itself, not by your program. This has been the case with all versions of Windows that I’ve ever programmed going back to 3.1.

Because the menus are handled by Windows and the Alt key is used to activate menus, attempting to use the Alt key to cause the displaying of your own popup menu causes Windows sound that annoying nasty beep indicating that the key sequence is invalid. It doesn’t matter that you handle the Alt key in an event. The beep still occurs.

Handling:

You can get rid of the annoying beep by adding a undocked Windows menu and setting the menu visibility to false.

You trap the Alt key using KeyDown event on form.

Here’s the event code for KeyDown event:

private void frmYourFormName_KeyDown(object sender, KeyEventArgs e)
{

// We are only interested in certain keys here.

if (e.Alt == true)

{

if (e.KeyValue == 70)   // Alt F (Can use any character)
{

// CODE YOU WANT TO EXECUTE WHEN USER PRESSES ALT-F

}

}

}

Clean-up

With this method there is one clean up that is needed. Pressing Alt-F activates the Windows menu even though it’s invisible. The Windows menu does not deactivate until an item in the Windows menu is selected, or the user presses Alt or Escape, or the user clicks somewhere else in the window.

There is no menu.Deactivate function. 😦

The answer to this  is to send an Escape key to the program. If you do not do this the invisible Windows menu will retain focus regardless of anything else you do. Setting focus to a control will not deactivate the menu. This is done in C# with the following code:

System.Windows.Forms.SendKeys.Send(“{ESC}”);

This handling also should work for VB.NET, C and C++.

Why This Blog

Posted in Introduction on July 6, 2010 by christophermelvin

This is my new blog on unusual programming issues and how I’ve resolved them.

When I run into something unusual I do a search to see if there’s any worthwhile posts on the matter and 95% of the time I’ll find the exact answer I need. However, there are those times when there’s either nothing at all or finding the solution takes a long, long time.

This blog is for those hard to find programming tricks of the trade.

While I currently write mostly in C# and the handlings are geared toward that language, these apply equally to VB and often to C and C++.

Design a site like this with WordPress.com
Get started