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 ...
随机推荐
- Linq 查询 与方法调用
通常,使用linq查询时需要一个实现IQueryable<T> 的查询对象 public class DataA<T> : IQueryable<T> {....} ...
- c#实现FTP上传
/// <summary> /// 上传文件 /// </summary> /// <param name="fileinfo">需要上传的文件 ...
- Java validator整理
Java validator整理 因为想对方法的入参和出参作简单的非空或者非空字符做校验,所以找了下相关的@NotNull注解 类 | 说明 --- | --- javax.validation.co ...
- Boot Petalinux Project Using a remote system
通过jtag实现在远程服务器端下载petalinux image到连接在本地PC的开发板上的方法. 具体连接方式为 比如Host的系统为Windows,Remote system为运载在远程服务器上的 ...
- VS2010 EntityFramework Database First
本文演练介绍如何使用实体框架进行 Database First 开发.通过 Database First,可以从现有数据库对模型进行反向工程处理.模型存储在一个 EDMX 文件(扩展名为 .edmx) ...
- jQuery手风琴广告展示插件
效果说明:当鼠标移动到已折叠广告的标题后,折叠当前已展开的广告,并同步展开相应的折叠广告.这种Accordion效果,看似简单,但因为存在动画同步的问题,不能简单地用两个animate()来实现.必须 ...
- 没用调用flush导致的数据保存丢失
在将字符串保存到文件时,我们采有下面的写法,大部分情况下,都可以直接将数据保存到文件中, using (var fs = System.IO.File.Create(path)) { var sw = ...
- PHPCMS二次开发教程(转)
转自:http://www.cnblogs.com/semcoding/p/3347600.html PHPCMS V9 结构设计 根目录 |–api 结构文件目录 |–caches 缓存文件目录 ...
- /dev/null 2>&1 解释(转)
cmd >a 2>a 和 cmd >a 2>&1 为什么不同? cmd >a 2>a :stdout和stderr都直接送往文件 a ,a文件会被打开两遍, ...
- 【prism】前期准备
在网上下了prism框架源码,目前最新版本为4.1,其中包含的内容如下: 其中包含三类文件: 1.类似于Desktop only-Prism Library.bat的批处理文件,用来打开相应的Pris ...
注意