KeyPressEventArgs.Handled 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置一个值,该值指示是否处理过 KeyPress 事件。
public:
property bool Handled { bool get(); void set(bool value); };
public bool Handled { get; set; }
member this.Handled : bool with get, set
Public Property Handled As Boolean
属性值
如果处理过事件,则为 true
;否则为 false
。
示例
以下示例创建一个 TextBox 控件。 方法keypressed
使用 KeyChar 属性检查是否按下 Enter 键。 如果按 Enter 键,则 Handled 属性设置为 true
,这表示已处理事件。
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Windows::Forms;
public ref class Form1: public Form
{
public:
Form1()
{
// Create a TextBox control.
TextBox^ tb = gcnew TextBox;
this->Controls->Add( tb );
tb->KeyPress += gcnew KeyPressEventHandler( this, &Form1::keypressed );
}
private:
void keypressed( Object^ /*o*/, KeyPressEventArgs^ e )
{
// The keypressed method uses the KeyChar property to check
// whether the ENTER key is pressed.
// If the ENTER key is pressed, the Handled property is set to true,
// to indicate the event is handled.
if ( e->KeyChar == (char)13 )
e->Handled = true;
}
};
int main()
{
Application::Run( gcnew Form1 );
}
using System;
using System.Windows.Forms;
public class Form1: Form
{
public Form1()
{
// Create a TextBox control.
TextBox tb = new TextBox();
this.Controls.Add(tb);
tb.KeyPress += new KeyPressEventHandler(keypressed);
}
private void keypressed(Object o, KeyPressEventArgs e)
{
// The keypressed method uses the KeyChar property to check
// whether the ENTER key is pressed.
// If the ENTER key is pressed, the Handled property is set to true,
// to indicate the event is handled.
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
}
}
public static void Main()
{
Application.Run(new Form1());
}
}
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Public Sub New()
' Create a TextBox control.
Dim tb As New TextBox()
Me.Controls.Add(tb)
AddHandler tb.KeyPress, AddressOf keypressed
End Sub
Private Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
' The keypressed method uses the KeyChar property to check
' whether the ENTER key is pressed.
' If the ENTER key is pressed, the Handled property is set to true,
' to indicate the event is handled.
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
e.Handled = True
End If
End Sub
Public Shared Sub Main()
Application.Run(New Form1())
End Sub
End Class
注解
如果未处理事件,则会将其发送到操作系统进行默认处理。
true
设置为 Handled 以取消KeyPress
事件。