How to check textbox inputs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bresy
    New Member
    • Mar 2007
    • 4

    How to check textbox inputs

    Hi I relatively new to VB.Net. And I have to create a sort of quiz type application.
    There are a few Text boxes where the user will input their answers. I want the code to check the users input and compare with set answer. If correct add 1 to a total and at the end display the total correct answers. I have a code but I keep getting Errors like cannot convert boolean to integer etc. Any help please..

    this is my code at the moment what is wrong?

    Private Sub btnCheck_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnCheck.Click
    Dim TotalAns As Integer
    Dim txt As Boolean

    Select Case txt
    Case txtRep1.Text = "A"
    TotalAns += 1
    Case txtRep2.Text = "B"
    TotalAns += 1
    End Select
    txtResults.Text = Str(TotalAns)
    End Sub
    End Class
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Hi,

    Why are you taking txt as Boolean.Its not used anyway in the procedure.
    Can you write the procedure like this?

    Private Sub btnCheck_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnCheck.Click

    Dim TotalAns As Integer
    Dim txt As String

    txt=txtRep1.Tex t

    Select Case txt
    Case "A"
    TotalAns += 1
    Case "B"
    TotalAns += 1
    End Select

    txtResults.Text = Str(TotalAns)
    End Sub
    End Class

    Comment

    • Bresy
      New Member
      • Mar 2007
      • 4

      #3
      Sorry about that The boolean was included as that was the error I got from the source. It said "Option Strict On disallows implicit conversions from 'Boolean' to 'String'."

      Your answer works well. But only for one of the text boxes. I have 2 different text boxes at the minute and at the end I will need about 30 different user inputs.

      textrep1
      textrep2
      textrep3 ... etc.

      So I need to add one to TotalAns everytime the input matches.
      I thought the select case would allow me to do this ... am I using the wrong thing?

      Comment

      • Bresy
        New Member
        • Mar 2007
        • 4

        #4
        Ok Thanks a lot for your help. I have got a solution but its very long as like I said there will be 30 different inputs. So this is how I got it to work is there any other way to make this simpler.

        Private Sub btnCheck_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnCheck.Click

        Dim TotalAns As Integer
        TotalAns = 0

        If txtRep1.Text = "A" Then
        TotalAns += 1
        End If
        If txtRep2.Text = "B" Then
        TotalAns += 1
        End If
        If txtRep3.Text = "C" Then
        TotalAns += 1
        End If

        txtResults.Text = Str(TotalAns)
        End Sub
        End Class

        Comment

        • shweta123
          Recognized Expert Contributor
          • Nov 2006
          • 692

          #5
          Hi,

          Are you not using Database table for storing questions and the answers?
          If you use that much of your code can be reduced.You can just pass the the answer from any of your 30 textboxes to the query and you need not have to compare each with "A" ,"B" ,"C" etc.

          Comment

          Working...