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.
Related
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;
}
}
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.
I am building a robot car with a Raspberry Pi 3, using C#. I am able to control it with either a button on the user interface or with a keyboard.
The problem with the buttons is that when I click a button, the function won't stop until I press another button. I either need a way of checking while the button is being clicked or one what checks when the button is unclicked.
I have looked through other posts on button holding, but can't really get anything to work.
In the case of key presses, it works correctly because it stops moving when I stop pressing the button. I just add this to the in the XAML page. This is due to the KeyUp bit.
KeyDown="Background_KeyDown_1" KeyUp="Background_KeyUp_1">
This is the C# code.
//Event for when key is pressed
private void Background_KeyDown_1(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
KeyDirection(e.Key);
}
//Event when key is unpressed, emulates 'Enter' which represents stop.
private void Background_KeyUp_1(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
KeyDirection(Windows.System.VirtualKey.Enter);
}
//This guy is called when a key is pressed or unpressed.
public void KeyDirection(Windows.System.VirtualKey vkey)
{
//Detects which key has been pressed, then calls Direction();
switch (vkey)
{
case Windows.System.VirtualKey.W:
Direction("forwards");
break;
case Windows.System.VirtualKey.A:
Direction("left");
break;
case Windows.System.VirtualKey.S:
Direction("backwards");
break;
case Windows.System.VirtualKey.D:
Direction("right");
break;
case Windows.System.VirtualKey.Enter:
Direction("stop");
break;
default:
Direction("stop");
break;
}
}
As of current, I am unable to find a way of getting this to work. I was hoping someone who has got it to work previously could show me how they did it.
Cheers, Callum :)
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;
}
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;
}