private void Form1_Load(object sender, System.EventArgs e)
{
txtAmount.Validating += new CancelEventHandler(Validation.IsInteger);
} public static void IsInteger(object sender, System.ComponentModel.CancelEventArgs e)
{
SWF.TextBox txt;
txt = (SWF.TextBox) sender;
try
{
int.Parse(txt.Text);
}
catch(FormatException)
{
SWF.MessageBox.Show("Please enter a numeric value.", "Bank Customer App");
e.Cancel = true; // cancel validation, returns focus
}
catch(OverflowException)
{
SWF.MessageBox.Show("Please enter an integer in the range " + int.MinValue + " ... " + int.MaxValue, "Bank Customer App");
e.Cancel = true; // cancel validation, returns focus
}
catch(Exception ex)
{
SWF.MessageBox.Show("Unknown error: " + ex.Message, "Bank Customer App");
e.Cancel = true; // cancel validation, returns focus
}
}
现在的问题是:
txtAmount这个text在我输入非数字时,出现异常提示这个是正常的,但是我如果不输入任何东西,仅仅点击当前窗口的关闭按钮时同样出现“异常提示”,这个有点不好。请问大家有什么好的方法把它去掉?
{
txtAmount.Validating += new CancelEventHandler(Validation.IsInteger);
} public static void IsInteger(object sender, System.ComponentModel.CancelEventArgs e)
{
SWF.TextBox txt;
txt = (SWF.TextBox) sender;
try
{
int.Parse(txt.Text);
}
catch(FormatException)
{
SWF.MessageBox.Show("Please enter a numeric value.", "Bank Customer App");
e.Cancel = true; // cancel validation, returns focus
}
catch(OverflowException)
{
SWF.MessageBox.Show("Please enter an integer in the range " + int.MinValue + " ... " + int.MaxValue, "Bank Customer App");
e.Cancel = true; // cancel validation, returns focus
}
catch(Exception ex)
{
SWF.MessageBox.Show("Unknown error: " + ex.Message, "Bank Customer App");
e.Cancel = true; // cancel validation, returns focus
}
}
现在的问题是:
txtAmount这个text在我输入非数字时,出现异常提示这个是正常的,但是我如果不输入任何东西,仅仅点击当前窗口的关闭按钮时同样出现“异常提示”,这个有点不好。请问大家有什么好的方法把它去掉?
{
Application.Exit();
}
也不行,还是出现我刚才说的异常提示:
SWF.MessageBox.Show("Please enter a numeric value.", "Bank Customer App");
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show(this, "确定要退出吗?", "提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (result == DialogResult.Cancel)
{
e.Cancel = true;
}
}
但是按照你说的,
还是出现:“Please enter a numeric value”的提示,
我的终极目标是在关闭的时候不出现上面的提示。
{
SWF.MessageBox.Show("Please enter a numeric value.", "Bank Customer App");
e.Cancel = true; 这句话不要试试 // cancel validation, returns focus
}
{
txtAmount.Validating += new CancelEventHandler(Validation.IsInteger);
}
你这界面初始的时候就调用了这个事件!你把这话放在一个按钮事件里!刚开始我没看到!界面一加载就触发的肯定会这样的吧!
public static void IsInteger(object sender, System.ComponentModel.CancelEventArgs e)
{
SWF.TextBox txt;
txt = (SWF.TextBox)sender;
if (txt.Text != String.Empty)
{
try
{
int.Parse(txt.Text);
}
catch (FormatException)
{
SWF.MessageBox.Show("Please enter a numeric value.", "Bank Customer App");
e.Cancel = true; // cancel validation, returns focus
}
catch (OverflowException)
{
SWF.MessageBox.Show("Please enter an integer in the range " + int.MinValue + " ... " + int.MaxValue, "Bank Customer App");
e.Cancel = true; // cancel validation, returns focus
}
catch (Exception ex)
{
SWF.MessageBox.Show("Unknown error: " + ex.Message, "Bank Customer App");
e.Cancel = true; // cancel validation, returns focus
}
}
}
我也考虑过在Validation.IsInteger这个函数里面来处理,但是,他毕竟在关闭前的时刻,调用IsInteger这个函数,也就不可避免的还会要检查他的IsInteger,既然检查这个就难免出现我说的那个提示;
我刚才也和你说了我的终极目标:“在关闭的时候不出现上面的提示。”
不知您有什么好的方法否?
谢谢。
txtAmount.Validating -= new CancelEventHandler(Validation.IsInteger);
{
SWF.TextBox txt;
txt = (SWF.TextBox)sender;
if (string.IsNullOrEmpty(txt.Text))
{
return;
} try
{
int.Parse(txt.Text);
}
catch (FormatException)
{
SWF.MessageBox.Show("Please enter a numeric value.", "Bank Customer App");
e.Cancel = true; // cancel validation, returns focus
}
catch (OverflowException)
{
SWF.MessageBox.Show("Please enter an integer in the range " + int.MinValue + " ... " + int.MaxValue, "Bank Customer App");
e.Cancel = true; // cancel validation, returns focus
}
catch (Exception ex)
{
SWF.MessageBox.Show("Unknown error: " + ex.Message, "Bank Customer App");
e.Cancel = true; // cancel validation, returns focus
}
}
在你的close 事件
txtAmount.Validating -= new CancelEventHandler(Validation.IsInteger);