Form1_KeyDown calling button1_Click - c#

I have such piece of code:
private void button1_Click(object sender, EventArgs e) {
// Do something...
}
private void Form1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.A) {
// Call button1_Click.
}
}
How do I manage to call the Click event? What should I write instead of
// Call button1_Click.
Thank you!

Events are meant to be triggered not called. So you can trigger the event in your Form1_KeyDown event by performing the button click. I mean
private void Form1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.A) {
button1.PerformClick();
}
}

Since the button click event handler is just a method, you could just do:
private void Form1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyData == Keys.A) {
button1_Click(this, e);
}
}
However, it's often a better idea to move the logic into a separate method, and call it from both locations. By refactoring the logic within button1_Click into it's own method, you make it clear that you're calling that logic from either set of event handlers.

Related

Why KeyDown event wont fire?

My original intention in to make enter event for text box to run btnOK_Click event, but after several try I can't make it happen, so I tried another way and try KeyPress for any key but still didn't work, so I made these two simple code, but it still didn't work either;
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//enter key is down
//btnOK_Click(this, e);
System.Windows.Forms.MessageBox.Show("My message here");
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
//enter key is down
//btnOK_Click(this, e);
System.Windows.Forms.MessageBox.Show(((char)Keys.Return).ToString());
}
}
Any suggestion? I read some similar questions and they said to set the IsInputKey property to true but I can't find it anywhere. I use Visual Studio 2008
Two Options :
1) Use Key Up Event As
public void txt_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnOK_Click(sender, e); // or btn.PerformClick();
return;
}
}
2) Make BtnOK the AcceptButton of the form. (Note : this will be for all textboxes in your form)
this.AcceptButton = btnOK;
It seems to me that you are looking to something like this
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)//should be replaced with enter
{
button1.PerformClick();
}
}
NOTE: the code above is on the KeyDown instead of the KeyPress
This code should work, assuming you are using winforms
use Escape key instead of return key:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnOK_Click(sender, e);
MessageBox.Show("My message here");
}
else if (e.KeyCode == Keys.Escape)
{
btnOK_Click(sender, e);
MessageBox.Show(((char)Keys.Escape).ToString());
}
}
private void btnOK_Click(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
}
}
also you can check both keys in KeyDown event.
you can also use
btnOK.PerformClick();
instead of
btnOK_Click(sender, e);

Send keypress as parameters in function

I have a combobox, if the "Enter" key is pressed will do something. but i want to call this function manually but how i send "ENTER" key as parameter?
private void carga_todos(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
do something
}
im trying this:
carga_todos(null, ??????)
Don't call events manually like that.
Move your logic into a separate method, which you can call whenever you like:
private void carga_todos(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
DoSomething();
}
}
private void AnotherFunctionThatNeedsToDoSomethingToo()
{
DoSomething();
}
private void DoSomething()
{
// stuff to do
}
I agree with Grant Winney. However, if your requirements are to call the handler directly then the following should work:
KeyPressEventArgs kpea = new KeyPressEventArgs((char)Keys.Enter);
carga_todos(null, kpea);
Good luck
J

When are control events used in code?

When using the windows form app, you can create an event for example a picturebox.Click event. Inside this method all code will be ran when the butten is clicked.
Now inside another event, I can call the method Button1.click, but what is it used for? Can it be used for statements like this?
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(Button1.click == true) // If the button is clicked AND the mouse moves over the picturebox
{
//dance
}
}
No, you'll have to store a state. Where and how you do this can differ, but consider this:
var yourObject = new ObjectYouWishToControl();
...
private void Button1_Click(object sender, MouseEventArgs e)
{
yourObject.Button1WasClicked = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (yourObject.Button1WasClicked)
{
// do your thing
}
}

c# key press trigger button click

private void btnBrowserGo_Click(object sender, EventArgs e)
{
browser.Navigate(txtBrowserURL.Text);
}
The code above directs the browser to the URL address in the textbox. I would like this event to also occur if the user presses the ether key when typing the URL. I have this code (below) but don't know how I would call the above code
private void txtBrowserURL_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
}
}
You could just call the event directly using:
btnBrowserGo_Click(null, null);
However, it would be better if you didn't handle the navigation in the event itself and just called a method. That way, the method could be directly called from anywhere else in the class. This is especially useful if you have more logic in the method.
void NavigateBrowser()
{
browser.Navigate(txtBrowserURL.Text);
}
Then, from any event, you can just call the method.
private void txtBrowserURL_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
NavigateBrowser();
}
}
or
private void btnBrowserGo_Click(object sender, EventArgs e)
{
NavigateBrowser();
}
Try
btnBrowserGo_Click(null, null);
since you don't use last two parameters.
However, it might be good to extract 'logic' into some other method, and use that method from BOTH event handlers.
Your logic here is one-liner, but it might be more...
private void txtBrowserURL_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
btnBrowserGo_Click(btnBrowserGo,EventArgs.Empty);
}
}

Combo Box Enter Event WPF

I need to have an event fired whenever Enter is pressed inside of the combobox. This is a WPF C# 4.0 control, and I am unable to find a specific event handler to do this. I think I am missing something, as this seems like something that would be included. Is there preexisting code to accomplish this task?
I have also tried:
private void comboBox1_SelectionChanged(
object sender,
SelectionChangedEventArgs e)
{
if (e.Equals(Key.Enter))
{
// Do Something
}
}
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
// do stuff
}
else
{
// do stuff
}
}
private void comboBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// do stuff
}
}
or
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// do stuff
}
}
The difference being that KeyUp is when the key is released, KeyDown is when it is first pressed.

Categories