Why KeyDown event wont fire? - c#

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

Related

How do i set the label and display an integer value from a TextBox to the label when the enter key is pressed in wpf c#?

Here's the code. I have modified it bit?
public partial class MainWindow : Window
{
int coursenumber;
public MainWindow()
{
InitializeComponent();
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
bool res = int.TryParse(textBox.Text, out coursenumber);
if (res == true)
{
//success
}
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if the key that was pressed was the Enter key.
if (e.Key == Key.Enter)
{
//logic goes here
}
}
Register to the Key Down event and check
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
// your logic here
}
}
What you're looking for is the KeyDown event. Subscribe to this event on your TextBox like this:
<TextBox KeyDown="textBox_KeyDown" ... />
And your event handler will look something like this:
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if the key that was pressed was the Enter key.
if (e.Key == Key.Enter)
{
bool res = int.TryParse ...
//The rest of your logic here.
}
}
Thanks everyone, i was able to get it working from your responses. here's the modification
public partial class MainWindow : Window
{
int coursenumber;
public MainWindow()
{
InitializeComponent();
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
bool res = int.TryParse(textBox.Text, out coursenumber);
if (res == true)
{
//success
}
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if the key that was pressed was the Enter key.
if (e.Key == Key.Enter)
{
label2.Content = coursenumber;
}
}

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

Form1_KeyDown calling button1_Click

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.

ProcessTabKey(true) not working on c# splitcontainer at keydown event of windows form

i am trying to use ProcessTabKey() method not working on c# splitcontainer at keydown event of windows form
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.BringToFront();
this.Focus();
this.KeyPreview = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
this.ProcessTabKey(true);
}
}
Your code is correct. Ensure that all the controls you want to select have their TabStop property set to true.
Use SendKeys.send("{tab}"); instead of this.ProcessTabKey(true);

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