VB“从字符串“”转换为“ Double”类型无效”

时间:2019-12-26 10:38:42

标签: vba vb.net

我收到此消息:从字符串到Double类型的转换无效,我不知道该怎么做。这是代码。

    Dim Amount As String = TextBoxAmount.Text

    If Amount = "" Or Not IsNumeric(Amount) Then
        MsgBox("Incorect Format!", InfoOKOnly, AppTitle)
        TextBoxAmount.Focus()
        Return '- Exit Sub
    End If

    Dim DblAmount As Double = CType(Amount, Double)
    If (DblAmount < 500) Then
        MsgBox("Amount must be greater than or equal to 500", InfoOKOnly, AppTitle)
        TextBoxAmount.Focus()
        Return
    End If

    TextBoxAmount.Text = Format(DblAmount, "#,##0.  00")
End Sub

Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
    If (MsgBox("Do you want to continue?", vbQuestion + vbYesNo, AppTitle) = MsgBoxResult.Yes) Then
        If TextBoxAccountNo.Text = "" Or String.IsNullOrEmpty(TextBoxAccountNo.Text) Or
           TextBoxAccountName.Text = "" Or String.IsNullOrEmpty(TextBoxAccountName.Text) Or
           TextBoxPhoneNo.Text = "" Or String.IsNullOrEmpty(TextBoxPhoneNo.Text) Or
           TextBoxAmount.Text = "" Or String.IsNullOrEmpty(TextBoxAmount.Text) Then
            '--^_^
            Return
        End If

        Dim cf As MyClassFiles1 = New MyClassFiles1()

        Dim accNo As String = TextBoxAccountNo.Text
        Dim accName As String = TextBoxAccountName.Text
        Dim PhoneNo As String = TextBoxPhoneNo.Text
        Dim Cash As Double = CDbl(TextBoxAmount.Text)

问题就在这里,在这行代码中(上面第一个)。当我在文本框中输入数字(金额)时,它说不能转换为双精度数字。

        Dim o As MyAccountClass1 = New MyAccountClass1(accNo, accName, PhoneNo, Cash)

        FileOpen(1, cf.GetMasterFile, OpenMode.Append)

        '-- C stand for Create New Account
        WriteLine(1, o.AccountNo, o.AccountName, o.PhoneNo, o.Amount, "Active")

        FileOpen(2, cf.GetTransactionFile, OpenMode.Append)

        WriteLine(2, o.AccountNo, CreatedDate, CreatedTime, "C", o.Amount)

        '--Close before reopening in another mode.

        FileClose(1)
        FileClose(2)

        MsgBox("The files have been saved!", InfoOKOnly, AppTitle)

        ClearTextBoxes()
    End If
End Sub
End Class

2 个答案:

答案 0 :(得分:0)

我建议使用TryParse

    ...
    Dim Cash As Double
    If Not Double.TryParse(TextBoxAmount.Text, Cash) Then
        'do sth, show some msgbox that value in the textBox is incorrect
    End If
    ...

此外,您的支票String.IsNullOrEmpty应该替换为String.IsNullOrWhiteSpace,因为您在TextBoxAccountNo.Text = ""之前检查是否为空。

关于代码的另一件事。您想使用OrElse而不是Or 选中this question,尤其是this answer

答案 1 :(得分:0)

我认为您的问题出在这行上。

TextBoxAmount.Text = Format(DblAmount, "#,##0.  00")

Double不会接受0和00之间的空格。

您可以在“保存”按钮中组合您的验证。不要通过询问用户是否要继续来侮辱用户。

使用.net可用的System.IO方法。 File.AppendAllText打开,写入和关闭文件,为您节省几行代码。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim InputCash As Double

    If Not Double.TryParse(TextBoxAmount.Text, InputCash) OrElse InputCash < 500 Then
        MessageBox.Show("Please enter a valid amount greater than or equal to 500.")
        TextBoxAmount.Focus()
        Return '- Exit Sub
    End If


    If String.IsNullOrWhiteSpace(TextBoxAccountNo.Text) OrElse
       String.IsNullOrWhiteSpace(TextBoxAccountName.Text) OrElse
       String.IsNullOrEmpty(TextBoxPhoneNo.Text) Then
        MessageBox.Show("Please fill in all boxes.")
        Return
    End If

    Dim cf As MyClassFiles1 = New MyClassFiles1(TextBoxAccountNo.Text, TextBoxAccountName.Text, TextBoxPhoneNo.Text,InputCash)
    Dim message = cf.SaveFiles()
    MessageBox.Show(message)
End Sub

Public Class MyClassFiles1
    Public Property AccountNo As String
    Public Property AccountName As String
    Public Property PhoneNo As String
    Public Property Amount As Double

    Public Sub New(number As String, name As String, pnone As String, cash As Double)
        AccountNo = number
        AccountName = name
        PhoneNo = pnone
        Amount = cash
    End Sub

    Private Function GetMasterFile() As String
        Return "C:\SomeUser\MasterAccountFile.txt"
    End Function

    Private Function GetTransactionFile() As String
        Return "C:\SomeUser\TransactionFile.txt"
    End Function

    Public Function SaveFiles() As String
        Try
            Dim strForMaster = $"{AccountNo},{AccountName},{PhoneNo},{Amount},Active"
            File.AppendAllText(GetMasterFile(), strForMaster)
            Dim strForTransaction = $"{AccountNo},{Now.Date},{Now.ToShortTimeString},C,{Amount}"
            File.AppendAllText(GetTransactionFile(), strForTransaction)
        Catch ex As Exception
            Return ex.Message
        End Try
        Return "The files have been saved!"
    End Function
End Class