输入字符串的文本框格式不正确 vb visual basic 2010

input string was not in a correct format textbox vb visual basic 2010

我不知道该怎么做了,我只是在学习教程,我确定输入正确了吗?但它一直说输入字符串的格式不正确,我正在尝试总计饮料请帮助我已经第三次重新启动这个项目

您正在尝试将空字符串转换为双精度值,这是不可能发生的。您应该:

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

这是使用后者的示例:

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 (documentation):

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