c# - 如果文本框的双值具有空值,我必须收到一条消息
问题描述
如果文本框的双值具有空值,我必须收到一条消息,我尝试了以下代码,但在第二种情况下出现错误,请帮助!
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);
}
解决方案
您应该使用 来执行此操作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
推荐阅读
- c# - 对共享资源互斥的可疑需求
- r - 有没有办法找到与其他纬度/经度点半径内的纬度/经度点相关的值?
- javascript - 如何通过 JavaScript 显示“PWA Web 应用程序图标”的计数?
- javascript - 将数据粘贴到其他网站的 CK 编辑器中,内联 CSS 在 Google Chrome 中丢失,但在 IE 和 Firefox 中没有
- c# - 如何在 xunit 自我注册测试中获得 recapcha 响应
- javascript - 如何在 mongo 中使用相同的日期格式并做出反应?
- angular - Angular 生成的没有文件名后缀的 CSS 文件
- ironpython - 从 revit 元素获取几何图形
- spring-boot - Spring boot:ERROR StatusLogger Log4j2 找不到日志记录实现
- c++ - Qt 应用程序在调用 QMessageBox::question() 时不确定地崩溃