文本框和标签不匹配问题! vb6

Mismatch problem with textbox and label! vb6

我已经尝试更改代码但没有成功。 这是我的代码:

Private Sub CTcash_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        bill = Val(CTcash.Text - CLtotaloutput.Caption)
        If Val(bill) > 0 Then
            Change.CLchange.Caption = Val(bill)
            Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
            Change.Show
        End If
        If Val(bill) < 0 Then
            MsgBox "Not Enough Money", vbOKOnly, "Invalid"
        End If
    End If
End Sub

突出显示的代码

bill = Val(CTcash.Text - CLtotaloutput.Caption)

CTcash.Text和CLTotaloutput.Caption都是字符串,不能直接相减。尝试:

bill = Val(CTcash.text) - Val(CLtotaloutput.Caption)

你真的应该首先确保两个字符串都是数字!

@John Eason 说得对。您也不必在 bill 上使用 Val,因为它已经是一个整数。如果 bill 为 0,您什么都不做,但根据您的要求,这可能没问题。

Private Sub CTcash_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        bill = Val(CTcash.Text) - Val(CLtotaloutput.Caption)
        If bill > 0 Then ' Note that when bill is 0 nothing happens but that might be fine
            'Change.CLchange.Caption = bill 'Not sure why this is needed but it may be
            Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
            Change.Show
        End If
        If bill < 0 Then
            MsgBox "Not Enough Money", vbOKOnly, "Invalid"
        End If
    End If
End Sub

我认为更好的方法是使用 Select Case 语句。

Private Sub CTcash_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        bill = Val(CTcash.Text) - Val(CLtotaloutput.Caption)

        Select Case True
            Case bill > 0 
               Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
               Change.Show 
            Case bill < 0
               MsgBox "Not Enough Money", vbOKOnly, "Invalid"
            Case Else
               ' Do nothing - this documents that this is done on purpose
        End Select
     
    End If
End Sub

您可以使用 IsNumeric 函数来检查输入是否为数字并进行相应的解析。使用 Right 函数从左侧删除任何非数字。例如,Right("3", Len("3") - 1) 将删除前导 $.

从字符串中去除所有非数值:

Dim ResultString As String
myString = "aaa34BB12,000xcv9.9zz"

Dim i As Integer

For i = 1 To Len(myString)
    myChar = Mid(myString, i, 1)
    If IsNumeric(myChar) = True Then
        ResultString = ResultString + myChar
    End If
Next

MsgBox ResultString

请注意,这可能会导致小数金额出现问题,因为 1.23 会早于 123。这不是您想要的。尝试使用 CCur 函数。我自己从未使用过它,但它可能正是您想要的。

来源:https://www.vbforums.com/showthread.php?437526-RESOLVED-Removing-non-numeric-chars-from-a-string&p=2686411&viewfull=1#post2686411

起初我错过了const,我把它放在另一个sub:

Private Sub CTcash_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        bill = Val(CTcash.Text) - totalsum
        Select Case True
            Case bill > 0
               Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
               Change.Show
            Case bill < 0
               MsgBox "Not Enough Money", vbOKOnly, "Invalid"
            Case Else
        End Select
    End If
End Sub
Private Sub CTbarcode_KeyUp(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case Is = vbKeyF6
            totaldiscount = 200
            totalsum = Val(CLsuboutput.Caption) - totaldiscount
            CLtotaloutput.Caption = Format(totalsum, "Rp\. ###,###,###. ,-")
            CLdiscoutput.Caption = Format(totaldiscount, "Rp\. ###,###,###. ,-")
            CTcash.Enabled = True
            CTcash.SetFocus
    End Select
End Sub

这是我编辑后的样子:

Private Sub CTcash_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
        totaldiscount = 200
        totalsum = Val(CLsuboutput.Caption) - totaldiscount
        bill = Val(CTcash.Text) - totalsum
        Select Case True
            Case bill > 0
               Change.CLchange.Caption = Format(bill, "Rp\. ###,###,###.-")
               Change.Show
            Case bill < 0
               MsgBox "Not Enough Money", vbOKOnly, "Invalid"
            Case Else
        End Select
    End If
End Sub