Form.KeyPreview 属性
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。
可以使用此属性处理应用程序中的大部分击键事件,并可以处理击键事件或调用适当的控件来处理击键事件。例如,当应用程序使用功能键时,可能希望在窗体级别处理这些击键,而不是为可能接收击键事件的每个控件编写代码。
![]() |
---|
如果窗体没有可见或启用的控件,则该窗体自动接收所有键盘事件。 |
![]() |
---|
可以对窗体上的控件进行编程,以取消它所接收的任何击键。由于控件从不向窗体发送这些击键,因此无论 KeyPreview 为何种设置,窗体永远都无法看到它们。 |
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 属性的更多相关文章
- Form.KeyPreview 属性2
在使用.Net Framework编写窗体应用程序的时候,有时有需要响应窗体的按键消息. 当窗体上没有任何其他控件的时候,窗体是可以直接响应这些消息的. 但是当窗体上有其他控件时,会发现窗体再也不会响 ...
- Form类的KeyPreview属性
首先需要知道一个知识点,Form控件,Panel控件和GroupBox控件等容器类控件默认是不接收焦点的,而是负责管理容器中控件的焦点.当容器控件被选中时,默认把焦点传送至容器内Tab顺序为0的控件. ...
- 窗体的keypreview属性的作用是什么?(设置快捷键和钩子)
如果把窗体的KeyPreview属性设为True,那么窗体将比其内的控件优先获得键盘事件的激活权.比如窗体Form1和其内的文本框Text1都准备响应KeyPress事件,那么以下代码将首先激活窗体的 ...
- form.elements属性
form.elements属性与childNodes属性不同的是form.elements只返回的是表单元素组成的数组,包括input,textarea等
- 窗体的keyPreview属性
如果把窗体的keyPreview属性设置为true,那么窗体将比其内的控件优先获得键盘事件的激活权.比如Form1和其内的文本框Text1都准备响应keyPress事件,那么以下代码将首先激活窗体的k ...
- Form 详细属性--2016年12月4日
属性 名称 说明 AcceptButton 获取或设置当用户按 Enter 键时所单击的窗体上的按钮. AccessibilityObject 获取分配给该控件的 Accessib ...
- HTML5 Web Form 新增属性和表单验证
<form>标签的基本属性 method属性:指定浏览器向服务器传送数据的方式,可选: action属性:设置服务器接受和处理表单数据的URL: enctype属性:制定表单数据在发送到服 ...
- HTML form enctype 属性试验
HTML form enctype http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1%E2%80%8D enctype= conte ...
- 表单form的属性,单行文本框、密码框、单选多选按钮
基础表单结构: <body> <h1> <hr /> <form action="" name="myFrom" en ...
随机推荐
- ERwin 连接 mysql
1. install mysql connector; 2. run odbc connection management (c:\windows\syswow64\odbcad32.exe); 3. ...
- Mysql主从配置+读写分离
Mysql主从配置+读写分离 MySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具.因此,我们首先要在系统中源码编译安装cmake工具. ...
- C# 匿名类型
c#3.0引入匿名类型,其由编译器动态生成而非显式定义. using System;using System.Collections.Generic;using System.Linq;using S ...
- Android笔记之adb命令应用实例1(手机端与PC端socket通讯上)
Android端的代码: 布局文件:activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/ ...
- 学习C++ Primer 的个人理解(零)
由于自己看书之后经常容易忘记细节,所以写在博客上让我自己的思路更加清晰一些. 我的博客仅仅只是将书中的内容概括一下,有一些不好理解的部分我会用我自己理解的方式使用大白话写出来. 仅此而已.
- iOS 构造方法
构造方法:用于在对象创建出来的时候为对象的成员变量或者属性赋值 1.构造方法就是初始化对象的方法,它属于对象方法.构造方法分为系统自带和自定义构造方法. 如何创建自定义构造方法: 属于对象方法那么以 ...
- 简单改造 starling 中的 AssetManager 让其更适合 批次加载纹理
API文档参考:http://doc.starling-framework.org/core/starling/utils/AssetManager.html 项目想以不改动starling的情况下对 ...
- 超过130个你需要了解的vim命令
基础 :e filename Open filename for edition :w Save file :q Exit Vim :q! Quit without saving :x Write f ...
- Application.StartupPath同System.Environment.CurrentDirectory区别
System.Windows.Forms.Application.StartupPath:获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称. System.Environment.Curr ...
- Android开发第2篇 - Git插件安装
一.Eclipse上安装GIT插件EGit Eclipse的版本eclipse-java-helios-SR2-win32.zip(在Eclipse3.3版本找不到对应的 EGit插件,无法安装) E ...