首页 > 解决方案 > 如果文本框的双值具有空值,我必须收到一条消息

问题描述

如果文本框的双值具有空值,我必须收到一条消息,我尝试了以下代码,但在第二种情况下出现错误,请帮助!

acno = Txtacc.Text;
recoverymoney = double.Parse(Txtamount.Text);

if (string.IsNullOrEmpty(this.Txtacc.Text))
{
    MessageBox.Show("You have not entered account number, Please Enter it...!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (string.IsNullOrEmpty(this.Txtamount.Text))
{
    MessageBox.Show("You have not entered amount, Please Enter it..!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

标签: c#

解决方案


您应该使用 来执行此操作double.TryParse,并且您应该在检查后实际读取值:

if (string.IsNullOrEmpty(this.Txtacc.Text))
{
    MessageBox.Show("You have not entered account number, Please Enter it...!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (string.IsNullOrEmpty(this.Txtamount.Text))
{
    MessageBox.Show("You have not entered amount, Please Enter it..!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
eise if(!double.TryParse(this.Txtamount.Text, out recoverymoney ))
{
    MessageBox.Show("You have entered an invalid amount, Please Enter a number..!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
acno = Txtacc.Text

推荐阅读