Activate screen when hitting enter C# - 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;
}
}
}

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

Detect keyboard input and mouse activity on a single form

I have this below form and I want to detect key-strokes and any mouse clicks on the button.
For example, if I press S on my keyboard, message will show START and keypress will display keyboard press. The same thing happen when mouse-click on START button, except that keypress will display START BTN.
Here is my button code. If I only do for button or only IsKeyDown it works fine, but when I combine both in one form, they go haywire.
private void btnStart_Click(object sender, EventArgs e)
{
lblKeypress.Text = "START BTN";
lblmessage.Text = "START";
}
Here is my Keyboard.IsKeyDown code:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Keyboard.IsKeyDown(Key.S))
{
lblKeypress.Text = "Keyboard Press";
lblmessage.Text = "START";
}
}
Please help, thanks.
try this code
private void Form1_KeyDown(object sender,
EventArgs e){
if (e.KeyData == Keys.S){
lblKeypress.Text = "Keyboard Press";
lblmessage.Text = "START";
}
}
Make sure that in your Form you set KeyPreviewProperty to TRUE
In Form1.Designer.cs
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.form_keydown);
private void form_keydown(object sender, KeyEventArgs e)
{
int keyVal = (int)e.KeyValue;
keyValue = -1;
if ((keyVal >= (int)Keys.S))
{
keyValue = (int)e.KeyValue - (int)Keys.S;
lblKeypress.Text = "Keyboard Press";
lblmessage.Text = "START";
}
}
Note:
Ensure you do not have any unrelated WPF namespaces`
(Keyboard.IsKeyDown(Key.S)) is a WPF approach, in WinForms you are better off using combination of e.KeyValue & Keys

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"

UWP XAML Textbox Focus after Enter

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;
}

how to reset the focus to last entered textbox in windows application

I have two TextBoxes and a button control in the form. When the button is clicked the name of the last entered TextBox should be displayed in a MessageBox. At the same time I need to reset the focus to last entered TextBox.
string str=string.Empty;
bool foc;
In button click I wrote the following code
if (MessageBox.Show("You want to reset or continue", "control",
MessageBoxButtons.OKCancel) == DialogResult.Cancel)
{
if (foc== true)
{
textBox1.Focus();
}
else
{
textBox2.Focus();
}
}
When I clicks on cancel button the focus should be into textbox which is entered at last
private void textBox1_Enter(object sender, EventArgs e)
{
str = textBox1.Name;
foc= textBox1.Focus();
}
private void textBox2_Enter(object sender, EventArgs e)
{
str= textBox2.Name;
foc= false;
}
Other than the above lines of code is there any other possibility to focus into the textbox, but when number of textboxes increases how i need to write the conditions.
If I am having textbox,combobox,listbox,checkbox or any other controls in the form then how to find in which control the user enterd at last and set focus to that control by using any function instead of writing in every control Enter event
You can handle Leave event of the TextBoxes to store the Last TextBox Control.
Try this:
this.btnSubmit.Click += new System.EventHandler(this.Submit_Click);
this.btnCancel.Click += new System.EventHandler(this.Cancel_Click);
this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave);
this.textBox2.Leave += new System.EventHandler(this.textBox2_Leave);
TextBox txtLast = new TextBox();
private void textBox1_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void textBox2_Leave(object sender, EventArgs e)
{
txtLast = (TextBox)sender;
}
private void Submit_Click(object sender, EventArgs e)
{
MessageBox.Show(txtLast.Text);
}
private void Cancel_Click(object sender, EventArgs e)
{
txtLast.Focus();
}
public bool textBox1WasLastFocused = false, textBox2WasLastFocused = false; // Global Declaration
void textBox2_GotFocus(object sender, EventArgs e)
{
textBox2WasLastFocused = true;
textBox1WasLastFocused = false;
}
void textBox1_GotFocus(object sender, EventArgs e)
{
textBox1WasLastFocused = true;
textBox2WasLastFocused = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1WasLastFocused)
MessageBox.Show("textbox1 was ladst focused !");
else if(textBox2WasLastFocused)
MessageBox.Show("textbox2 was ladst focused !");
}

Categories