Skip to main content
.NET Framework Class Library
KeyEventArgs..::.SuppressKeyPress Property

Updated: August 2010

Gets or sets a value indicating whether the key event should be passed on to the underlying control.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Syntax
Public Property SuppressKeyPress As Boolean
public bool SuppressKeyPress { get; set; }
public:
property bool SuppressKeyPress {
	bool get ();
	void set (bool value);
}
member SuppressKeyPress : bool with get, set

Property Value

Type: System..::.Boolean
true if the key event should not be sent to the control; otherwise, false.
Remarks

You can assign true to this property in an event handler such as KeyDown in order to prevent user input.

Setting SuppressKeyPress to true also sets Handled to true.

Examples

The following code example prevents numeric keystrokes from reaching the TextBox control named textBox1.


Private Sub TextBox1_KeyDown(ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

    If e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9 And _
    e.Modifiers <> Keys.Shift Then
        e.SuppressKeyPress = True
    End If
End Sub


private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9 && e.Modifiers != Keys.Shift)
    {
        e.SuppressKeyPress = true;
    }
}

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Change History

Date

History

Reason

August 2010

Added SuppressKeyPress code example.

Customer feedback.