How to run event implementation from other event condition - c#

I'm wondering, if is it possible to run if (e.KeyCode == Keys.Enter) event process without pressing on enter, correctly to say without event condition itself.
The only way I know to get result, which can be useful if implemented, is to locate content of condition in function, but I'm asking if it is possible other way.
From textBox2_TextChanged event condition to get implementation of if (e.KeyCode == Keys.Enter) event of textBox1_KeyDown with entering of exist text and containing process implementation?
For example:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
/// code
if (e.KeyCode == Keys.Enter)
{
label1.Text = ("text inserted");
}
}
and desired result is to call (e.KeyCode == Keys.Enter) implementation with label1.Text = ("text inserted"); from textBox1_KeyDown in textBox2_TextChanged here:
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (x > 0)
{
/// here I want implement if (e.KeyCode == Keys.Enter) from code above with label1.Text = ("text inserted");
}
}

you can like this
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
label1.Text = ("text inserted");
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
KeyEventArgs ev = new KeyEventArgs(Keys.Enter);
textBox1_KeyDown(sender, ev);
}

Related

C# Compare the Key Pressed on the Keyboard

I have a textbox and a button.I'm saving the value(keyboard key) entered in the TextBox.I need to give a message when I press the right keyboard key.
private void btn_Click(object sender, EventArgs e)
{
Properties.Settings.Default.text1 = text1.Text;
Properties.Settings.Default.Save();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == text1.Text) //--------->> error
{
MessageBox.Show("success");
}
}
how can I provide this condition?
maybe easier it will be to use KeysConverter
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
KeysConverter convertor = new KeysConverter();
string keyPressed = convertor.ConvertToString(e.KeyValue);
if (keyPressed == text1.Text)
{
//do stuff
}
}
If you compare with one char text. You can try this.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (txt.Text.Length == 1 && e.KeyValue == (int)txt.Text[0]) //--------->> error
{
MessageBox.Show("success");
}
}

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"

Textbox will not accept more than 1 character without disappearing in C#

private void btnClassNameA_Click(object sender, EventArgs e)
{
txtbClassNameA.Visible = true;
txtbClassNameA.Focus();
}
private void txtbClassNameA_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) ;
btnClassNameA.Text = txtbClassNameA.Text;
txtbClassNameA.Visible = false;
}
Upon clicking of a button, a text box appears. I can't get it to accept more than 1 character at a time without disappearing. It is supposed to disappear by pressing the enter key. Any help would be greatly appreciated!
Your current code is equivalent to this:
private void txtbClassNameA_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) { } // does nothing, just evaluates the condition
btnClassNameA.Text = txtbClassNameA.Text;
txtbClassNameA.Visible = false;
}
You have to change it like this:
private void txtbClassNameA_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnClassNameA.Text = txtbClassNameA.Text;
txtbClassNameA.Visible = false;
}
}
Your if statement is not formatted correctly. Try it like this:
if (e.KeyCode == Keys.Enter)
{
btnClassNameA.Text = txtbClassNameA.Text;
txtbClassNameA.Visible = false;
}
private void txtbClassNameA_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnClassNameA.Text = txtbClassNameA.Text;
txtbClassNameA.Visible = false;
}
}
If that is your actual code, the semicolon might be throwing you off. Try this.
It appears you have a semicolon after your conditional.
Right now it is evaluating the conditional, and then moving on to update the text and make the box invisible.
private void txtbClassNameA_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnClassNameA.Text = txtbClassNameA.Text;
txtbClassNameA.Visible = false;
}
}
Might give you better results.

Keypress event in cell of datagridview with key return

Code run when I use with key A.
But code not run when I use with key return.
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txt = e.Control as TextBox;
txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
}
void txt_KeyPress(object sender, KeyPressEventArgs e)
{
/* Code run. */
if(e.KeyChar == (char)Keys.A)
{
MessageBox.Show("Hello");
}
/* Code not run! */
if (e.KeyChar == (char)Keys.Return)
{
MessageBox.Show("Hello");
}
}
But it run if I try with a text box.
Thank you TaW!
I solved it like this...
void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
}
}
void TextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
/* Your code here! */
}
}

Detect Enter Key C#

I have the following code which does not show the MessageBox when enter/return is pressed.
For any other key(i.e. letters/numbers) the MessageBox shows False.
private void cbServer_TextChanged(object sender, EventArgs e)
{
if (enterPressed)
{
MessageBox.Show("Enter pressed");
}
else
MessageBox.Show("False");
}
private void cbServer_Keydown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
enterPressed = true;
MessageBox.Show("Enter presssed: " + enterPressed);
}
else
enterPressed = false;
}
Any ideas?
EDIT: Above code, I thought the issue was with the _Keydown even so I only posted that.
in your form designer class (formname.designer.cs) add this :
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Login_KeyPress);
and add this code to backbone code (formname.cs):
void Login_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
MessageBox.Show("ENTER has been pressed!");
else if (e.KeyChar == (char)27)
this.Close();
}
This is because when you press Enter TextChanged event won't fire.
private void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
MessageBox.Show("Enter key pressed");
}
else if (e.Key == Key.Space)
{
MessageBox.Show("Space key pressed");
}
}
Use PreviewKeyDown event to detect any key before shown in textbox or input
void cbServer_PreviewKeyDown(object sender, System.Windows.Forms.PreviewKeyDownEventArgs e)
{
// Check if User Presses Enter
if (e.KeyCode == System.Windows.Forms.Keys.Return)
{
// Here: user pressed ENTER key
}
else
{
// Here: user did not press ENTER key
}
}

Categories