0

我目前在对我的二进制加法器进行验证检查时遇到错误,但只能检查它的 1 或 0。我希望它检查文本框是否包含从 2 到 9 以及从 a 到 z 的所有内容。我的代码目前是:

    Dim errorpass As Integer = 2
    Dim decnum As Integer = 2
    Dim errormsg As String = "ERROR:" + vbNewLine
    'Start of Error Checking
    'Checking if either textbox contains anything
    If TextBox1.Text = "" Or TextBox2.Text = "" Then
        errormsg += ("Please enter some form of input." + vbNewLine)
        errorpass = 1
        'Checking if either textbox are numbers
    ElseIf Not (IsNumeric(TextBox1.Text) Or IsNumeric(TextBox2.Text)) Then
        errormsg += ("Please enter a number." + vbNewLine)
        errorpass = 1
        'Checking if either textbox contains 1's or 0's
        For i = 2 To 9
            If TextBox1.Text.Contains(decnum) Or TextBox2.Text.Contains(decnum) Then
                errormsg += ("Please enter binary." + vbNewLine)
                errorpass = 1
            ElseIf Not TextBox1.Text.Contains("1" Or "0") Or TextBox2.Text.Contains("1" Or "0") Then
                errorpass = 1
                If decnum = 9 Then
                    decnum = 2
                Else
                    decnum += 1
                End If
            Else
                errorpass = 2
            End If
        Next

    End If
    'Processing the request
    If errorpass = 1 Then
        MsgBox(errormsg, MsgBoxStyle.Exclamation, Title:="ERROR PROCESSING YOUR REQUEST")
    ElseIf errorpass = 2 Then
        'Adder
        TextBox3.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text, 2) + Convert.ToInt32(TextBox2.Text, 2), 2)
    End If
    errorpass = 2

谢谢 :)

4

2 回答 2

0

以下是要尝试的模式的基础知识

    Dim num1 As Integer
    Dim num2 As Integer
    If Integer.TryParse(TextBox1.Text, num1) AndAlso Integer.TryParse(TextBox2.Text, num2) Then
        If Not ((num1 = 0 OrElse num1 = 1) AndAlso (num2 = 0 OrElse num2 = 1)) Then
            'not 0 or 1
            Stop
        Else
            '0 or 1
            Stop
        End If
    Else
        'error input - not a number
        Stop
    End If
于 2013-10-08T14:27:43.857 回答
0

我在您的代码中看到了几件事……首先,您在 ElseIf 中有 For 循环,用于检查字段是否为数字。其次,在您的 For 循环中,您正在评估 decnum 而不是您的计数器,即。尝试这个:

Dim errorpass As Integer = 2
Dim decnum As Integer = 2
Dim errormsg As String = "ERROR:" + vbNewLine
'Start of Error Checking
'Checking if either textbox contains anything
If TextBox1.Text = "" Or TextBox2.Text = "" Then
    errormsg += ("Please enter some form of input." + vbNewLine)
    errorpass = 1
    'Checking if either textbox are numbers
ElseIf Not (IsNumeric(TextBox1.Text) Or IsNumeric(TextBox2.Text)) Then
    errormsg += ("Please enter a number." + vbNewLine)
    errorpass = 1
Else
    'Checking if either textbox contains 1's or 0's
    For i = 2 To 9
        If TextBox1.Text.Contains(i) Or TextBox2.Text.Contains(i) Then
            errormsg += ("Please enter binary." + vbNewLine)
            errorpass = 1
        End If
    Next

End If
'Processing the request
If errorpass = 1 Then
    MsgBox(errormsg, MsgBoxStyle.Exclamation, Title:="ERROR PROCESSING YOUR REQUEST")
ElseIf errorpass = 2 Then
    'Adder
    TextBox3.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text, 2) + Convert.ToInt32(TextBox2.Text, 2), 2)
End If
errorpass = 2
于 2013-10-08T14:39:14.983 回答