输入的字符串格式不正确。 vb.net

时间:2019-03-14 15:27:15

标签: vb.net textbox

我正在尝试对两个文本框的内容求和,其中一个包含来自数据库的数据,但是由于这些详细信息,我不断收到错误消息

    System.FormatException
     HResult=0x80131537
     Message=Input string was not in a correct format.
     Source=mscorlib
     StackTrace:

    at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)

    at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)

   at System.Int32.Parse(String s)

     at SchoolManager.Pay_Fees.TxtTermFee_TextChanged(Object sender, EventArgs e) in C:\Users\MASENO\Source\Repos\SchoolManager\SchoolManager\Pay Fees.vb:line 271

    at System.Windows.Forms.Control.OnTextChanged(EventArgs e)

   at System.Windows.Forms.TextBoxBase.OnTextChanged(EventArgs e)

   at System.Windows.Forms.Control.set_Text(String value)

  at System.Windows.Forms.TextBoxBase.set_Text(String value)

  at System.Windows.Forms.TextBox.set_Text(String value)

  at SchoolManager.Pay_Fees.InitializeComponent() in 

  C:\Users\MASENO\Source\Repos\SchoolManager\SchoolManager\Pay Fees.Designer.vb:line 158
 at SchoolManager.Pay_Fees..ctor() in C:\Users\MASENO\Source\Repos\SchoolManager\SchoolManager\Pay Fees.vb:line 9

当我使用tryparse()方法使用数据库中的数据测试文本框时,我得到了真,意味着内容是有效的整数

       TxtTotalFee.Text = Integer.TryParse(TxtPBalance.Text,0)

如何获得两个数据?

这是我的代码

   Private Sub TxtTermFee_TextChanged(sender As Object, e As EventArgs) Handles TxtTermFee.TextChanged
    TxtTotalFee.Text = Integer.Parse(TxtPBalance.Text) + Integer.Parse(TxtTermFee.Text)
End Sub

谢谢。

1 个答案:

答案 0 :(得分:2)

除了不使用转换后的值之外,您使用tryparse()处在正确的轨道上。

将代码更改为此。

    Private Sub TxtTermFee_TextChanged(sender As Object, e As EventArgs) Handles TxtTermFee.TextChanged
    ' Input String.
    Dim value As String = TxtPBalance.Text

    ' Use Integer.TryParse.
    Dim i As Integer
    If (Integer.TryParse(value, i)) Then
        If TxtTermFee.Text = "" Then
        Else
            TxtTotalFee.Text = i + TxtTermFee.Text
        End If

    End If
End Sub