How do i click button from keypress event - c#

I want to click a button when i press F1
this is my code
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
buttonGfxIn_Click.PerformClick();
}
}
But i get this error
that button name Is a 'method' which is not valid in the given context error

I have test with a example and it is working well. try it.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("button1 was clicked");
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
KeyEventArgs e = new KeyEventArgs(keyData);
if (e.KeyCode == Keys.F1)
{
button1_Click("", e);
}
return base.ProcessCmdKey(ref msg, keyData);
}
}

Related

Using Double Right Click in C#

I am fairly new with C# and I want to know if there is a way to implement a double right click in this event handler? Can anyone tell me how? thanks
private void pictureBox_MouseClick(object sender, MouseEventArgs e){
if(e.Button == MouseButtons.Left)
{
MessageBox.Show("A");
}
else if(e.Button = MouseButtons.Right)
{
MessageBox.Show("B");
}
else if(e.Button = MouseDoubleClick.Right) <--how to fix this?
{
MessageBox.Show("C");
}
else
{
MessageBox.Show("D");
}
}
You can use the ClickCount propery of MouseRightButtonDown event.
private void OnMouseDownClickCount(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 1)
{
// one click.
}
if (e.ClickCount == 2)
{
// two clicks.
}
}
Override the WndProc function and listen for WM_RBUTTONDBLCLK, which as can be seen on this pinvoke page is {0x0206}.
Then
public class RButton : Button
{
public delegate void MouseDoubleRightClick(object sender, MouseEventArgs e);
public event MouseDoubleRightClick DoubleRightClick;
protected override void WndProc(ref Message m)
{
const Int32 WM_RBUTTONDBLCLK = 0x0206;
if (m.Msg == WM_RBUTTONDBLCLK)
DoubleRightClick(this, null);
base.WndProc(ref m);
}
}

C# Windows Forms KeyDown event fires continiously [duplicate]

This question already has answers here:
how not to allow multiple keystokes received at one key press?
(3 answers)
Closed 6 years ago.
In a Windows forms application I'm trying to execute some function when I press a hotkey using Form's KeyDown event. The problem is that when I keep the hotkey pressed the event fires continuously.
I just want to execute some function at the first time I press the button and another function when I release it.
Here is the code I used to accomplish this operation:
bool isPressed_Num7 = false;
bool isPressed_Num9 = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.NumPad7 && !isPressed_Num7)
{
isPressed_Num7 = true;
Console.WriteLine("Keydown 7");
}
if (e.KeyData == Keys.NumPad9 && isPressed_Num9)
{
isPressed_Num9 = true;
Console.WriteLine("Keydown 9");
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.NumPad7)
{
isPressed_Num7 = false;
Console.WriteLine("Keyup 7");
}
if (e.KeyData == Keys.NumPad9)
{
isPressed_Num9 = false;
Console.WriteLine("Keyup 9");
}
}
I'm going to use a bunch of hotkeys. So what I'm asking is there a better way to accomplish what I'm trying to do?
You could just a dictionary instead of one variable per key.
Dictionary<Keys, bool> keysPressed = new Dictionary<Keys, bool>();
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
keysPressed[e.KeyData] = false;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (!keysPressed.ContainsKey(e.KeyData) || !keysPressed[e.KeyData]) {
Console.WriteLine(e.KeyData);
keysPressed[e.KeyData] = true;
}
}
Try this:
bool isPressed_Num7 = false;
bool isPressed_Num9 = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyData == Keys.NumPad7) && (!isPressed_Num7))
{
isPressed_Num7 = true;
Console.WriteLine("Keydown 7");
}
if ((e.KeyData == Keys.NumPad9) && (!isPressed_Num9))
{
isPressed_Num9 = true;
Console.WriteLine("Keydown 9");
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if ((e.KeyData == Keys.NumPad7)&&(isPressed_Num7==true))
{
isPressed_Num7 = false;
Console.WriteLine("Keyup 7");
}
if ((e.KeyData == Keys.NumPad9)&&(isPressed_Num9==true))
{
isPressed_Num9 = false;
Console.WriteLine("Keyup 9");
}
}
This solution uses the recommended way to handle keyboard shortcuts according to Microsoft. And allows you to assign action to invoke when pressing a key combination.
public partial class Form1 : Form
{
private readonly IDictionary<Keys, Action> _operationsMap = new Dictionary<Keys, Action>();
public Form1()
{
InitializeComponent();
RegisterKeyShortcuts();
}
private void RegisterKeyShortcuts()
{
_operationsMap.Add(Keys.Control | Keys.F, WhenPressingF);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (_operationsMap.ContainsKey(keyData))
{
_operationsMap[keyData].Invoke();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
protected void WhenPressingF()
{
MessageBox.Show("What the Ctrl+F?");
}
}

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

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

Associating keys to buttons on a Windows Form

I have to write a method on C# that associates a certain key (from the keyboard) to a specific button. For example, if I press A, the button that I created on a form application should appear like it is being pressed.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
}
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.MessageBox.Show("Ctrl-F was Pressed.");
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.F))
{
button1.PerformClick();
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
Note: To simulate click animation, make the Click event look like this:
private void button1_Click(object sender, EventArgs e)
{
button1.FlatStyle = FlatStyle.Flat;
System.Windows.Forms.MessageBox.Show("foo");
button1.FlatStyle = FlatStyle.Standard;
}
It's not perfect, but it works.

Categories