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 HorvatRank #792 · 2.9K reputationPosted Jul 6, 2011, 2:54 AM EDT
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.
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 5, 2011, 11:49 PM EDT
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
Mayur GujrathiRank #666 · 3.6K reputationPosted Jul 6, 2011, 12:04 AM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 6, 2011, 12:21 AM EDT
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 GujrathiRank #666 · 3.6K reputationPosted Jul 6, 2011, 1:08 AM EDT
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 HorvatRank #792 · 2.9K reputationPosted Jul 6, 2011, 1:17 AM EDT
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 GujrathiRank #666 · 3.6K reputationPosted Jul 6, 2011, 1:22 AM EDT
it is giving error
only assignment,call,increament,decreament can be used as statement
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 6, 2011, 1:28 AM EDT
Zoran
Mayur GujrathiRank #666 · 3.6K reputationPosted Jul 6, 2011, 1:40 AM EDT
Mayur GujrathiRank #666 · 3.6K reputationPosted Jul 6, 2011, 1:45 AM EDT
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();
}
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 6, 2011, 2:07 AM EDT
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 GujrathiRank #666 · 3.6K reputationPosted Jul 6, 2011, 2:44 AM EDT
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();
}
}
Mayur GujrathiRank #666 · 3.6K reputationPosted Jul 6, 2011, 3:24 AM EDT