Form.KeyPreview 属性

今天再做KeyDown 和 KeyUp 事件时,就是忘了设置,窗体的KeyPreview 属性,所以KeyDown 和 KeyUp 事件没有反应(这里说明一下,本人使用的是自定义控件,如果是窗体控件就没有问题的。具体原因,下面会有解释。)

获取或设置一个值,该值指示在将键事件传递到具有焦点的控件前,窗体是否将接收此键事件。

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

 
public bool KeyPreview { get; set; }
 
/** @property */
public boolean get_KeyPreview () /** @property */
public void set_KeyPreview (boolean value)
 
public function get KeyPreview () : boolean

public function set KeyPreview (value : boolean)

属性值

如果窗体将接收所有键事件,则为 true;如果窗体上当前选定控件接收键事件,则为 false。默认为 false

备注

当此属性设置为 true 时,窗体将接收所有 KeyPressKeyDown 和 KeyUp 事件。在窗体的事件处理程序处理完该击键后,然后将该击键分配给具有焦点的控件。例如,如果 KeyPreview 属性设置为 true,而且当前选定的控件是 TextBox,则在窗体的事件处理程序处理了击键后,TextBox 控件将接收所按的键。要仅在窗体级别处理键盘事件并且不允许控件接收键盘事件,请将窗体的 KeyPress 事件处理程序中的KeyPressEventArgs.Handled 属性设置为 true

可以使用此属性处理应用程序中的大部分击键事件,并可以处理击键事件或调用适当的控件来处理击键事件。例如,当应用程序使用功能键时,可能希望在窗体级别处理这些击键,而不是为可能接收击键事件的每个控件编写代码。

注意

如果窗体没有可见或启用的控件,则该窗体自动接收所有键盘事件。

注意

可以对窗体上的控件进行编程,以取消它所接收的任何击键。由于控件从不向窗体发送这些击键,因此无论 KeyPreview 为何种设置,窗体永远都无法看到它们。

示例

 
 
下面的代码示例演示如何将窗体的 KeyPreview 属性设置为 true 以及如何处理窗体级别的键事件。要运行该示例,请将以下代码粘贴到一个空白窗体中。

 
using namespace System::Windows::Forms;

// This button is a simple extension of the button class that overrides
// the ProcessMnemonic method. If the mnemonic is correctly entered,
// the message box will appear and the click event will be raised.
// This method makes sure the control is selectable and the
// mnemonic is correct before displaying the message box
// and triggering the click event.
public ref class MyMnemonicButton: public Button
{
protected:
bool ProcessMnemonic( char inputChar )
{
if ( CanSelect && IsMnemonic( inputChar, this->Text ) )
{
MessageBox::Show( "You've raised the click event "
"using the mnemonic." );
this->PerformClick();
return true;
} return false;
} }; // Declare the controls contained on the form.
public ref class Form1: public System::Windows::Forms::Form
{
private:
MyMnemonicButton^ button1; public private:
System::Windows::Forms::ListBox^ ListBox1; public:
Form1()
: Form()
{ // Set KeyPreview object to true to allow the form to process
// the key before the control with focus processes it.
this->KeyPreview = true; // Add a MyMnemonicButton.
button1 = gcnew MyMnemonicButton;
button1->Text = "&Click";
button1->Location = System::Drawing::Point( 100, 120 );
this->Controls->Add( button1 ); // Initialize a ListBox control and the form itself.
this->ListBox1 = gcnew System::Windows::Forms::ListBox;
this->SuspendLayout();
this->ListBox1->Location = System::Drawing::Point( 8, 8 );
this->ListBox1->Name = "ListBox1";
this->ListBox1->Size = System::Drawing::Size( 120, 95 );
this->ListBox1->TabIndex = 0;
this->ListBox1->Text = "Press a key";
this->ClientSize = System::Drawing::Size( 292, 266 );
this->Controls->Add( this->ListBox1 );
this->Name = "Form1";
this->Text = "Form1";
this->ResumeLayout( false ); // Associate the event-handling method with the
// KeyDown event.
this->KeyDown += gcnew KeyEventHandler( this, &Form1::Form1_KeyDown );
} private: // The form will handle all key events before the control with
// focus handles them. Show the keys pressed by adding the
// KeyCode object to ListBox1. Ensure the processing is passed
// to the control with focus by setting the KeyEventArg.Handled
// property to false.
void Form1_KeyDown( Object^ /*sender*/, KeyEventArgs^ e )
{
ListBox1->Items->Add( e->KeyCode );
e->Handled = false;
} }; [System::STAThreadAttribute]
int main()
{
Application::Run( gcnew Form1 );
}

Form.KeyPreview 属性的更多相关文章

  1. Form.KeyPreview 属性2

    在使用.Net Framework编写窗体应用程序的时候,有时有需要响应窗体的按键消息. 当窗体上没有任何其他控件的时候,窗体是可以直接响应这些消息的. 但是当窗体上有其他控件时,会发现窗体再也不会响 ...

  2. Form类的KeyPreview属性

    首先需要知道一个知识点,Form控件,Panel控件和GroupBox控件等容器类控件默认是不接收焦点的,而是负责管理容器中控件的焦点.当容器控件被选中时,默认把焦点传送至容器内Tab顺序为0的控件. ...

  3. 窗体的keypreview属性的作用是什么?(设置快捷键和钩子)

    如果把窗体的KeyPreview属性设为True,那么窗体将比其内的控件优先获得键盘事件的激活权.比如窗体Form1和其内的文本框Text1都准备响应KeyPress事件,那么以下代码将首先激活窗体的 ...

  4. form.elements属性

    form.elements属性与childNodes属性不同的是form.elements只返回的是表单元素组成的数组,包括input,textarea等

  5. 窗体的keyPreview属性

    如果把窗体的keyPreview属性设置为true,那么窗体将比其内的控件优先获得键盘事件的激活权.比如Form1和其内的文本框Text1都准备响应keyPress事件,那么以下代码将首先激活窗体的k ...

  6. Form 详细属性--2016年12月4日

    属性       名称 说明   AcceptButton 获取或设置当用户按 Enter 键时所单击的窗体上的按钮.   AccessibilityObject 获取分配给该控件的 Accessib ...

  7. HTML5 Web Form 新增属性和表单验证

    <form>标签的基本属性 method属性:指定浏览器向服务器传送数据的方式,可选: action属性:设置服务器接受和处理表单数据的URL: enctype属性:制定表单数据在发送到服 ...

  8. HTML form enctype 属性试验

    HTML form enctype http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1%E2%80%8D enctype= conte ...

  9. 表单form的属性,单行文本框、密码框、单选多选按钮

    基础表单结构: <body> <h1> <hr /> <form action="" name="myFrom" en ...

随机推荐

  1. 通过自关联替代开窗函数实现SQL优化

    数据库环境:SQL SERVER 2005 我们平时都是通过开窗析函数来取代自关联,减少表扫描,从而优化SQL.今天,反其道而行, 用自关联改写开窗函数.我们先来看一下SQL. 原SQL语句 SELE ...

  2. [转]js中几种实用的跨域方法原理详解

    转自:js中几种实用的跨域方法原理详解 - 无双 - 博客园 // // 这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同 ...

  3. Wooden Sticks

    Wooden Sticks Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  4. update语句

    [update cicm.cicmodt0702 set msgbody = :1 where msgid between :2 and :3         ] [update cicm.cicmo ...

  5. MyBatis入门教程(基于Mybatis3.2)

    MyBatis和Hibernate一样都是基于ORM的关系型数据库框架 ORM工具的基本思想: 1.从配置文件(通常是XML配置文件中)得到 sessionfactory. 2. 由sessionfa ...

  6. Yii 获取验证码与Yii常用的URL

    $this->createAction('captcha')->getVerifyCode(); //获取当前验证码的值 当前页面url  echo Yii::app()->requ ...

  7. 支付宝api教程,支付宝根据交易号自动充值

    最近公司要用php做一个网站支付宝自动充值的功能,具体就是客户把钱直接转到公司的支付宝账号里,然后在我们网站上填写上交易号,我们网站程序自动获取交易信息,自动给网站的账户充值. 我的具体想法就是利用支 ...

  8. 51nod1174区间中最大的数

    1174 区间中最大的数基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中, ...

  9. php练习1——计算器

    目标:输入两个数,计算两个数的和/差/积/商 程序如下:两个文件jiSuanQi.html和jiSuanQi.php    结果如下:  

  10. 使用curl获取网站的http的状态码

    发布:thebaby   来源:net     [大 中 小] 本文分享一例shell脚本,一个使用curl命令获取网站的httpd状态码的例子,有需要的朋友参考下.本文转自:http://www.j ...