如何调用特定按钮单击上的keydown事件

如何调用特定按钮单击上的keydown事件

本文介绍了如何调用特定按钮单击上的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,

推荐答案


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);

}


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事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:40