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在我输入非数字时,出现异常提示这个是正常的,但是我如果不输入任何东西,仅仅点击当前窗口的关闭按钮时同样出现“异常提示”,这个有点不好。请问大家有什么好的方法把它去掉?

解决方案 »

  1.   

    我的目的是想关闭这个form 不出现提示。
      

  2.   

    你关闭的按钮难道也和那个TEXTBOX关联的啊?直接this.close();或者Application.Exit();这样都有异常?
      

  3.   

    我说的关闭是直接点击form上的x,来实现的。
      

  4.   

            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                Application.Exit();
            }
    也不行,还是出现我刚才说的异常提示:
    SWF.MessageBox.Show("Please enter a numeric value.", "Bank Customer App"); 
      

  5.   

    我并没有“关闭的按钮难道也和那个TEXTBOX关联的啊?”关闭的按钮,只是选择了form上的的关闭。
      

  6.   

    那你就用窗体的FORMCLOSING事件啊?不知道你怎么弄的,不然就是我没理解!
    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;
                }
            } 
      

  7.   

    首先非常感谢你!
    但是按照你说的,
    还是出现:“Please enter a numeric value”的提示,
    我的终极目标是在关闭的时候不出现上面的提示。
      

  8.   

    catch(FormatException) 

    SWF.MessageBox.Show("Please enter a numeric value.", "Bank Customer App"); 
    e.Cancel = true; 这句话不要试试 // cancel validation, returns focus 

      

  9.   

    private void Form1_Load(object sender, System.EventArgs e) 

                txtAmount.Validating += new CancelEventHandler(Validation.IsInteger); 

    你这界面初始的时候就调用了这个事件!你把这话放在一个按钮事件里!刚开始我没看到!界面一加载就触发的肯定会这样的吧!
      

  10.   

    换成下面的代码试试
    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 
                    }
                }
            } 
      

  11.   

    可能我问得比较大,
    我也考虑过在Validation.IsInteger这个函数里面来处理,但是,他毕竟在关闭前的时刻,调用IsInteger这个函数,也就不可避免的还会要检查他的IsInteger,既然检查这个就难免出现我说的那个提示;
    我刚才也和你说了我的终极目标:“在关闭的时候不出现上面的提示。”
    不知您有什么好的方法否?
    谢谢。
      

  12.   

    http://topic.csdn.net/u/20080518/10/c3b1409c-be80-43d6-8485-c171cafd2f6b.html还是要感谢你的方法,我测试了,按照你新的代码:“在关闭的时候不出现上面的提示。” ,但是如果我在需要调用“比方说添加数据时”会调用IsInteger这个方法,在其他地方会出现异常,所以我想咱们不再IsInteger这个函数考虑,看看还有没有其他的办法,实现我的终极目标。比方说事件的调用的顺序等等,还有一些我想不出来的,但是各位高手能想到的方法,等等。
      

  13.   

    你在formclosing事件中将这个委托去掉试试
    txtAmount.Validating -= new CancelEventHandler(Validation.IsInteger); 
      

  14.   

    在int.Parse(txt.Text);之前可以先 string.IsNullOrEmpty(txt.Text.Trim())判断是否为空或者为nullpublic static void IsInteger(object sender, System.ComponentModel.CancelEventArgs e)
    {
        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 
        }
      

  15.   

    把委托事件去到
    在你的close 事件
    txtAmount.Validating -= new CancelEventHandler(Validation.IsInteger);