-
Getting inputs values to display true or false
Hello I have several text boxes that I will input answers into and I want them to catch the values if they are incorrect or correct.
Example: the user will input a Letter for the answer, A or a, and I was curious to how the correct input can detect if the answer is correct.
If the answer is A B C or D then no message should display but when it's some other value this message will come up.
How do I use true or false in this code?
Code:
Dim dbl1 As Double
dbl1 = CDbl(txt1.Text)
If dbl1 Not 'A OR a' Then
'Display message
MessageBox.Show("Enter A, B, C, or D")
-
Re: Getting inputs values to display true or false
If you use regular expressions, you can get a boolean with isMatch(). In this case it accepts one character, either upper/lowercase of A through D.
Code:
Imports System.Text.RegularExpressions
Code:
If Regex.IsMatch(TextBox1.Text, "[A-Da-d]") Then
MessageBox.Show("Match")
End If
However, it may just be easier to convert all texboxes to uppercase and look for the specified answer that way.
Code:
Select Case TextBox1.Text.ToUpper
Case "A"
'do stuff
Case "B"
'etc....
End Select
-
Re: Getting inputs values to display true or false
How come when I do
Code:
Imports System.Text.RegularExpressions
If regex.IsMatch(txt1.Text, "[A-D]") Then
I get a Syntax error for Imports
and "'IsMatch' is not a member of string"
Im really new to this program and taking an intro course so I barely know basics
-
Re: Getting inputs values to display true or false
put imports outside the class, so at the very top of your code.
Code:
Imports System.Text.RegularExpressions
Public Class Form1
'the rest of your code
End Class
-
Re: Getting inputs values to display true or false
So I have a problem where I am given a list of 20 answers to a quiz. All answers are A-D. The user will input into 20 different text boxes. If the user gets it rights it will add to a total count of 20. If it user enters anything beside A-D then an error message will display. At the end of the program, the number of correct answers will display, and if the user gets 15 or more then a message will display that you have passed the test, if the user gets less then it will display a fail message.
Anyways, not for you to do it. but how do I do an ItemCount for correct answers? Would it be
Code:
Dim intCount As Integer
If str1.IsMatch(txt1.Text, "[A-D]") Then
intCount += 1
Select Case txt1.Text.ToUpper
Case "A"
Case "B"
Case "C"
Case "D"
End Select
If Not MessageBox.Show("Enter A,B,C or D") Then
End If
'Holds Values
str1 = CDbl(txt1.Text)
str2 = CDbl(txt2.Text)
str3 = CDbl(txt3.Text)
str4 = CDbl(txt4.Text)
str5 = CDbl(txt5.Text)
str6 = CDbl(txt6.Text)
str7 = CDbl(txt7.Text)
str8 = CDbl(txt8.Text)
str9 = CDbl(txt9.Text)
str10 = CDbl(txt10.Text)
str11 = CDbl(txt11.Text)
str12 = CDbl(txt12.Text)
str13 = CDbl(txt13.Text)
str14 = CDbl(txt14.Text)
str15 = CDbl(txt15.Text)
str16 = CDbl(txt16.Text)
str17 = CDbl(txt17.Text)
str18 = CDbl(txt18.Text)
str19 = CDbl(txt19.Text)
str20 = CDbl(txt20.Text)
-
Re: Getting inputs values to display true or false
You convert txt1.Text to double value and put the result in dbl1 and then you check if it is A or a, how comes?
Try this code instead
vb Code:
Select Case TextBox1.Text.ToUpper
Case "A", "B", "C", "D"
' Check if it is the correct answer
Case Else
'Display message
MessageBox.Show("Enter A, B, C, or D")
End Select
Edit:
Sorry i replayed without seeing above replay
-
Re: Getting inputs values to display true or false
try this for me:
Code:
Dim score As Integer = 0
For i As Integer = 1 To 20
Dim txt As TextBox = DirectCast(Me.Controls("txt" & i), TextBox)
If Regex.IsMatch(txt.Text, "[A-Da-d]") Then
score += 1
End If
Next
MessageBox.Show("Score: " & score)
Also, as 4x2y mentions, you don't need to use a double when the only thing you're working with is a string.
-
Re: Getting inputs values to display true or false
During my debugging program I get Object Reference not set to an instance of an object
Code:
If Regex.IsMatch(txt1.Text, "[A-Da-d]") Then
This is what I have so far
Code:
Dim score As Integer = 0
For i As Integer = 1 To 20
Dim txt1 As TextBox = DirectCast(Me.Controls("B" & i), TextBox)
If Regex.IsMatch(txt1.Text, "[A-Da-d]") Then
score += 1
End If
Next
If score >= 15 Then
MessageBox.Show("You have passed the Test!")
End If
If score < 15 Then
MessageBox.Show("You have failed the Test!")
End If
'Holds Values
str1 = CStr(txt1.Text)
str2 = CStr(txt2.Text)
str3 = CStr(txt3.Text)
str4 = CStr(txt4.Text)
str5 = CStr(txt5.Text)
str6 = CStr(txt6.Text)
str7 = CStr(txt7.Text)
str8 = CStr(txt8.Text)
str9 = CStr(txt9.Text)
str10 = CStr(txt10.Text)
str11 = CStr(txt11.Text)
str12 = CStr(txt12.Text)
str13 = CStr(txt13.Text)
str14 = CStr(txt14.Text)
str15 = CStr(txt15.Text)
str16 = CStr(txt16.Text)
str17 = CStr(txt17.Text)
str18 = CStr(txt18.Text)
str19 = CStr(txt19.Text)
str20 = CStr(txt20.Text)
-
Re: Getting inputs values to display true or false
I know I just have to do the part for all 20 of them but I just am trying to get the first one to work
Code:
Dim score As Integer = 0
For i As Integer = 1 To 20
Dim txt1 As TextBox = DirectCast(Me.Controls("B" & i), TextBox)
If Regex.IsMatch(txt1.Text, "[A-Da-d]") Then
score += 1
End If
Next
-
Re: Getting inputs values to display true or false
No no no, you changed everything on what I gave you. What the code does is creates an object of TextBox and iterates from the actual TextBoxes called txt1 through txt20. For each textbox, itchecks to see if it matches the letters A through D and if it does, adds 1 to the score.
replace everything you have there with this:
Code:
Dim score As Integer = 0
For i As Integer = 1 To 20
Dim txt As TextBox = DirectCast(Me.Controls("txt" & i), TextBox)
If Regex.IsMatch(txt.Text, "[A-Da-d]") Then
score += 1
End If
Next
If score >= 15 Then
MessageBox.Show("You have passed the Test!")
End If
If score < 15 Then
MessageBox.Show("You have failed the Test!")
End If
-
Re: Getting inputs values to display true or false
So now how Do I declare if the Answer for the problem is correct or incorrect?
like
Code:
If str1 = "B" Then
score += 1
End If
Is that correct?
-
Re: Getting inputs values to display true or false
I would make an array of the correct answers ahead of time, and with an index of the loop (in this case 'i') you could compare the supplied answer to the correct answer.
Code:
Dim answers As String() = {"A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D"}
Dim score As Integer = 0
For i As Integer = 1 To 20
Dim txt As TextBox = DirectCast(Me.Controls("txt" & i), TextBox)
If Regex.IsMatch(txt.Text, "[A-Da-d]") Then
If answers(i-1) = txt.Text.ToUpper Then score += 1
End If
Next
If score >= 15 Then
MessageBox.Show("You have passed the Test!")
End If
If score < 15 Then
MessageBox.Show("You have failed the Test!")
End If
-
Re: Getting inputs values to display true or false
I'm not quite sure how to correspond the array string to the index number
Would it be like this?
Code:
Dim strAnswers() As String = {"B", "D", "A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"}
Dim score As Integer = 0
For i As Integer = 1 To 20
Dim txt As TextBox = DirectCast(Me.Controls("txt" & i), TextBox)
If Regex.IsMatch(txt.Text, "[A-Da-d]") Then
score += 1
End If
Next
Or more like this
Code:
strAnswers(0) = "B"
-
Re: Getting inputs values to display true or false
The first part, yes. I edited my last post, let me know if that works for you.
-
Re: Getting inputs values to display true or false
how would I declare the array to that specific answer. for example, like answer 20 to what they put in.
Code:
For str1 = strAnswers(0) Then
score += 1
Next
-
Re: Getting inputs values to display true or false
Read post 14, then look at the code in post 12.
-
1 Attachment(s)
Re: Getting inputs values to display true or false
I'm really confused at what I should be doing next now. I get a running error when I run the program. If you could take a quick look. I'm stumped
-
Re: Getting inputs values to display true or false
Some questions on your setup
Is it correct that all entries like A,B,C or D (upper or lower case) are correct and make an increase in the overall result?
Shouldn't it be like A,B,c or D are usable answers, however only one of them will result in an increase of score?
-
Re: Getting inputs values to display true or false
I believe with this statement, it includes the lowercase as well. It just turns it into an uppercase.
Code:
Select Case txt1.Text.ToUpper
If answers(i - 1) = txt.Text.ToUpper Then score += 1
Case "A", "B", "C", "D"
' Check if it is the correct answer
Case Else
'Display message
MessageBox.Show("Enter A, B, C, or D")
End Select
The one thing I can't seem to understand is the running error I get
Code:
If Regex.IsMatch(txt.Text, "[A-Da-d]") Then
-
Re: Getting inputs values to display true or false
Okay, I found the issue with the code you currently uploaded. You had your textboxes within a GroupBox, so the "Me.Controls" portion wouldn't work and would cause an error. I replaced it with "GroupBox1.Controls" and am having it work correctly. Also I cleaned up a little of the code.
Code:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
For i As Integer = 1 To 20
DirectCast(GroupBox1.Controls("txt" & i), TextBox).Clear()
Next
End Sub
Private Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScore.Click
Dim answers As String() = {"A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D"}
Dim score As Integer = 0
For i As Integer = 1 To 20
Dim txt As TextBox = DirectCast(GroupBox1.Controls("txt" & i), TextBox)
If Regex.IsMatch(txt.Text, "[A-Da-d]") Then
If answers(i - 1) = txt.Text.ToUpper Then score += 1
End If
Next
If score >= 15 Then
MessageBox.Show("You have passed the Test!")
End If
If score < 15 Then
MessageBox.Show("You have failed the Test!")
End If
End Sub
End Class
-
Re: Getting inputs values to display true or false
How come when I put random answers that equal less then 15 I get this running error
"Object Reference not set to an instance of an object"
Code:
If score < 15 Then
MessageBox.Show("You have failed the Test!")
End If
-
Re: Getting inputs values to display true or false
Even when I put all the right answers It still displays that I have failed the test?
-
Re: Getting inputs values to display true or false
With what I have above, i can put any alphanumeric key and even symbols with no error. So it leads me to believe that you may have changed the code? What are you putting in that is random?
-
Re: Getting inputs values to display true or false
Well look at post #22, for some reason nothing is making me pass the test?
-
Re: Getting inputs values to display true or false
I did. And with the code in post 20 exactly as it is shown there, and typing the answer from the code into the boxes, it passes just fine. Are you SURE you didn't change the code again like you did before?
-
1 Attachment(s)
Re: Getting inputs values to display true or false
This is what I have in total. I need the answer form for later but as of right now. it just fails me with my inputs
-
Re: Getting inputs values to display true or false
Okay, you're clearly not listening when I mentioned to replace all of the code.
Here it is again...
Code:
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
For i As Integer = 1 To 20
DirectCast(GroupBox1.Controls("txt" & i), TextBox).Clear()
Next
End Sub
Private Sub btnScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScore.Click
Dim answers As String() = {"A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D"}
Dim score As Integer = 0
For i As Integer = 1 To 20
Dim txt As TextBox = DirectCast(GroupBox1.Controls("txt" & i), TextBox)
Dim lbl As Label = DirectCast(AnswerForm.GroupBox1.Controls("label" & i), Label)
lbl.Text = i & ": " & DirectCast(GroupBox1.Controls("txt" & i), TextBox).Text
If Regex.IsMatch(txt.Text, "[A-Da-d]") Then
If answers(i - 1) = txt.Text.ToUpper Then
score += 1
Else
lbl.ForeColor = Color.Red
End If
End If
Next
If score >= 15 Then
MessageBox.Show("You have passed the Test!")
End If
If score < 15 Then
MessageBox.Show("You have failed the Test!")
End If
AnswerForm.Show()
End Sub
End Class
EDIT: I noticed what you were also trying to do with the second form and added a loop at the end to make those changes as well. Of course the ordering of those labels are in the wrong location on the form though.
EDIT2: Condensed code.
-
1 Attachment(s)
Re: Getting inputs values to display true or false
Ok this is my final product and I am not getting any pass on this test at all. I'm not sure if it is my program that I was I constantly opening other projects but I still get the same results of failure
-
1 Attachment(s)
Re: Getting inputs values to display true or false
-
Re: Getting inputs values to display true or false
I see why I keep getting fail. Everything is mixed up. Why is this?
http://i.imgur.com/at0Ng.jpg
-
Re: Getting inputs values to display true or false
Update the answers array on the button click to the answers you want. I would have figured you knew the answers you wanted.
-
Re: Getting inputs values to display true or false
Wow, my answers got switched around a little bit. Thank you so much for everything.
For everyone else that reads this. It's really the little errors that screw over your whole program. +1 skor13
One more thing. How can I make sure that the only values that are entered are only A, B, C, or D?
I tried this but it seems to fail
Code:
Select Case txt1.Text.ToUpper
Case "A", "B", "C", "D"
' Check if it is the correct answer
Case Else
'Display message
MessageBox.Show("Enter A, B, C, or D")
End Select
-
Re: Getting inputs values to display true or false
Add this Sub to the code and remove what you just posted entirely.
Code:
Private Sub txt1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt9.KeyDown, txt8.KeyDown, txt7.KeyDown, txt6.KeyDown, txt5.KeyDown, txt4.KeyDown, txt3.KeyDown, txt20.KeyDown, txt2.KeyDown, txt19.KeyDown, txt18.KeyDown, txt17.KeyDown, txt16.KeyDown, txt15.KeyDown, txt14.KeyDown, txt13.KeyDown, txt12.KeyDown, txt11.KeyDown, txt10.KeyDown, txt1.KeyDown
If Not (e.KeyCode = Keys.A Or Keys.B Or Keys.C Or Keys.D) Then
e.SuppressKeyPress = True
End If
End Sub
-
Re: Getting inputs values to display true or false
Weird, restricting keys from being pressed. For some reason it will only let me press A and not B, C, or D
-
Re: Getting inputs values to display true or false
ahh shoot, sorry, change that line to
Code:
If Not (e.KeyCode = Keys.A) And Not (e.KeyCode = Keys.B) And Not (e.KeyCode = Keys.C) And Not (e.KeyCode = Keys.D) Then
-
Re: Getting inputs values to display true or false
Thanks! You've taught me a lot. Couldn't have done it without you
-
Re: Getting inputs values to display true or false
one last bit, you may want to add this to the if statement so they can delete what they wrote.
Code:
And Not (e.KeyCode = Keys.Back)
users can still type more than one character, but you can limit that by setting the length of the textboxes.