UWP XAML Textbox Focus after Enter - c#

I have a Menu like this:
I´d like that if the cursor is on ValorInsTextBox (Valor Textbox) and I press Enter, the app call the button InserirBtn_ClickAsync (Inserir Button) and after the process, the cursor come back to PosicaoInsTextBox (Posição Textbox).
I made some methods using Key_Down, but something weird happens. Look the code:
private void PosicaoInsTxtBox_KeyDown(Object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
InserirBtn_ClickAsync(sender, e);
PosicaoInsTxtBox.Focus(FocusState.Programmatic);
}
}
private void ValorInsTxtBox_KeyDown(Object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
InserirBtn_ClickAsync(sender, e);
if (PosicaoInsTxtBox.IsEnabled)
{
PosicaoInsTxtBox.Focus(FocusState.Programmatic);
}
else
{
ValorInsTxtBox.Focus(FocusState.Programmatic);
}
}
}
When I debug the code, I press Enter when the ValorInsTextBox is on focus, and the method ValorInsTextBox_KeyDown starts and everything goes well. And when it arrives on line:
PosicaoInsTxtBox.Focus(FocusState.Programmatic);
it goes to execute the method PosicaoTextBox_KeyDown and starts to execute it. I don´t know why! Anyone can help me?

You could set the Handled property of the KeyRoutedEventArgs to true in the ValorInsTxtBox_KeyDown event handler to prevent the PosicaoInsTxtBox_KeyDown event handler from being invoked:
private void ValorInsTxtBox_KeyDown(Object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
InserirBtn_ClickAsync(sender, e);
if (PosicaoInsTxtBox.IsEnabled)
{
PosicaoInsTxtBox.Focus(FocusState.Programmatic);
}
else
{
ValorInsTxtBox.Focus(FocusState.Programmatic);
}
}
e.Handled = true;
}
And do the same in the PosicaoInsTxtBox_KeyDown event handler to prevent it from being invoked again when you press ENTER in the Posicao" TextBox:
private void PosicaoInsTxtBox_KeyDown(Object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
InserirBtn_ClickAsync(sender, e);
PosicaoInsTxtBox.Focus(FocusState.Programmatic);
}
e.Handled = true;
}

Related

how to stop the button execution even i exit the form - C#

Good day, I'd just like to ask on how to stop button code execution even i exit the form.
my plan is to ask the user to input the authcode before deleting something. this authcode form will show when delete button clicked. this my AuthCodeFrm:...
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnok_Click(object sender, EventArgs e)
{
if (txtDelAuth.Text == G36_Spare.Properties.Settings.Default.User_DeletePass)
{
this.Close();
}
else if (txtDelAuth.Text != null)
{
MessageBox.Show("Auth code required");
}
}
private void txtDelAuth_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
btnok_Click(sender, e);
}
this is my Deletebtn
deletebtn

Allow only specific keys on TextBox

I have a question. Examples that i find is on a "KeyPress" and they are not longer working on WPF
Can you tell me, how to allow only specified keys from keybord to be write on WPF textbox? I know about keyUp and Down functions, but how to define letters that i want to by type into it?
I think it will be easier, if i will post my code and i tell u what i want to do. What to change here?
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//something here to only allow "A" key to be pressed and displeyed into textbox
if (e.Key == Key.A)
{
stoper.Start();
}
}
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.A)
{
//here i stop the stopwatch to count time of pressing the key
stoper.Stop();
string aS = stoper.ElapsedMilliseconds.ToString();
int aI = Convert.ToInt32(aS);
stoper.Reset();
}
}
You can use PreviewKeyDown and use e.Key to filter out what you need.
Or, in any place of your code you can use Keyboard class:
if (Keyboard.IsKeyDown(Key.E)) { /* your code */ }
UPDATE:
To forbid a key, you need to set event as handled:
if (e.Key == Key.E)
{
e.Handled = true;
MessageBox.Show($"{e.Key.ToString()} is forbidden");
}
That thing works pretty fine to me (thanks for #JohnyL):
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//something here to only allow "A" key to be pressed and displeyed into textbox
if (e.Key == Key.A)
{
stoper.Start();
}
else
e.Handled = true;
}
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.A)
{
//here i stop the stopwatch to count time of pressing the key
stoper.Stop();
string aS = stoper.ElapsedMilliseconds.ToString();
int aI = Convert.ToInt32(aS);
stoper.Reset();
}
}

Detecting Enter key press on a button c#

This piece of code detect most keys and puts it in a messageBox, but one of the keys it doesn't is the enter key. *Note it is a key_down method
MessageBox.Show(e.KeyData.ToString());
so far I have tried so many methods to fix and searched a lot for an answer
I have tried this
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("this is it");
e.IsInputKey = true;
}
}
it says 0 references which I know I'm not calling the method but where do I call it?
where ever I do call it, it gives an error.
you delete this line
e.IsInputKey = true;
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("this is it");
//e.IsInputKey = true;
}
}
or
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("this is it");
//e.IsInputKey = true;
}
}
In your Form1 Design Properties:
AcceptButton Property set dropdown to button1 or the name of your Button
KeyUp Event of your Button
private void button1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key has been pressed");
}
}
or
private void button1_KeyUp(object sender, KeyEventArgs e)
{
button1_KeyDown(sender, e);
}
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key has been pressed");
}
}
Put this line soon after: InitializeComponents(); -->
button1.PreviewKeyDown +=new PreviewKeyDownEventHandler(button1_PreviewKeyDown);
.
The event you're trying to use will only fire when you focus on that button. The focus is passed in 2 different ways ( via user interaction ) one of which is an actual mouse click on that element and another is "tabbing" on to the control, and make sure you read about "Events" and "EventsHandlers"

Activate screen when hitting enter C#

I have a login screen and I want to activate it when I press enter in the password textbox.
the problem is that even though it works, when I close the form the app acts like enter is still pressed and the form opens in an endless loop.
here is my code:
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox2.KeyDown += new KeyEventHandler(textBox2_KeyDown);
}
public void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (user == Username[1] && pass == passwords[1])
{
MessageBox.Show("Login successfull", "Welcome, HR");
UpdateDBForm newEmployee = new UpdateDBForm();
this.Hide();
newEmployee.ShowDialog();
return;
}
}
How tdo I solve this problem?
Thanks.
You are assigning the KeyDown-EventHandler everytime your text changes:
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox2.KeyDown += new KeyEventHandler(textBox2_KeyDown); // EVIL!
}
This means, that the more often you input data into your textbox, the more the eventhandler will be assigned and when you eventually hit enter it will be called as many times.
Assign the eventhandler once, i.e. in your constructor and this should work.
As #bash.d written you are assigning event multiple times, do it once (either by designer or in constructor (after InitializeComponent call) or in Form_Load event)
private void Form_Load(object sender, EventArgs e)
{
textBox2.KeyDown += new KeyEventHandler(textBox2_KeyDown);
}
also you've written that you want login after user click enter, so you have to add this if:
public void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (user == Username[1] && pass == passwords[1])
{
MessageBox.Show("Login successfull", "Welcome, HR");
UpdateDBForm newEmployee = new UpdateDBForm();
this.Hide();
newEmployee.ShowDialog();
return;
}
}
}

show a button pressed effect when default key is pressed

I want my button to have a pressed and released effect(simulating a mouse press) when i press my default key(enter).
I have tried doing this
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
VisualStateManager.GoToState(btnLogin, "Pressed", true);
System.Threading.Thread.Sleep(300);
VisualStateManager.GoToState(btnLogin, "Normal", true);
btnLogin_Click(sender, e);
}
}
Cant get it to work.
Did you make your button Property IsDefault=True
Try the following
'
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
VisualStateManager.GoToState(btnLogin, "Pressed", true);
this.Refresh();
System.Threading.Thread.Sleep(300);
VisualStateManager.GoToState(btnLogin, "Normal", true);
this.Refresh();
btnLogin_Click(sender, e);
}
}
'

Categories