首页 > 解决方案 > 输入字符串的格式不正确 文本框 vb Visual Basic 2010

问题描述

图片在这里

我不知道该做什么了,我只是按照一个教程来确保我输入正确?但它一直说输入字符串的格式不正确,我试图总计饮料请帮助我已经第三次重新启动这个项目

标签: vb.net

解决方案


您正在尝试将空字符串转换为 Double,但这是不可能的。您应该:

  1. 用 NumericUpDown 控件替换您的 TextBox 控件(文档
  2. 将 Convert.ToDouble 替换为 Double.TryParse(文档

这是使用后者的示例:

Dim cider_soda As Double
If (Not Double.TryParse(tx1.Text, cider_soda)) Then
    MessageBox.Show("The value in tx1 cannot be converted to a Double.")
End If

如果您想在文本为空的情况下进行具体区分,请使用 String.IsNullOrWhitespace (文档):

Dim cider_soda As Double
If (String.IsNullOrWhitespace(tx1.Text)) Then
    MessageBox.Show("The value in tx1 cannot be empty.")
ElseIf (Not Double.TryParse(tx1.Text, cider_soda)) Then
    MessageBox.Show("The value in tx1 cannot be converted to a Double.")
End If

推荐阅读