show button being pressed when keyboard key is pressed - c#

well i'm new to c# and i'm using Visual studio 2012.
i'm trying to make a checkbox with the appearance of a button.
when a keyboard key is pressed i would like for it to show the same way when the mouse clicks a button. If i hit the A key the button/checkbox is pressed down and if A key is hit again the button/checkbox is raised up.
i got this to work with just the button1 but i can't get it to show the pressing of the button by using this code
switch (e.KeyCode)
{
case Keys.D1:
// Simulate clicks on button1
ShowPictureButton.PerformClick();
break;
default:
break;
}
i figured i can use a checkbox so it will stay down when pressed.

If you are saying that you are using a Checkbox with it's Appearance Property set to Button you could do something like this
switch (e.KeyCode)
{
case Keys.D1:
// Simulate clicks on CheckBox's
ShowPictureButton.Checked = !ShowPictureButton.Checked;
break;
default:
break;
}

first set KeyPreview in your form properties to true
add events in your form (keypress and mouseclick) and then write your code, like this:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
checkBox2.Checked = !checkBox2.Checked;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
checkBox1.Visible = !checkBox1.Visible;
}

Related

How to get a clickable button to also activate upon a keypress? (Windows Form C#)

I have a regular button called Btn_Down that activates when clicked, but I also want it to activate when the 'S' key is pressed. Can anyone help me out with this?
Subscribe to KeyDown event of form control which contains your button and add following code
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyData)
{
case Keys.S:
button1_Click(this, EventArgs.Empty);
//Or
//button1.PerformClick();
break;
}
}
Set form's KeyPreview property to true. These settings should give the effect you want.
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case 'S':
case 's':
button1_Click(this, EventArgs.Empty);
//Or
//button1.PerformClick();
break;
}
}

MouseDown event for multiple buttons

Basically, I've named my mousedown event to be LBTNDOWN, and I've linked the event together with 3 other buttons. I want to make a switch case for each button when it's pressed down, it does something. And I'll also be making a separate mouseup event that does something when the mouse is released, but I'm already stuck at mousedown.
I've tried almost everything and researched so many solutions however it doesn't work! I'm desperate as I have to submit this project tomorrow omg!
private void LBTNDOWN(object sender, MouseEventArgs e)
{
///Code
switch (e.Button)
{
case btnCFL:
txtbox1.text = '1';
break;
case btnCFR:
txtbox1.text = '2';
break;
}
}
I expected the output to be for example when button CFL is pressed down, textbox1 will change to 1, then when button CFR is pressed down, textbox1 will change to 2.
I don't think "switch (e.Button)" is well supported.
Please try below code:
private void LBTNDOWN(object sender, MouseEventArgs e){
///Code
switch ((sender as Button).Text){
case "CFL":
txtbox1.text = '1';
break;
case "CFR":
txtbox1.text = '2';
break;
default:
Console.WriteLine("Default case should be included as a good habit");
break;
}
}
If text cannot distinguish these buttons, you may use tag property of button instead.

C# Detect when key gets pressed and trigger a button

So I'm trying to make a simple calculator. The user can only input the numbers by the buttons on the form or by the numpad. This is the code I have:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
string key = "";
switch (e.KeyCode)
{
case (Keys.NumPad1):
key = "1";
break;
case (Keys.NumPad2):
key = "2";
break;
default:
break;
}
txt_string.Text = txt_string.Text + key;
}
If I make a breakpoint on the KeyDown function and press the Numpad keys (and every other keys) the program doesnt even comes to that breakpoint.
Do I have to change something on my Form to detect the Keys?
You'll need to set KeyPreview to true (property on the form). Also, I would advise against trying to debug the behaviour - because you may affect the behaviour you're testing (Debug.WriteLine()) is your friend here.
Just to point out that many keyboard doesnt have numpad. You can check if the key is a integer.
void Form1_KeyDown(object sender, KeyPressEventArgs e)
{
if (char.IsDigit(e.KeyChar))
{
txt_string.Text += e.KeyChar;
}
}
This is more a Code Review than a solution though.

Handling arrow key events on a winform textbox without overriding

I have a situation where I'm provided with a WinForms TextBox instance which I want to attach autocomplete functionality to.
I've got the autocomplete (string matching + dropdown) all figured out and it works reliable so far.
What is the ability to navigate the dropdown with the keyboard (as is the norm with this sort of UI).
The natural solution would be to handle KeyDown (or somesuch) event for the textbox and moving the selection in the dropdown accordingly.
However, it happens that to do this, you need to override the IsInputKey() event to allow capture of arrow key events. The alternative is to override ProcessCmdKey() and handle the event there. The problem with these two is that I cannot override anything since I can't replace the textbox instance.
Edit: Let's assume I have the code below:
void _textBox_KeyDown(object sender, KeyEventArgs e)
{
if (_dropdown.Visible)
{
// TODO The stuff below fails because we need to either handle ProcessCmdKey or override IsInputKey
switch (e.KeyCode)
{
case Keys.Tab:
{
// click selected item
_dropdown.Items[GetSelectedItemIndex()].PerformClick();
break;
}
case Keys.Down:
{
// select next (or first) item
int i = GetSelectedItemIndex() + 1;
if (i >= _dropdown.Items.Count) i = 0;
_dropdown.Items[i].Select();
break;
}
case Keys.Up:
{
// select previous (or last) item
int i = GetSelectedItemIndex() - 1;
if (i < 0) i = _dropdown.Items.Count - 1;
_dropdown.Items[i].Select();
break;
}
}
}
}
Them problem with the code above is that it is never called. The event is never triggered for arrow keys. More info: Up, Down, Left and Right arrow keys do not trigger KeyDown event
I hope i haven't missunderstood you, but is this a solution:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
// Place logic for textbox here
}
}
I'd use a KeyDown event on the form and then compare the keycode with the Keys.Down keycode
Not working
see here: Up, Down, Left and Right arrow keys do not trigger KeyDown event
I may not be understanding your question entirely, but wouldn't an approach like this work?
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
comboBox1.Text = //results of your matching algorithm.
}
private void textBox1_Validated(object sender, EventArgs e)
{
textBox1.Text = (string) comboBox1.Text;
}

Are some keyboards more loquacious than others?

The lead developer says that when he uses my app, his keyboard beeps when he moves between TextBoxes on the TableLayoutPanel via the directional arrow keys.
However, I hear no such aural activity.
Here's my code:
// Had to intercept Up and Down arrows from Windows
private void textBoxPlatypi_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
TextBox tb = (TextBox)sender;
if (e.KeyCode.Equals(Keys.Up)) {
SetFocusOneRowUp(tb.Name);
return;
}
if (e.KeyCode.Equals(Keys.Down)) {
SetFocusOneRowDown(tb.Name);
return;
}
}
private void textBoxPlatypi_KeyDown(object sender, KeyEventArgs e) {
TextBox tb = (TextBox)sender;
if (e.KeyCode.Equals(Keys.Left)) {
SetFocusOneColumnBack(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Right)) {
SetFocusOneColumnForward(tb.Name);
e.Handled = true;
return;
}
}
..He thought maybe I needed "e.Handled" but that is not available in the PreviewKeyDown event.
Is there a way to suppress the beeping (which apparently occurs only with certain keyboards or specific setups (he's using Windows7, I'm on XP still))?
UPDATE
I've got this code now:
private void textBoxPlatypus1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
switch (e.KeyCode) {
case Keys.Down:
case Keys.Up:
e.IsInputKey = true;
break;
}
}
private void textBoxPlatypus1_KeyDown(object sender, KeyEventArgs e) {
TextBox tb = (TextBox)sender;
if (e.KeyCode.Equals(Keys.Up)) {
SetFocusOneRowUp(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Down)) {
SetFocusOneRowDown(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Left)) {
SetFocusOneColumnBack(tb.Name);
e.Handled = true;
return;
}
if (e.KeyCode.Equals(Keys.Right)) {
SetFocusOneColumnForward(tb.Name);
e.Handled = true;
return;
}
}
...but he still hears the beeping (I don't).
He's in Alaska and using Windows 7; I'm in California and using XP. I don't know if some combination/mismatch there is the problem...
UPDATED AGAIN
I know this may be shocking to some, but the Alaska/California disconnection has nothing to do with it. I'm now hearing the beeps, too, and it's not from the arrow keys. It's when a value is entered in a TextBox and then, if that text box already has a character, focus is moved to the next textBox and the value is entered there (this is my code that causes this to happen). But the irritating beeping seems to be random - I haven't figured out the pattern for when it beeps (sometimes it does, sometimes it doesn't)...has anybody ever run across anything like that, or, better yet, know how to suppress the beep? All I'm doing is pressing either the "1" or the "2" key above the keyboard.
There is no way in the PreviewKeyDownEvent to Handle / Supress a KeyEvent like there is in the normal KeyDown Event. What the documentation suggests is to set the PreviewKeyDownEventArgs.IsInputKey property to true in order to handle key presses that are not available normally in the KeyDown Event.
From above Link, they are using a button as an example:
Some key presses, such as the TAB, RETURN, ESC, and arrow keys, are typically ignored by some controls because they are not considered input key presses... By handling the PreviewKeyDown event for a Button and setting the IsInputKey property to true, you can raise the KeyDown event when the arrow keys are pressed. However, if you handle the arrow keys, the focus will no longer move to the previous or next control.
Try this:
e.SuppressKeyPress = true;

Categories