you can set the windows form property for-
1- AcceptButton
- button id ( on which button you have to submit.)
2-CancelButton
- button id ( on which button you have to close the form.)
Question
I have two Forms Frm1
and Frm2
.
Both having single textbox
.
On keyup
event of first form textbox
, second form is opened if KeyChar
is ENTER
.
Now on KeyUp
event for textbox
in 2nd form I am closing this form i.e submitting.
Now both events are called. Is there any way to get rid of thi?
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Frm2 frm=new Frm2();
Frm2.RefToForm1=this;
frm.StartPosition = FormStartPosition.CenterParent;
frm.ShowDialog(this);
}
}
Now in second form
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.RefToForm1.textBox1.Text=textBox2.Text;
this.Close()
}
}
Problem is when I press enter on textBox1
, form2
is opened and closed immediately.
Any Solutions
La solution
you can set the windows form property for-
1- AcceptButton
- button id ( on which button you have to submit.)
2-CancelButton
- button id ( on which button you have to close the form.)
Autres conseils
There's no reason that releasing the Enter key when textBox1
is focused and no instance of Frm2
is opened yet, would also raise the KeyUp
event on textBox2
in Frm2
.
Are you sure you don't have some addition code in your project that's causing this behavior? Did you try putting a breakpoint on this.Close()
in textBox2_GotFocus
method to see if it actually gets executed in your scenario?
I even created a small sample project using your code with some minor modifications to make it work (explained in comments):
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Frm2 frm=new Frm2();
frm.RefToForm1=this; // you said RefToForm1 isn't static and it shouldn't be
frm.StartPosition = FormStartPosition.CenterParent;
frm.ShowDialog(this);
}
}
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.RefToForm1.textBox1.Text=textBox2.Text;
this.Close(); // missing semicolon
}
}
public Form1 RefToForm1 { get; set; } // property in Frm2
You can download this working sample project from here.
define a boolean variable in form 2, set it to false initially and close the form based on that variable. You could set it to true later when you need it. You could use the GotFocus method of the textbox to set to it true. e.g
textBox2.GotFocus += textBox2_GotFocus;
Set the boolean to true inside the textBox2_GotFocus method. Your key_up method would look like this :
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if(boolean_var){
this.RefToForm1.textBox1.Text=textBox2.Text;
this.Close();
}
}
}
Perhaps you could prevent the second form from closing if the textbox is empty -- assuming something needs to go in there for it to close.
Could you give us more information about what you're trying to do with this? Perhaps there's another way to solve the feature you're trying to create.