TextBox Beep on Enter Key

R

Rob Windsor [MVP]

Hi all,

Does anybody know why the system emits a beep when the Enter key is pressed
inside a TextBox? I know how to stop the beep from happening but I was
wondering what the purpose of the beep is in the first place.

TIA
 
C

C# Learner

Rob said:
Hi all,

Does anybody know why the system emits a beep when the Enter key is pressed
inside a TextBox? I know how to stop the beep from happening but I was
wondering what the purpose of the beep is in the first place.

TIA

This happens in a single-line (as opposed to multi-line) text box. I
believe the reason behind this is to indicate to the user that pressing
[enter] in a single-line text box won't create a new line.
 
J

Jason Newell

Rob,
I have pasted a skeleton class that extends the TextBox class. I use
this class when I need to override some of the default stuff that goes on in
the TextBox control. The main point of interest is the WM_GETDLGCODE that
is sent to the TextBox. You need to detect that message and modify it's
return value as show. This is how you can receive the OnKeyPress() event
for the Enter and Esc key. This will fix the beep for the Enter key, but
not the Esc key. Not sure why. HTH.

public class TextBoxEx : System.Windows.Forms.TextBox
{
const int WM_GETDLGCODE = 0x0087;
const int DLGC_WANTARROWS = 0x0001;
const int DLGC_WANTTAB = 0x0002;
const int DLGC_WANTALLKEYS = 0x0004;
const int DLGC_WANTMESSAGE = 0x0004;
const int DLGC_HASSETSEL = 0x0008;
const int DLGC_DEFPUSHBUTTON = 0x0010;
const int DLGC_UNDEFPUSHBUTTON = 0x0020;
const int DLGC_RADIOBUTTON = 0x0040;
const int DLGC_WANTCHARS = 0x0080;
const int DLGC_STATIC = 0x0100;
const int DLGC_BUTTON = 0x2000;

public TextBoxEx()
{
}

protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress (e);

switch ((Keys)e.KeyChar)
{
case Keys.Enter:
e.Handled = true;
break;
case Keys.Escape:
e.Handled = true;
break;
}
}

protected override void WndProc(ref Message m)
{
base.WndProc(ref m);

if( m.Msg == (int)WM_GETDLGCODE )
{
m.Result = new IntPtr( (int)DLGC_WANTCHARS |
(int)DLGC_WANTARROWS |
(int)DLGC_HASSETSEL |
(int)DLGC_WANTALLKEYS |
m.Result.ToInt32() );
}
}
}
 
J

Jeremy Todd

Jason Newell said:
Rob,
I have pasted a skeleton class that extends the TextBox class. I use
this class when I need to override some of the default stuff that goes on in
the TextBox control. The main point of interest is the WM_GETDLGCODE that
is sent to the TextBox. You need to detect that message and modify it's
return value as show. This is how you can receive the OnKeyPress() event
for the Enter and Esc key. This will fix the beep for the Enter key, but
not the Esc key. Not sure why. HTH.

That may work, but it's doing it the hard way. ProcessDialogKey lets
you do the same without messing with WndProc, and it will work for Escape
too. In VB, try something like:

Protected Overrides Function ProcessDialogKey(ByVal keyData As
System.Windows.Forms.Keys) As Boolean
Select Case keyData
Case Keys.Enter, Keys.Return, Keys.Escape: Return True
Case Else: Return MyBase.ProcessDialogKey(keyData)
End Select
End Function

Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top