二进制加法器验证检查

时间:2013-10-08 14:15:07

标签: .net vb.net

我目前在使用我的二进制加法器的验证检查时遇到错误,但只检查了它的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

谢谢:)

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我在你的代码中看到了一些东西......首先,你在ElseIf中有你的For循环来检查字段是否为数字。其次,在你的For循环中,你正在评估decnum而不是你的计数器,i。试试这个:

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