Reputation: 83
I have a text box TB
and a handler EH
for the event Focus / Leave.
Also, I have a button BT
to quit the program when clicked, only dispose();
If I leave TB
box without proper data in it, the focus leave event handler is triggered to check the data, it warns me and return focus back to TB
.
But if I want to quit the program while TB
has the focus and click BT
, again the EH
is triggered and returns the focus to TB
and the program won't quit.
How can I solve this matter? Here's the code :
public Form1()
{
InitializeComponent();
}
private void EH(object sender, EventArgs e) // event handler EH
{
double temp;
if (TB.Text == "")
{
MessageBox.Show("Must enter a valid distance for d1!\r\n" +
"The valid range is ( 10,32 )",
"Wake up!", MessageBoxButtons.OK, MessageBoxIcon.Error);
TB.Focus();
return;
}
else
try
{
temp = Convert.ToDouble(TB.Text);
if (temp < 10 || temp > 32)
{
MessageBox.Show("Invalid distance for d1!\r\n" +
"The valid range is ( 10,32 )",
"Again! Wake up!", MessageBoxButtons.OK, MessageBoxIcon.Error);
TB.Text = "";
TB.Focus();
return;
}
minh1 = 1 / 8 * temp; // sets minimum h1
if (minh1 < 10)
minh1 = 10;
}
catch (Exception) // can't convert
{
MessageBox.Show("Invalid numeric entry!\r\n" +
"Please enter a valid number!",
"Hey! Wake up!", MessageBoxButtons.OK, MessageBoxIcon.Error);
TB.Text = "";
TB.Focus();
}
}
private void TB_TextChanged(object sender, EventArgs e) // change text in TB
{
if (TB.Text == "")
btgo.Enabled = false;
else
btgo.Enabled = true;
}
private void btgo_Click(object sender, EventArgs e) // Execute!
{
say.Text = "Minimum height h1 has been calculated to be " +
string.Format("{0:#0.00}", minh1) + " Fts";
BT.Focus();
}
private void BT_Click(object sender, EventArgs e) // --- PROGRAM END ---
{
Dispose();
}
Upvotes: 1
Views: 1351
Reputation: 6547
Keep an indicator that states whether an exit request was made.
private bool _isQuitRequested = false;
In the BT click event add
private void BT_Click(object sender, EventArgs e) // --- PROGRAM END ---
{
_isQuitRequested = true;
Dispose();
}
In the begining of the event handler EH
add
if (_isQuitRequested) return;
Don't forget to change _isQuitRequest
back to false in TB_TextChanged
Upvotes: 1
Reputation: 1045
You can try to create a global field to store permission for the program to exit,like this;
bool Quit=false;
Now when you've done some checks whether to exit the program,you can set this variable to true
or false
in case something is left.From now onwards whenever the BT_Click
event is fired(the Exit event) set this variable to true,like this;
private void BT_Click(object sender, EventArgs e) //--- PROGRAM END ---
{
Quit = true;//Flag to exit the program.
Dispose();
}
When you've don this add this code at the very beginning of EH
;
private void EH(object sender, EventArgs e) // event handler EH
{
if(Quit)//If Quit is true,means exit the program.
{
return;//No processing,just return.
}
//Run regular logic,if the function doesn't need to return.
}
But to make sure that the logic in EH
runs,set Quit
to false
in the TextChangedEvent
,like this;
private void TB_TextChanged(object sender, EventArgs e) // change text in TB
{
Quit=false;//Set this to false to make sure `EH` event's code is executed.
if (TB.Text == "")
btgo.Enabled = false;
else
btgo.Enabled = true;
}
Upvotes: 0