Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project I write following key event
C#
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,
Posted

You need to hook's the Button's event handlers.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Feb-11 13:08pm    
Best answer for far, my 5.
No code no problem :-)
However, I post my improvements, please see.
--SA
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);

}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Feb-11 13:07pm    
Manas, I vote 5 because this is correct.
But I'm trying to fight against this syntax and broken naming conventions.
So, please see my improvements in my answer.
Thank you for understanding.
--SA
Manas Bhardwaj 22-Feb-11 3:46am    
Agree. And that is the reason I explicitly mentioned that Visual Studio generated this code. :)
Sergey Alexandrovich Kryukov 22-Feb-11 13:22pm    
I recommend avoiding it. A tactic validated on many projects.
--SA
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:

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


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:

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


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.
 
Share this answer
 
v2
Comments
Manas Bhardwaj 22-Feb-11 3:46am    
nice +5
Sergey Alexandrovich Kryukov 22-Feb-11 13:21pm    
Thank you.
--SA
You have to put code in to see if the key being pressed is the one you're interested in. Maybe you should review the info in MSDN:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keyup.aspx[^]
 
Share this answer
 
Comments
Nish Nishant 21-Feb-11 10:39am    
Hey John, I think he wants to get the events to fire for key presses on a button control but he currently has event handlers on the form's events which won't fire for child controls.
Yusuf 21-Feb-11 11:50am    
Many of questions gets interpreted in many ways. I wish questioners put a little effort clarifying their issue. I know English is may not be their first language, but we all have seen clear questions in broken English. Aaarrrgggg!
Sergey Alexandrovich Kryukov 21-Feb-11 13:08pm    
Agree with Yusuf.
--SA
#realJSOP 21-Feb-11 11:53am    
I figured that when he said "button", he meant a keyboard key. I figured that's what he meant because English is obviously not his first language.
Nish Nishant 21-Feb-11 11:54am    
Well, yeah he may have meant it that way too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900