我有一个文本框TB
和EH
事件焦点/离开的处理程序。
另外,我有一个按钮BT
可以在单击时退出程序,只有 dispose(); 如果我留下TB
没有正确数据的框,则触发焦点离开事件处理程序以检查数据,它会警告我并将焦点返回到TB
.
但是如果我想在TB
有焦点的情况下退出程序并单击BT
,再次EH
触发并返回焦点,TB
程序不会退出。
我该如何解决这个问题?这是代码:
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();
}