C# dateTimePicker Focus does not work - c#

In my C# Windows Forms application I want to navigate between two dateTimePicker (dateTimePicker1, dateTimePicker2) by pressing the Enter key.
When the Form open focus on dateTimePicker1 and press Enter key then focus dateTimePicker2 and press Enter key focus dateTimePicker1.
I'm trying the below code but it doesn't work:
private void dateTimePicker2_Enter(object sender, EventArgs e)
{
dateTimePicker1.Focus();
}

The Enter Event is triggered when you enter a control by pressing tab to change focus or clicking into it. If you want to listen for the enter key you need to use the KeyDown Event.
An implementation of the event handling would look like this:
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();
}
}

Try this
private void dateTimePicker1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
dateTimePicker2.Focus();
}
}
private void dateTimePicker2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
dateTimePicker1.Focus();
}
}

The Enter event does not represent the key press of the Enter key, it represents the user changing focus to the control.
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.enter(v=vs.110).aspx

You should use KeyDown event
private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
dateTimePicker2.Focus();
}

Related

How to do Button_Click from keybord?

How to do my "Button_click" when I press some key on keyboard? My code:
private void Button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
textBox1.Text = textBox1.Text + b.Text;
}
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
Char ch0 = '0';
if (e.KeyChar == ch0)
{
//MyButtonClick event
}
}
...
KeyPress event runs only when the button on form in focus, but I need to KeyPress worked just as I push the button on my keyboard. Please, give an exaple in you'r answer.
And don't be strict, I am only a beginner...
It is not completely clear that what are you asking, but I guess you want to do the same action on both keypress of your keyboard and when user clicks on the button.
look at this topic on MSDN for key_press:
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 48 && e.KeyChar <= 57)
{
MessageBox.Show("Form.KeyPress: '" +
e.KeyChar.ToString() + "' pressed.");
switch (e.KeyChar)
{
case (char)49:
case (char)52:
case (char)55:
MessageBox.Show("Form.KeyPress: '" +
e.KeyChar.ToString() + "' consumed.");
e.Handled = true;
break;
}
}
}
To do the same action in 2 event, you can write another method and call them from both events.
public void MyAction(mypars)
{
....
}
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MyAction(mypars);
}
private void Button_Click(object sender, EventArgs e)
{
MyAction(mypars);
}
It is also possible to call another event method, since they are like other methods, but I really do not suggest that! but it is like this:
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
Button_Click(sender,new EventArgs());
}
Update
You have to be better than "I want my button to be pressed". If you mean the visuals, you can set the focus of the control, like:
myButton.Focus();
Hope I could help you out :)
update 2
you have to use it like this
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MyAction(mypars);
myButton.Focus();
}
if you need it to focus only on special chars, then check it before doing anythig like
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 48 && e.KeyChar <= 57)
{
MyAction(mypars);
myButton.Focus();
}
}

If Enter key hit - show message. Else - type as normal

If the user hits the enter key, I want to show a message saying the enter key was hit. Else, I want to type in the textbox as normal.
private void enterCheck(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter Was Clicked");
}
}
If enter is hit the message is displayed correctly. However, if I try to type a sentence, nothing appears in the textbox.
This will work...
private void enterCheck(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter Was Clicked");
}
else
{
MessageBox.Show(e.KeyCode.ToString());
}
}
However I don't want a messagebox showing every letter that was typed.
Try this:
private void enterCheck(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(e.KeyCode.ToString());
}
else
{
e.Handled = true;
}
}
Please work! This is the most confusing I've ever encountered!
Use the KeyPress event:
// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Check for the flag being set in the KeyDown event.
if (e.KeyChar == (char)Keys.Return)
{
// Stop the character from being entered into the control since it is non-numerical.
MessageBox.Show("Enter Was Pressed");
e.Handled = true;
}
}

Make code for multiple textbox when press Enter send command Tab better

i already have code when user press key Enter on keyboard, it return tab and "jump" to next field, its working great, its possible make it for 2 or 3 textbox, problem when need make it on multiple textbox like 20 textbox for each form, its just not work.
See code:
// Detect if Enter key is pressed on each text box, mute sound enter "ding" sound and replace Enter for tab (problem that have make it for each textbox)
private void txtAltura_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true; //Silenciar Enter
SendKeys.Send("{TAB}");
}
}
private void txtLargura_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true; //Silenciar Enter
SendKeys.Send("{TAB}");
}
}
private void txtProfundidade_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true; //Silenciar Enter
SendKeys.Send("{TAB}");
}
}
//execute keypress command when enter is typed on textbox
private void txtProfundidade_TextChanged(object sender, EventArgs e)
{
if (txtProfundidade.Text != "") { foreach (char c in txtProfundidade.Text.ToCharArray()) txtProfundidade_KeyPress(sender, new KeyPressEventArgs(c)); }
}
private void txtLargura_TextChanged(object sender, EventArgs e)
{
if (txtLargura.Text != "") { foreach (char c in txtLargura.Text.ToCharArray()) txtLargura_KeyPress(sender, new KeyPressEventArgs(c)); }
}
private void txtAltura_TextChanged(object sender, EventArgs e)
{
if (txtAltura.Text != ""){foreach (char c in txtAltura.Text.ToCharArray()) txtAltura_KeyPress(sender, new KeyPressEventArgs(c));}
}
Hope make it better.
Thanks in advance..
if its windows form app can use this, this will replace tab key press with Enter key
protected override bool ProcessKeyPreview(ref Message m)
{
if (m.Msg == 0x0100 && (int)m.WParam == 13)
{
this.ProcessTabKey(true);
}
return base.ProcessKeyPreview(ref m);
}
From what I understand, you're trying to figure out a way to assign event handlers to multiple textbox controls and don't want to write a handler for each one. If that's the case, try this:
private void Form1_Load(object sender, EventArgs e)
{
foreach (TextBox textBox in this.Controls.OfType<TextBox>())
{
textBox.KeyDown += new KeyEventHandler(textBox_KeyDown);
}
}
void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
SendKeys.Send("{TAB}");
}
}
This will assign a handler to each textbox control on the form.
That worked perfectly well.
I also have to repeat this on each textbox to check if all codes was filled and update statusbar:
private void txtAltura_TextChanged(object sender, EventArgs e)
{
testePreenchidoecalculo();
}
private void txtLargura_TextChanged(object sender, EventArgs e)
{
testePreenchidoecalculo();
}
private void txtProfundidade_TextChanged(object sender, EventArgs e)
{
testePreenchidoecalculo();
}
Its possible to make it better ?

Does the KeyDown KeySuppress cancel KeyUp event?

Let's say I've got this:
private void txtAnalogValue_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//Non-numeric key pressed => prevent this from being input into the Textbox
e.SuppressKeyPress = true;
}
}
and this:
private void txtAnalogValue_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
try
{
UpdateState(double.Parse(((TextBox)sender).Text));
}
catch (Exception ex)
{
((TextBox)sender).Text = ioElement.StateVal.ToString("0.00");
}
}
}
I know this code doesn't make a lot of sense, it's just test.
The question is: Will the e.SuppressKeyPress = true in KeyDown event have affect on KeyUp event, so the Enter key will not be accepted?
No, e.SuppressKeyPress = true will just ignore the Enter key (it won't go to the next line and Text property of the textbox won't be changed) and e.Keycode will be visible in the KeyUp. Therefore suppressing the key in the KeyDown doesn't affect the KeyUp event and your code should work. The UpdateState will be called when you hit Enter button in the TextBox. You can try this code to check it:
private void txtAnalogValue_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
e.SuppressKeyPress = true;
}
}
private void txtAnalogValue_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A)
{
MessageBox.Show("Up");
}
}

Detect when two keys are pressed at the same time

I have no idea how do this.
I know only how do detect one key:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.C)
{
MessageBox.Show("C key");
}
}
You have to keep track of keydown/keyup events, and keep a list of all the keys that are currently "down". The keyboard handler can only trigger on individual keys, and it's up to your code to detect/keep track of which ones are down, and if those individual keydown events are close enough to each other to be counted as "together".
put a break point in your key down event and press your two keys together. examine the KeyData of the KeyEventArgs. it will show you what you have to use to detect two keys pressed together. Use some dummy code like this:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("KeyData is: " + e.KeyData.Tostring());
}
like I have done for shift and r pressed together
As you can see, you can use a timer event with booleans to detect if two keys are pressed:
bool keyup = false;
bool keyleft = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = true;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = false;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (keyleft && keyup)
{
Console.Beep(234, 589);
}
}
Use this:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
string keysPressed = keyData.ToString();
// your code goes here
}
This is what I get for Up + Shift: "Up, Shift"

Categories

Resources