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. JavaScript学习笔记(8)——JavaScript语法之运算符

    一. 算术运算符: 运算符 描述 例子 结果 + 加 x=y+2 x=7 - 减 x=y-2 x=3 * 乘 x=y*2 x=10 / 除 x=y/2 x=2.5 % 求余数 (保留整数) x=y%2 ...

  2. [MAXscript Tool]TimeSlider v1.3

    一个简单的小工具,方便在MAX里面快速的修改帧速率,像maya一样.具体看视频演示.

  3. oninput和onpropertychange

    时常会有监听输入框输入的场景,比如新浪微博的发微博输入框: 还有边输入边提示: 以及form表单边输入边验证,当内容为空提示或者改变输入框的样式达到提示效果. 在IE中是onpropertychang ...

  4. From MSI to WiX, Part 2 - ARP support, by Alex Shevchuk

    Following content is directly reprinted from From MSI to WiX, Part 2 - ARP support Author: Alex Shev ...

  5. jQuery事件绑定和委托

    可以用多种方法实现,on()  . bind()  . live()  . delegate() ,还有one(). 有时我们可能会像下面这样绑定一个事件: $("#div1"). ...

  6. jtemplate使用笔记

    最近的项目中用到了jtemplate, 它是客户端基于javascript的模板引擎,绑定的数据为json对象.以前我在页面上显示数据列表时最喜欢用Repeater控件了,因为它相对与其它几个服务端控 ...

  7. Keil 4 与Proteus 7.8联调

    实验环境: windows 8.1 pro with Keil 4 and Proteus 7.8 both cracked. 步骤: 下载联调工具Vdmagdi,安装. keil下Option/De ...

  8. (转载)delphi checklistbox用法

    delphi checklistbox用法 在Delphi中checklistbox中高亮选中(不论是否Checked)能够进行操作么?删除,上下移动等等 删除:CheckListBox.Delete ...

  9. python 调用第三方库压缩png或者转换成webp

    因为工作需要去研究了下png的压缩,发现转换成webp可以小很多,但是webp在手机上的解码速度比png的解码速度慢很多.出于进几年手机设备的处理器的性能也不错了,所以准备两套方案. 在网上搜索了一些 ...

  10. Hive 自定义函数(转)

    Hive是一种构建在Hadoop上的数据仓库,Hive把SQL查询转换为一系列在Hadoop集群中运行的MapReduce作业,是MapReduce更高层次的抽象,不用编写具体的MapReduce方法 ...