如何调用特定按钮单击上的keydown事件 [英] how to call keydown event on particular button click

查看:92
本文介绍了如何调用特定按钮单击上的keydown事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我编写了以下关键事件

In my project I write following key event

private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            LogWrite("KeyDown  - " + e.KeyData.ToString());
        }
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            LogWrite("KeyPress     - " + e.KeyChar);
        }
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
             LogWrite("KeyUp        - " + e.KeyData.ToString());
        }


我想将这三个事件称为特定的按钮事件.为此,我是什么?
谢谢,


I want to call this three events on particular button event.for this what do I am?
Thanks,

推荐答案

您需要挂钩Button的事件处理程序.
You need to hook''s the Button''s event handlers.


就像这样.
不知道您尝试了什么.但是,此完整的代码是由Visual Studio本身生成的.您要做的就是将控件(在这种情况下为按钮)挂接到相应的事件.

Like this.
Not sure what you tried. But this complete code is generated by Visual Studio itself. All you have to do is hook the control(button in this case) to the respective events.

public Form1()
{
	InitializeComponent();
}

private void button1_KeyDown(object sender, KeyEventArgs e)
{

}

private void button1_KeyPress(object sender, KeyPressEventArgs e)
{

}

private void button1_KeyUp(object sender, KeyEventArgs e)
{

}

private void InitializeComponent()
{
	this.button1 = new System.Windows.Forms.Button();
	this.SuspendLayout();
	// 
	// button1
	// 
	this.button1.Location = new System.Drawing.Point(52, 27);
	this.button1.Name = "button1";
	this.button1.Size = new System.Drawing.Size(75, 23);
	this.button1.TabIndex = 0;
	this.button1.Text = "button1";
	this.button1.UseVisualStyleBackColor = true;
	this.button1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.button1_KeyDown);
	this.button1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.button1_KeyPress);
	this.button1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.button1_KeyUp);
	// 
	// Form1
	// 
	this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
	this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
	this.ClientSize = new System.Drawing.Size(292, 273);
	this.Controls.Add(this.button1);
	this.Name = "Form1";
	this.Text = "Form1";
	this.ResumeLayout(false);

}


看Manas的正确答案.

我只想添加一个改进.
为事件设置处理程序的传统"形式非常糟糕. Microsoft Designer生成的代码也是错误的,并且基于过时的语法.一件坏事是Microsoft违反了自己的命名规则(不建议使用下划线字符).我提倡很快就要付诸实践的不同方法.您根本不应该单击Designer中的事件.创建它们并不能真正加速您的工作,但会使代码的支持性大大降低.

更现代,更受支持的方法基于匿名代表.在显示表单之前,请设置事件.我通常在表单的构造函数中执行此操作,在它的末尾:

Look at the correct answer by Manas.

I only want to add an improvement.
The "traditional" form of setting up a handler for the event is pretty bad. The code generated by Microsoft Designer is also bad and based on obsolete syntax. One bad thing is Microsoft violates its own naming rules (the do not recommend underscore character). I promote different approach which pays of soon. You should not click on events in Designer at all. Creation of them does not really accelerate your work but makes the code much less supportable.

More modern and supportable approach is based on anonymous delegates. Before showing you form, set up the events. I usually do it in form''s constructor, at the end of it:

MyButton.KeyDown += delegate(object sender, KeyEventArgs eventArgs) {
    LogWrite(string.Format("KeyDown  - {0}", e.KeyData));
    //I also added Format, which is better then "+"
}



如您所见,它看起来更简单,更容易实现.通常,您不使用处理程序的一个或两个参数.因此,通过使用方法主体,您可以使用参数方便的方式调用其他任何方法,而不是事件声明:).此代码也更受支持,完全在您的控制之下.

现在,您很有可能正在使用C#v.3或更高版本.比您可以使用委托的lambda形式进一步简化代码:



As you can see, it looks more simple and easier to right. Very often, you don''t use one or both parameters of the handler. So, from withing the method body you can call any other method with parameters convenient for you, not for the event declaration :). This code is also more supportable and all under your control.

Now, chances are you''re using C# v.3 or later. Than you can further simplify the code using lambda form of the delegate:

MyButton.KeyDown += (sender, eventArgs) => {
    LogWrite(string.Format("KeyDown  - {0}", e.KeyData));
    //I also added Format, which is better then "+"
}



在这种形式下,您甚至不需要参数类型,因为它们是从事件类型自动推断出来的.称为类型推断.

我认为这种代码风格也更接近您在问题"中的含义.
就这样!

祝你好运,

—SA
P.S.:我看到问题的表达方式上的矛盾引起了事件ButtonForm的来源引起了一些争论.
您可能的意思是Button,但是代码示例与Form有关.
KeyDown使用的技术与KeyDown完全相同,因为FormConrtol.重要的是什么事件以什么类型声明.



In this form, you don''t even need parameter types as they are automatically inferred from the event type. It''s called type inference.

I think this style of code is also closer to what you meant in your Question.
That''s it!

Good luck,

—SA
P.S.: I see the contradiction in the formulation of the question caused some argument on the source of event Button or Form.
You probably mean Button but code sample was about the Form.
The technique to be used is exactly the same for KeyDown, as Form is Conrtol. All what matters is what events are declared in what type.


这篇关于如何调用特定按钮单击上的keydown事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆