I am attempting to move a PictureBox(picUser) up and down via the key press events. I am newer to C# and am able to do this via VB. As such I am confused as to what the problem is with the following code:
private void picUser_keyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
picUser.Top -= 10;
}
}
There is no "error" with the code, the picturebox just doesn't move.
A PictureBox has no KeyDown event. It has a PreviewKeyDown instead and requires the PictureBox to have the focus.
I would suggest to use the KeyDown of the form that host the PictureBox instead and use the same exact code:
public Form1()
{
InitializeComponent();
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
picUser.Top -= 10;
}
}
It's probably not working because picUser does not have the focus, and thus does not receive any key events.
If picUser has the focus, your code should work. However, a better way is probably to set your form's KeyPreview property to true, and then put your code above into the form's keyDown event (and set e.Handled = true as well, to prevent the key event from being passed on to whichever control does have the focus).
Related
I'm trying to convert VB.NET code to C#, from all the code I found this PerformClick issue and I don't know what happened.
My VB.Net code:
Private Sub FormLogin_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Enter
FlBtnLogin.PerformClick()
End Select
End Sub
The code worked, but when I tried to convert it to C#, it didn't work.
My C# code
private void FormLogin_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
FlBtnLogin.PerformClick();
}
}
Event Handler
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormLogin_KeyDown);
Any help and instruction would be appreciated, thanks
You would need to set the KeyPreview property of the form to true. If it is false, which it is by default, then the form will not raise keyboard events when a child control that can raise keyboard events has focus. Set it to true and the form will raise the events before the child control does. This is true in both C# and VB, so it's nothing specifically to do with the code you are converting.
As per #John which is the best option, this is another option if you want to have your events in a specific Control when the KeyPreview is OFF.
This code will only work if the form is on focus, but won't work if you put the focus/cursor on other controls.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter!");
}
}
While this event will be called when the focus is on a TextBox.
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter!");
}
}
So you must add the event on the control where you will likely press the Enter Key which is the TextBox for password.
I have a program containing multiple C# Forms TextBoxes. I've set up Hotkeys for the entire form activating certain functions. My problem is that my Hotkeys have been set onto the Form KeyDown event and they activate if I write something on a TextBox.
Example: One Hotkey might be I. Everytime I write the letter onto a textbox the Hotkey activates.
Alterior solutions and problems: I've thought about putting a Key in front of the Hotkey like CTRL+Hotkey, but these also present problems as CTRL+C is Windows Copy command etc. SHIFT is an UpperKey button.
Question: Can I prevent Hotkeys from activating when I am writing onto a TextBox without having to go through all of them in the form?
EDIT: Some code as requested. The button codes come from a stored XML file or the Hotkeys Form+Class (separate) where I've set up a window for them.
public Hotkeys hotkeysForm = new Hotkeys();
void Form1_KeyDown(object sender, KeyEventArgs e)
{
toggleInformation = hotkeysForm.toggleInformation;
if (e.Control && e.KeyCode == toggleInformation)
{
showInfo(true);
}
else if (e.KeyCode == toggleInformation)
{
if (!isInfoActive)
showInfo();
else
hideInfo();
}
}
You can disable hotkeys while texbox is an active control. Add the Enter and Leave events for all textboxes:
private void textBox_Enter(object sender, EventArgs e)
{
KeyPreview = false;
}
private void textBox_Leave(object sender, EventArgs e)
{
KeyPreview = true;
}
You should try this hack, if it could solve your problem,
Create a Extented TextBox and use it in your code. you can handle whether to write the pressed key in textbox or not in hotkeyPressed check.
public class ETextBox : System.Windows.Forms.TextBox
{
protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
if (hotKeyPressed) // this is the condition when you don't want to write in text.
{
//Do whatever you want to do in this case.
}
else
{
base.OnKeyDown(e);
}
}
}
Im trying to do like that: If I press the key "P" a messagebox will open in the screen.But I need to do it without a textbox or other tool, I want to do that direct in the form.
I tried:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.P)
{ MessageBox.Show("Key P pressed"); }
}
Try with the KeyPress Event of the form. It just works fine.
Assuming this is Winforms, on the form you're trying to catch the event on, make sure to set
Form1.KeyPreview = true;
KeyPreview ensures that keyboard events anywhere on the particular form (such as to a textbox with focus) will still count as a keyboard event for the form itself.
Good day! I want a button behave like the enter key and another to behave like the backspace key. Has anyone got any ideas how this can be done please?
Set the KeyPreview property on your form to true. And set AcceptButton to the button.
public Form1()
{
InitializeComponent();
// When this property is set to true, the form will receive all
// KeyPress, KeyDown, and KeyUp events.
this.KeyPreview = true;
//This property enables you to designate a default action to occur when the
//user presses the ENTER key in your application.
this.AcceptButton = button1;
this.KeyDown += Form1_KeyDown;
}
Just hande Form KeyDown event and check pressed keys for other keys than ENTER.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back)
{
BackSpaceButton_Click(null, null); // Or do whatever you want
}
}
For Enter Button : If you set your Form's AcceptButton property to one of the Buttons on the form, you'll get that behaviour by default.
Otherwise
Set the KeyPreview property to True on the form and handle its KeyDown event. You can do like this
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyDown += Form1_KeyDown;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
ProcessTabKey(true);//this will move textbox focus on Enter Key pressed.
}
else if (e.KeyCode == Keys.Back)
{
BackSpaceButton_Click(null,null);//this is a button event,it fire when you press Back key
}
}
I know this has been asked hundred of times, but I haven't been able to find a solution that helps me. I'm using a barcode scanner and I want to be able to get the keys that are typed using just the keydown events. For some reason, I can't use both keydown and keypress events (my keypress events won't run).
I need to be able to get the characters, including hyphens, uppercase letters and dots and also need to detect the enter key.
These are my listeners:
form.KeyDown += new KeyEventHandler(Input_KeyDown);
form.KeyPress += new KeyPressEventHandler(Input_KeyPress);
And these are my methods:
private void TimedOut(object sender, EventArgs e)
{
_barcode = "";
}
private void Input_KeyDown(object sender, KeyEventArgs e)
{
_timer.Stop();
_timer.Start();
if (e.KeyData == Keys.Enter)
{
if (!_barcode.Equals(""))
{
this.BarcodeScanned(_barcode, new EventArgs());
}
}
else
{
}
}
private void Input_KeyPress(object sender, KeyPressEventArgs e)
{
_timer.Stop();
_timer.Start();
_barcode += e.KeyChar;
}
Your code above works...on a blank form. However there are several things that can interfere with the key events, especially when there are other controls on the page. Make sure that
The AcceptButton property isn't set on the form (this will trap the Enter key)
That there are no controls on the form with TabStop set to true (might not be viable but give it a go)
That the form has focus when you're typing (unlikely given the description but check anyway)
That focus is not otherwise on any control in the form when typing, e.g., a TextBox
That no other controls are trying to process the KeyPress or KeyDown events and that no other custom events are configured/set anywhere else in your code
One thing I notice is that you are registering the events like so;
form.KeyDown += new KeyEventHandler(Input_KeyDown);
This implies that you are instantiating this form from another place and trying to get it to send its key events to the calling code. Are you sure that the form instance is persisted/saved to a private class level variable or some such?