Associating keys to buttons on a Windows Form - c#

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.

Related

How to pass different values to textboxes by clicking the same button in C# window applications?

Hy
I have developed a calculator in C# (window applications). I have two textboxes on the form to get inputs from the user and combobox to select the operation to on the input numbers.
And my problem is that when I pressed the 1 button the code is passing 1 to both textboxes. I want to solve this problem like when i click on the textbox1 and then i press the button 1 the code will pass value to only textbox1. And when i click the textbox2 and then by pressing the button 1 the code will have to pass value only to textbox2.
Thanks in advance for answering.
Simply add a Bool to determine which textbox the currently selected focus is on.
Add focus event to textbox:
private void textBox1_Enter(object sender, EventArgs e)
{
Flag = true;
}
private void textBox2_Enter(object sender, EventArgs e)
{
Flag = false;
}
Code:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static bool Flag = true;
private void button1_Click(object sender, EventArgs e)
{
if (Flag)
{
textBox1.Text += "1";
}
else
{
textBox2.Text += "1";
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
Flag = true;
}
private void textBox2_Enter(object sender, EventArgs e)
{
Flag = false;
}
}
}
Output:

How do i click button from keypress event

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

Wait for Input in User Control

I'm trying to implement a customized exit prompt in my WinForms. (I should not be using DialogBox)
I have a User Control Object placed in my main form that is invisible and disabled by default. Clicking in a certain button I have placed on the form shows and enables the object, disabling everything in my form except the User Control.
private void btn_close_Click(object sender, EventArgs e) {
prompt1.Visible = true;
prompt1.Enabled = true;
disableControls();
//Wait for a button to be pressed in prompt1
//Make an action based on a button pressed.
//closeApp returns a boolean
if (!prompt1.closeApp)
{
prompt1.Visible = false;
prompt1.Enabled = false;
enableControls();
}
else
{
Application.Exit();
}
}
Here's my code at the prompt object:
public partial class Prompt : UserControl
{
bool exit;
public bool closeApp
{
get{return exit;}
}
public Prompt()
{
InitializeComponent();
}
private void btn_yes_Click(object sender, EventArgs e)
{
exit = true;
}
private void btn_no_Click(object sender, EventArgs e)
{
exit = false;
this.Hide();
}
}
What I want to do is wait for a button to be pressed in my prompt object before proceeding to the next line in the btn_close_Click().
What should I do? Is there a better way to implement this?
Add events to your usercontrol then handle those events on your main form.
In your usercontrol:
public event EventHandler<EventArgs> ExitCancelled;
public event EventHandler<EventArgs> ExitApplication;
private void btn_yes_Click(object sender, EventArgs e)
{
ExitApplication?.Invoke(this, EventArgs.Empty);
}
private void btn_no_Click(object sender, EventArgs e)
{
ExitCancelled?.Invoke(this, EventArgs.Empty);
}
Handle the events on your form:
public void prompt1_ExitApplication(object sender, EventArgs e)
{
Application.Exit();
}
public void prompt1_ExitCancelled(object sender, EventArgs e)
{
prompt1.Hide();
enablecontrols();
}

Sendkeys event is not detected inside the application in windows form application

I am new to windows app development. I am developing a Windows Form Application where the layout is as follows:
There is one textbox and i have created the keyboard inside the application using SendKeys event.
Problem is that all other application on the system are able to detect the keys but the textbox inside the application is not able to detect the keys.
Basically app is having complete keyboard this is just one button press code
What I have tried:
public partial class Form1 : Form
{
Control focusedC;
protected override CreateParams CreateParams
{
get
{
CreateParams param = base.CreateParams;
param.ExStyle |= 0x08000000;
return param;
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
TopMost = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape) {
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
TopMost = false;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
//checkbox is for CapsLock Key
}
private void button14_Click(object sender, EventArgs e)
{
if (checkBox1.Checked && focusedC != null)
{
focusedC.Focus();
SendKeys.Send("Q");
}
else if(focusedC != null)
{
focusedC.Focus();
SendKeys.Send("q");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
focusedC = sender as TextBox;
}
}
Of cource it doesn't work on your window. You set the WS_EX_NOACTIVATE style! It works on other windows but not yours, obviously. If you want it to work on your textbox remove or comment this line
param.ExStyle |= 0x08000000;
and it will work fine in your app window not others:
private void button14_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox1.Focus();
SendKeys.Send("Q");
}
else
{
textBox1.Focus();
SendKeys.Send("q");
}
}
For WPF applications, you have to use SendKeys.SendWait() method.
SendKeys.SendWait("Q")
SendKeys.Send() will work for WinForm Applications.
Another option is to use WinAPI instead of SendKeys. More information here
Edit 1
Control focusedC;
//Enter event handler for your TextBox
private void textBox1_TextChanged(object sender, EventArgs e)
{
focusedC = sender as TextBox;
}
//Click event handler
private void button14_Click(object sender, EventArgs e)
{
if (focusedC != null)
{
focusedC.Focus();
SendKeys.Send("Q");
}
}
Edit 2: Using WinAPI
[DllImport("user32.dll")]
static extern void SendInput(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
public static void PressKey(byte keyCode)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
SendInput((byte)keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
SendInput((byte)keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
Use by calling the PressKey function and Keycodes can be found here

create a control assigner menu for games

I am trying to build a windows application using WinForms and C#, in one of the forms i want the user to be able to assign keys for each movement (i.e. left , right , up ,down motion etc.). That is something similar to
On the left hand side column the moves will be listed and the user should be able to assign a key for every move. I am very new to windows forms and am unable to figure out what control to use for the left hand side things, i tried using buttons with KeyDown event but in this the event does not trigger for enter/return key, for rest of the keys it works fine. So what control along with what event should be used so that the user can assign any key of his choice for any motion/control.
EDIT: this was the initial code.
namespace ControllerWinServe
{
public partial class Form2 : Form
{
static string[] array = new string[6];
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button_d_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
}
private void button_u_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Form.KeyPress: '" + e.KeyCode.ToString() + "' pressed.");
}
private void button_d_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Form.KeyPress: b2 '" +e.KeyCode.ToString() + "'pressed.");
}
}
}
AFTER Trying to use user17753 's suggestion.
namespace ControllerWinServe
{
public class EnterTextBox : TextBox
{
protected override bool IsInputKey(Keys key)
{
if (key == Keys.Enter)
return true;
return base.IsInputKey(key);
}
}
public partial class Form2 : Form
{
static string[] array = new string[6];
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void button_d_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
}
private void button_u_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Form.KeyPress: '" + e.KeyCode.ToString() + "' pressed.");
}
private void button_d_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("Form.KeyPress: b2 '" +e.KeyCode.ToString() + "'pressed.");
}
}
}
If you're talking about hitting enter in a TextBox it isn't triggered by default. You can create a new one called for example EnterTextBox that is derived from TextBox that overrides IsInputKey to allow enter to trigger the event.
One such implementation could be:
public class EnterTextBox : TextBox
{
protected override bool IsInputKey(Keys key)
{
if (key == Keys.Enter)
return true;
return base.IsInputKey(key);
}
}
With this class in your project's namespace you'll be able to add EnterTextBox from the Toolbox under your project's namespace category.
Then you can add a method that is triggered by the KeyDown event on the EnterTextBox such as this:
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
//stuff to do after enter is pressed
}
}

Categories