KeyPressEventArgs.Handled 属性

获取或设置一个值,该值指示是否处理过 KeyPress 事件。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Property Handled As Boolean
用法
Dim instance As KeyPressEventArgs
Dim value As Boolean

value = instance.Handled

instance.Handled = value
public bool Handled { get; set; }
public:
property bool Handled {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_Handled ()

/** @property */
public void set_Handled (boolean value)
public function get Handled () : boolean

public function set Handled (value : boolean)

属性值

如果处理过事件,则为 true;否则为 false

备注

如果未处理事件,则会将它发送到操作系统进行默认处理。将 Handled 设置为 true,以取消 KeyPress 事件。

示例

下面的示例创建一个 TextBox 控件。keypressed 方法使用 KeyChar 属性检查是否按了 Enter 键。如果按了 Enter 键,则 Handled 属性设置为 true,表示此事件已处理。

Imports System
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 'New

    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 'keypressed

    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub 'Main
End Class '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());
    }
}
#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 );
}
import System.*;
import System.Windows.Forms.*;

public class Form1 extends Form
{
    public Form1()
    {
        // Create a TextBox control.
        TextBox tb = new TextBox();
        this.get_Controls().Add(tb);
        tb.add_KeyPress(new KeyPressEventHandler(KeyPressed));
    } //Form1

    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.get_KeyChar() == (char)(13)) {
            e.set_Handled(true);
        }
    } // KeyPressed

    public static void main(String[] args)
    {
        Application.Run(new Form1());
    } //main
} //Form1

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

KeyPressEventArgs 类
KeyPressEventArgs 成员
System.Windows.Forms 命名空间
Control.KeyPress 事件