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
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
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
Last edited by skor13; Nov 18th, 2011 at 04:40 AM.
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
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?
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button Wait, I'm too old to hurry!
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
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
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
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?
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.
Last edited by skor13; Nov 18th, 2011 at 06:31 AM.
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
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