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

问题描述 投票:0回答:1

我试图总结两个文本框的内容,其中一个文本框中有数据来自数据库,但我不断得到上述错误与这些细节

    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()方法使用数据库中的数据测试文本框时,我得到true意味着内容是有效的整数

       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

谢谢。

vb.net textbox
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.