Hello every one
I am moving between controls in my application on enter click
How can i know enter key is pressed on leave event of combobox or textbox in c#
Actually i am doing something on leave event of textbox as my control goes to next control on enter key in my whole application but it that particular code should be execute only on leave event and when enter key is pressed
Now problem is that it is executing on leave event but i am not getting how to know enter is pressed
Loading

Zoran HorvatPosted Jul 6, 2011, 6:54 AM
private void comboBox5_Leave(object sender, EventArgs e)
{
bool enterPressed = _isEnterPressed;
_isEnterPressed = false; // Clear the flag unconditionally; previous value is remembered in local variable
if (radioButton2.Checked)
{
if (enterPressed == true & comboBox5.Text == "")
{
medicinerpt m = new medicinerpt();
m.pid = int.Parse(comboBox9.Text);
m.d1 = dateTimePicker4.Value.ToString();
m.Dock = DockStyle.Fill;
Reports v = new Reports();
v.Text = "Medicines";
v.panel1.Controls.Clear();
v.panel1.Controls.Add(m);
v.Show();
}
else
comboBox4.Focus();
}
}
That is the only way to clear the flag and to use its active value in the Leave handler. Just copy this code and that will work.
Mayur GujrathiPosted Jul 6, 2011, 7:24 AM
Mayur GujrathiPosted Jul 6, 2011, 6:44 AM
bool _isEnterPressed ;
bool enterPressed;
protected override bool ProcessDialogKey(Keys keyData)
{
switch (keyData)
{
case Keys.Enter:
enterPressed = _isEnterPressed;
_isEnterPressed = false;
return base.ProcessDialogKey(Keys.Tab);
}
return base.ProcessDialogKey(keyData);
}
private void comboBox5_Leave(object sender, EventArgs e)
{
if (radioButton2.Checked)
{
if (enterPressed == true & comboBox5.Text == "")
{
//now control is not coming here in any condition
medicinerpt m = new medicinerpt();
m.pid = int.Parse(comboBox9.Text);
m.d1 = dateTimePicker4.Value.ToString();
m.Dock = DockStyle.Fill;
Reports v = new Reports();
v.Text = "Medicines";
v.panel1.Controls.Clear();
v.panel1.Controls.Add(m);
v.Show();
}
else
comboBox4.Focus();
}
}
Zoran HorvatPosted Jul 6, 2011, 6:07 AM
bool enterPressed = _isEnterPressed;
_isEnterPressed = false; // Clear the flag unconditionally; previous value is remembered in local variable
if (radioButton2.Checked)
{
if (enterPressed == true & comboBox5.Text == "")
{
medicinerpt m = new medicinerpt();
m.pid = int.Parse(comboBox9.Text);
m.d1 = dateTimePicker4.Value.ToString();
m.Dock = DockStyle.Fill;
Reports v = new Reports();
v.Text = "Medicines";
v.panel1.Controls.Clear();
v.panel1.Controls.Add(m);
v.Show();
}
else
comboBox4.Focus();
}
Mayur GujrathiPosted Jul 6, 2011, 5:45 AM
protected override bool ProcessDialogKey(Keys keyData)
{
switch (keyData)
{
case Keys.Enter:
_isEnterPressed = true;
return base.ProcessDialogKey(Keys.Tab);
}
return base.ProcessDialogKey(keyData);
}
leave event of combobox
if (radioButton2.Checked)
{
if (_isEnterPressed == true & comboBox5.Text == "")
{
_isEnterPressed = false;
medicinerpt m = new medicinerpt();
m.pid = int.Parse(comboBox9.Text);
m.d1 = dateTimePicker4.Value.ToString();
m.Dock = DockStyle.Fill;
Reports v = new Reports();
v.Text = "Medicines";
v.panel1.Controls.Clear();
v.panel1.Controls.Add(m);
v.Show();
}
else
comboBox4.Focus();
}
Mayur GujrathiPosted Jul 6, 2011, 5:40 AM
Zoran HorvatPosted Jul 6, 2011, 5:28 AM
Zoran
Mayur GujrathiPosted Jul 6, 2011, 5:22 AM
it is giving error
only assignment,call,increament,decreament can be used as statement
Zoran HorvatPosted Jul 6, 2011, 5:17 AM
Second, first thing in "my code" part should be _isEnterPressed = false. That would do it.
Something like this:
private void comboBox5_Leave(object sender, EventArgs e)
{
if (_isEnterPressed == true & comboBox5.Text == "")
{
_isEnterPressed = false;
// my code
}
}
Zoran
Mayur GujrathiPosted Jul 6, 2011, 5:08 AM
i did
private void comboBox5_Leave(object sender, EventArgs e)
{
if (_isEnterPressed = true & comboBox5.Text == "")
{
my code
}
}
and to move on enter click between controls i am using following code and here i am setting _isEnterPressed to true
protected override bool ProcessDialogKey(Keys keyData)
{
switch (keyData)
{
case Keys.Enter:
_isEnterPressed = true;
return base.ProcessDialogKey(Keys.Tab);
}
return base.ProcessDialogKey(keyData);
}
Zoran HorvatPosted Jul 6, 2011, 4:21 AM
In that way you're completing the cycle - flag is set only if Enter is pressed AND Leave event is about to be raised, and it is cleared in the Leave event handler which is invoked immediately and synchronously from inside the Enter key press handler. Hence there is no fear that any other entity could interfere with the flag and cause it to be misinterpretted in the Leave handler.
Zoran
Mayur GujrathiPosted Jul 6, 2011, 4:04 AM
Zoran HorvatPosted Jul 6, 2011, 3:49 AM
2. Set it to true when Enter is pressed and you move focus to other control (which will cause Leave event to be raised).
3. In Leave event handler test the _isEnterPressed field and do appropriate action, but make sure that clearing it to false is the first thing to do after testing.
Zoran