Focus selected controls by pressing Enter Key - c#

In C# windows application to navigate all control of a Form (using Enter Key) I am using the below code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == System.Windows.Forms.Keys.Enter)
{
SendKeys.Send("{TAB}");
}
}
N.B.: Form Property KeyPreview = True;
The above code works fine but when I am going to navigate between two dateTimePicker (dateTimePicker1, dateTimePicker2) pressing Enter Key.
When Form open Focus on dateTimePicker1 and press Enter Key then Focus dateTimePicker2 and press Enter Key Focus dateTimePicker1.
The below code works fine without the above code. What is the best way to navigate the two dateTimePicker using the above code or any other way?
private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
dateTimePicker2.Focus();
}
}
private void dateTimePicker2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
dateTimePicker1.Focus();
}
}
Anybody please help me.

You can subscribe your two DateTimePickers to the same event handler instead of using two events, and use the sender object:
private void dateTimePicker_KeyDown(object sender, KeyEventArgs e)
{
var dtp = sender as DateTimePicker;
if (e.KeyCode == Keys.Enter)
{
if (dtp?.Name[dtp.Name.Length - 1] == '1')
dateTimePicker2.Focus();
else dateTimePicker1.Focus();
}
}
Just don't forget to change the value of the KeyDown event in the properties window of the both DateTimePickrs to point to this event.

Related

C# Key Events - KeyDown

I have a Windows Form Application. I want some functions to work with the space key. But when I press the space key, the function I want is not working and it goes to the next form. (I did KeyPreview = true)
private void Form7_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
IEyeTracker eyeTracker = EyeTrackingOperations.FindAllEyeTrackers().FirstOrDefault();
GazeDataStop(eyeTracker);
}
}
Because:
1- If you have buttons, ... keydown won't work as form won't have focus anymore
2-you must handle the keydown so that it is not passed to ohter controls
Solution for 1:
set KeyPreview property of your form to true
Solution for 2:
set e.Handled = true:
private void Form7_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
if (e.KeyCode == Keys.Space)
{
IEyeTracker eyeTracker = EyeTrackingOperations.FindAllEyeTrackers().FirstOrDefault();
GazeDataStop(eyeTracker);
}
}

c# detect and send f13-f24 keys

I need an application that can detect the keypress of F13-F24. I tried making a form and setting keydown and Keypress events and printing in a messagebox the key pressed but when i press F13-F24, i don't get a messagebox
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(e.KeyCode.ToString());
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}
This didn't seem to work. How can I do this?
You can capture this using System.Windows.Forms.KeyEventHandler.
The KeyEventHandler will provide a KeyEventArgs object that includes a KeyCode property. KeyCode is a System.Windows.Forms.Keys enumeration and supports F1-F24.
private void KeyDownHandler(object sender, System.Windows.Forms.KeyEventArgs e) {
if (e.KeyCode == Keys.F13) {
//F13
} else if (e.KeyCode == Keys.F14) {
//F14
} else if (e.KeyCode == Keys.F15) {
//F15
}
// etc
}
If you don't have a keyboard capable of sending higher F-keys, the F13-F24 keys are the equivilent of SHIFT-F1-SHIFT-F12. You can capture this sequence using the KeyCode and Modifiers (Modifiers == Keys.Shift) properties together.
Windows 10 doesn't recognize the higher Function keys 13-24. You can test this by turning on the Windows Narrator functionality, and then press Shift -F1. The narrator just says "Shift F1", not "F13"
So, instead I recommend that you just detect Shift -F1 to Shift -F12 instead.
private void Form1_PreviewKeyDown(object sender, KeyPressEventArgs e)
{
if(e.KeyCode == Keys.F1 && e.Modifiers == Keys.Shift)
{
MessageBox.Show("Shift-F1 pressed");
}
}
As MstfAsan commented, you need to use the PreviewKeyDown eventhandler and set the form's KeyPreview property to True for this to work

How to check if key is pressed in a text box Windows Form

I'm using Visual Studio C# and trying to check if the ENTER key has been pressed while in a text box
private void PasswordTextBox_TextChanged(object sender, EventArgs e)
{
if(keyenterhasbeenpressed) //I dont know what to put here
{
MessageBox.Show("You pressed the enter key");
}
}
So when someone clicks enter in that text box it will do something.
The TextChanged event isn't really suited for checking for keypresses.
It's best used to examine the sender object which is the textbox itself.
To check for the ENTER key you can do the following:
private void PasswordTextBox_KeyDown(object sender, EventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
MessageBox.Show("You pressed the enter key");
}
}
or
private void PasswordTextBox_KeyPress(object sender, EventArgs e)
{
if(e.KeyChar == 13)
{
MessageBox.Show("You pressed the enter key");
}
}
It depends on what event best suits your needs.
You can always put a break point (assuming visual studio) and check what data is available to you in the eventargs and sender object
Inorder to link the function with the key press event of the textbox add the following code in the designer.cs of the form
this.textbox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDownHandler);
Now define the function 'OnKeyDownHandler' in the cs file of the same form
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//enter key has been pressed
// add your code
}
}

Restrict keypress in wpf textbox

I have TextBox in WPF where i need to fill the box only by pasting (ctrl +v) not by typing. So i need to restrict entire key press except ctrl+v. Since WPF is not having keypress event i am facing the problem to restrict the keypress
Do it WPF style and use ApplicationCommands.Paste and make the textbox readonly.
you can add this Key_Down handler to the textBox:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.Key==Key.V)
{
//Logic here
}
else
e.handled=true;
}
Provided you don't allow Right Click + Paste, but only Ctrl + V, I would simply check for the Ctrl key modifier being pressed and prevent everything else.
So try this:
myTextBox.KeyDown += new KeyEventHandler(myTextBox_KeyDown);
private void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
input = myTextBox.Text;
}
else
{
input = "";
}
}
<TextBox IsReadOnly="True" Name="Policy_text">
<TextBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Paste" CanExecute="PasteCommand_CanExecute" Executed="PasteCommand_Executed" />
</TextBox.CommandBindings>
</Textbox>
and in code behind
private void PasteCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = Clipboard.ContainsText();
}
private void PasteCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
Policy_text.Paste();
}

How can I override the default button on a form when focus is in a TextBox

I have a winform form which has typical OK and Cancel buttons.
The OK button is set as the default button. When the user presses the ENTER key, the form is closed.
The form also has a text box which has a Search button beside it. The text box allows the user to enter search text and the Search button initiates the search function.
I would like the user to be able to press the ENTER key when the text box has the input focus and have the search function activate.
The problem is that the form is grabbing the ENTER key event before the text box's handler gets it.
EDIT: I've tried using the following event handlers, but they never get hit, and the form closes when the Enter key is hit:
private void txtFilter_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
e.Handled = true;
}
}
private void txtFilter_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
e.Handled = true;
}
}
I've also tried enabling the KeyPreview property on the form and implementing a KeyDown event handler for the form, but the Enter key never seems to cause any of these to be hit.
What is the best way to accomplish this?
Try handling the Enter and Leave events of the TextBox to clear out your form's AcceptButton property:
private void txtFilter_Enter(object sender, EventArgs e) {
this.AcceptButton = null;
}
private void txtFilter_Leave(object sender, EventArgs e) {
this.AcceptButton = closeButton;
}
Then you can just process your KeyUp event as your want:
private void txtFilter_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
}
}
Add a KeyDown event handler to the textBox and then add this to it
if (e.KeyCode == Keys.Enter)
{
btnSearch.PerformClick();
}

Categories

Resources