I am doing my graduation work and I need a little help.
So far I've done most of my work and now I need someone to help me with the shortcuts.
This is how my work looks like (for now):
I need to make shortcuts for those buttons so that user doesn't have to click on button with mouse. I wish to make it possible that when user presses F1 on the keybord - it's like he pressed button1 with mouse. F2 stands for button2 and so on.
Shortcuts as CTRL+ some key are also acceptable. I only need example how to make couple buttons, and i will make others :)
Thanks
Set Form1's KeyPreview-property to true in the designer or in the code:
this.KeyPreview = true;
Then add KeyUp-action to your form, which accepts all keys (even Delete etc):
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
button1ClickMethod();
}
}
Then separate your buttons' actions to individual methods, like button1ClickMethod(), and avoid using the Button1_Click(null, null);. Call this button1ClickMethod() in the KeyUp-event when a desired key is up.
To use combinations, you can use this:
// If CTRL and F1 were pressed
if (e.Control && e.KeyCode == Keys.F1)
{
MessageBox.Show("Shortcut CTRL + F1 was pressed!");
}
You can also check if the Shift was pressed at the same time with e.Shift-property, same way like the e.Control.
KeyPress event will help you
Sample code ;
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
//Call the event according to key
// if(e.KeyChar == "")
//btnSave_Click(null,null)
}
Related
I have a user authentication form with username and password textboxes.There is an okay button that fires the code to validate the credentials.
I want the same code to get executed when the user hits Enter key anywhere on the form.
So i register for the keypress event like this
this.KeyPress += UserLogin_KeyPress;
private void UserLogin_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
MessageBox.Show("enter");
}
}
This event is not triggered at all.What i'm i doing wrong?
Try setting the property keypreview to true and changing to keydown instead since KeyPress don't support e.Keycode:
private void UserLogin_KeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter");
}
}
Try this:
private void UserLogin_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("enter");
}
}
As mentioned, set form.KeyPreview = true so that most key events will be given to the form before the controls.
However, note that this doesn't always work, and some controls will still "steal" certain key presses anyway. For example, if a Button has focus, then an Enter keypress will be used by the button to activate it, before you get a chance to see it, and since it counts as having been "handled", you never get to see the keypress.
The workaround I use for this is to set focus to something that I know will not steal the enter key press, for key presses that occur leading up to the enter key press. So, if the pressed keys are <1><2><3><4><ENTER> then the 1-4 keys all set focus to something that is not a button (usually, the textbox where I am displaying the resulting text), and then when <ENTER> is pressed, it should make it to your form's event handler (as long as KeyPreview == true).
It's only looking at the form. Controls on the form also need to be wired in.
I want to use the combination Alt+X to close the current form, but there is also one condition: if the user presses a similar combination like Alt+X,C, it shouldn't work.
The form/control has an event called key press and key down. In the properties window input 'Form1_KeyPress'. This will run the method below
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == //KeyCode for alt && e.KeyChar == //KeyCode for x)
{
//run code here
}
}
I have a button for which I set the KeyPress event.
this.myButton.KeyPress += new KeyPressEventHandler(this.myButtonEvent_keypress);
private void myButtonEvent_keypress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return || e.KeyChar == (char)Keys.Space)
{
// do something
}
}
Now whenever the Space key is pressed, I get the event triggered. This part works fine.
But for some reason, the Enter key press is not triggering the KeyPress
event. Also Alt, Ctrl, Shift are not working.
How can I make the button receive Enter key press?
UPDATE:
I tried below too without any luck
this.myButton.MouseClick += new MouseEventHandler(myButton_Click);
When a Button has focus and you press Enter or Space the Click event raises.
So to handle Enter or Space it's enough to handle Click event and put the logic you need there.
So you just need to use button1.Click += button1_Click;
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Clicked!");
}
If you really want to know if Enter or Space was pressed, you can hanlde PreviewKeyDown event:
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if(e.KeyCode== Keys.Enter)
{
MessageBox.Show("Enter");
}
if (e.KeyCode == Keys.Space)
{
MessageBox.Show("Space");
}
}
Enter and space can be handled using click event.
this.myButton.Click += new EventHandler(myButton_Click);
The Button control in WinForms will eat Enter, Space, Tab, ESC, and a few other special key's press events. One method to intercept these events is to override Control.ProcessDialogKey. Or you can override IsDialogKey to say if a key should be handled as a special case.
A another option is to set KeyPreview = true on you parent Form. Then you can handle all KeyPress events at the Form level and use Form.ActiveControl if you need to see what control has Focus
Hello I am trying to match the button down visual (on the WinForm, the button boarder gets a little darker, indicating it is pressed) with a keydown event. First of all I need to detect a keydown for numbers only. Then when the key goes down, the corresponding number button on the form should look like it is depressed as well. Sorry if this is already been answered using differt jargon. I already know how to perform a button click with the keydown.
Make a test code on KeyDown event. Write down the keyboard codes you shall see from pressing 0 to 9. Then use those keyboard codes in your KeyDown's if statement
You can use a Checkbox and set the appearance to be Button. Then you can do something like this:
private void OnKeyDown(object sender, KeyEventArgs e)
{
//if key
checkBox1.Checked = true;
}
private void OnKeyUp(object sender, KeyEventArgs e)
{
//if key
checkBox1.Checked = false;
}
As far as the Keys, you can just use the KeyEventArgs.KeyCode
e.KeyCode == Keys.D0 || .. || e.KeyCode == Keys.D9
I have a listbox which displays the contents of an array. The array is populated with a list of results when my "go" button is pressed.
The go button is set as the AcceptButton on the form properties so pressing the Enter key anywhere in the focus of the form re-runs the go button process.
Double clicking on a result from the array within the listbox works fine using below:
void ListBox1_DoubleClick(object sender, EventArgs e) {}
I would like to be able to use my arrow keys and enter keys to select and run an event without having to double click on the line within the listbox. (however go button runs each time instead)
Basically open the form, type search string, press enter to run go button, use up and down arrows then press enter on selection to run same event as double click above. Will need to change focus after each bit.
You can handle the KeyDown events for the controls you want to override. For example,
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//execute go button method
GoButtonMethod();
//or if it's an event handler (should be a method)
GoButton_Click(null,null);
}
}
That will perform the search. You can then focus your listbox
myListBox.Focus();
//you might need to select one value to allow arrow keys
myListBox.SelectedIndex = 0;
You can handle the Enter button in the ListBox the same way as the TextBox above and call the DoubleClick event.
This problem is similar to -
Pressing Enter Key will Add the Selected Item From ListBox to RichTextBox
Certain controls do not recognize some keys when they are pressed in Control::KeyDown event. For e.g. list box does not recognize if the key pressed is Enter key.
See the remarks section of the Control::KeyDown event reference.
One way to resolve your problem might be writing a method for the Control::PreviewKeyDown event for your list box control:
private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up && this.listBox1.SelectedIndex - 1 > -1)
{
//listBox1.SelectedIndex--;
}
if (e.KeyCode == Keys.Down && this.listBox1.SelectedIndex + 1 < this.listBox1.Items.Count)
{
//listBox1.SelectedIndex++;
}
if (e.KeyCode == Keys.Enter)
{
//Do your task here :)
}
}
private void listBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
e.IsInputKey = true;
break;
}
}