[RESOLVED] Cutting off part of a label caption for a multiple choice question game.
Hi there folks! I am working on another review game for my class! :) They loved the first one!
What I am doing this time, is I am typing a series of questions with 4 possible choices(sort of like who wants to be a millionaire kind of thing)
The questions look like this in the textfile
How many provinces are there in Canada? 5, 10, 13, 15 *10
The *10 in this case would be the correct answer. The program picks a random question, and puts the line in a label caption called PickLBL. I would like to separate the 4 possible choices for answers and put them in a label array called ChoiceLBL(0)-(3). All questions will have 4 choices. The one with the *before it is the correct answer and I would like to cut off that and put it into the RightANS.caption, so when the student picks their choice, the program will compare their choice to the right answer to see if it is the correct answer.
What I have done so far is used MID$ to cut off the question itself
VB Code:
Public Sub GetQuestionFromLabel()
Dim intPos As Integer
Dim strQ As String
intPos = InStr(PickLBL.Caption, "?") ' Locate the position of the "?" in PickLBL.Caption
strQ = Mid$(PickLBL.Caption, 1, intPos - 1) ' Copy up to but not including the "?" sign to strQ
QuestionLBL.Caption = strQ & "?"
End Sub
Now that I have the question I am stuck on how to cut of the multiple label captions because they are separated by commas. I can use RIGHT$ (I think) to cut off one of those answers with a comma, but would it work with 4 commas separating possible choices?
Your expertise in this project is greatly appreciated as always!
Thanks!
Re: Cutting off part of a label caption for a multiple choice question game.
Ok, so to get the correct answer cut off from the PICKLBL.caption, I used this...
Vb Code:
RightANS.Caption = Trim$(Mid$(PickLBL.Caption, intPos + 1))
So, now I am looking for how to get the 4 possible choices separated by commas.
Re: Cutting off part of a label caption for a multiple choice question game.
You can use Split()
Code:
Dim sQuestions() As String
Dim intPos As Integer
Dim strQ As String
intPos = InStr(PickLBL.Caption, "?") ' Locate the position of the "?" in PickLBL.Caption
strQ = Mid$(PickLBL.Caption, 1, intPos - 1)
intPos = InStrRev(strQ,"*")
RightANS.Caption = Mid$(strQ, intPos+1)
sQuestions() = Split(Trim$(Left$(strQ, intPos-1)), ",")
' now you can loop 0 to 3 to get the 4 individual questions and display them individually how you'd like
Edited: Misread your original description. The answer is not preceded with a comma, so the above would need to be tweaked... updated
Re: Cutting off part of a label caption for a multiple choice question game.
Thank you, I went back to change the question so the commas are preceding the choices, for example..
How many provinces are there in Canada? ,5 ,10 ,13 ,15* 10
However, for some reason I am getting an "invalid procedure call or argument" on this line of code.
VB Code:
sQuestions = Split(Trim$(Left$(strQ, intPos - 1)), ",")
Do you know where I went wrong? :)
Re: Cutting off part of a label caption for a multiple choice question game.
I tweaked/updated my sample code to use your previous formatting.
Re: Cutting off part of a label caption for a multiple choice question game.
thank you for the update! Do you know how I could set up the loop to put sQuestions into the ChoicesLBL caption array for 0-3?
Thanks again!!
Re: Cutting off part of a label caption for a multiple choice question game.
Quote:
Originally Posted by
LaVolpe
You can use Split()
Code:
Dim sQuestions() As String
Dim intPos As Integer
Dim strQ As String
intPos = InStr(PickLBL.Caption, "?") ' Locate the position of the "?" in PickLBL.Caption
strQ = Mid$(PickLBL.Caption, 1, intPos - 1)
intPos = InStrRev(strQ,"*")
RightANS.Caption = Mid$(strQ, intPos+1)
sQuestions() = Split(Trim$(Left$(strQ, intPos-1)), ",")
' now you can loop 0 to 3 to get the 4 individual questions and display them individually how you'd like
In the above, I mentioned that... notice the last comment.
Code:
For intPos = 0 To 3
ChoicesLBL(intPos).Caption = Trim$(sQuestions(intPos))
Next
Assumption is your format is like you stated in post #1: 5, 10, 13, 15 *10
Edited: I'll check back at half-time. Gonna watch a little Chicago Bears Monday Night Football. Just finished watching my Cubs take the 2-1 lead in the league series.
Re: Cutting off part of a label caption for a multiple choice question game.
Thank you for the quick response!
Ok, so now the code looks like...
VB Code:
Public Sub GETCHOICES()
Dim sQuestions() As String
Dim intPos As Integer
Dim strQ As String
intPos = InStr(PickLBL.Caption, "?") ' Locate the position of the "?" in PickLBL.Caption
strQ = Mid$(PickLBL.Caption, 1, intPos - 1)
intPos = InStrRev(strQ, "*")
RightANS.Caption = Mid$(strQ, intPos + 1)
sQuestions() = Split(Trim$(Left$(strQ, intPos - 1)), ",")
' now you can loop 0 to 3 to get the 4 individual questions and display them individually how you'd like
For intPos = 0 To 3
ChoicesLBL(intPos).Caption = Trim$(sQuestions(intPos))
Next
End Sub
And the example question look like...
How many provinces are there in Canada? 5, 10, 13, 15 *10
However, I think I made a mistake somewhere, I keep getting an ("invalid procedure call or argument") error on this line..
VB Code:
sQuestions = Split(Trim$(Left$(strQ, intPos - 1)), ",")
I am not sure why that is a problem?
Re: Cutting off part of a label caption for a multiple choice question game.
I'll give some more info. I press a command button to grab a random question from the Qlist listbox where all questions are stored on formload.
When I press the command1 button it calls this..
VB Code:
Dim lIndex As Long
If Qlist.ListCount Then
lIndex = Int(Rnd * Qlist.ListCount)
PickLBL.Caption = Qlist.List(lIndex)
Call GetQuestionFromLabel
Call GETCHOICES
End If
The GetQuestionFromLabel sub is this I dont think it is affecting the other sub...
VB Code:
Public Sub GetQuestionFromLabel()
Dim intPos As Integer
Dim strQ As String
intPos = InStr(PickLBL.Caption, "?") ' Locate the position of the "?" in PickLBL.Caption
strQ = Mid$(PickLBL.Caption, 1, intPos - 1) ' Copy up to but not including the "?" sign to strQ
QuestionLBL.Caption = strQ & "?"
'''''''''''''''''
End Sub
This code gets the question from PickLBL.caption and put that question in QuestionLBL.caption
Re: Cutting off part of a label caption for a multiple choice question game.
Let's do it in one sub
Code:
ShowQuestion Qlist.List(lIndex)
Code:
Private Sub ShowQuestion(ByVal strText As String)
Dim strChoices() As String
Dim intPos1 As Integer
Dim intPos2 As Integer
intPos1 = InStr(strText, "?") ' Locate the position of the "?"
intPos2 = InStrRev(strText, "*") ' Locate the position of the "*"
QuestionLBL.Caption = Mid$(strText, 1, intPos1 - 1) ' pick question
RightANS.Caption = Mid$(strText, intPos2 + 1) ' pick answer
strChoices() = Split(Trim$(Mid$(strText, intPos1 + 1, intPos2 - intPos1 - 1)), ",") ' pick what between intPos1 and intPos2
For intPos1 = 0 To 3
ChoicesLBL(intPos1).Caption = Trim$(strChoices(intPos1))
Next
End Sub
Re: Cutting off part of a label caption for a multiple choice question game.
Ok. :) Sorry, can you tell me where I would declare the line ShowQuestion Qlist.List(lIndex)
Thank you!
Re: Cutting off part of a label caption for a multiple choice question game.
Here
Code:
Dim lIndex As Long
If Qlist.ListCount Then
lIndex = Int(Rnd * Qlist.ListCount)
ShowQuestion Qlist.List(lIndex)
End If
Re: Cutting off part of a label caption for a multiple choice question game.
Oh you are marvelous! Thank you so much for your help on this! That totally works! The kids are gonna love this!
Re: [RESOLVED] Cutting off part of a label caption for a multiple choice question gam
Re: Cutting off part of a label caption for a multiple choice question game.
Quote:
Originally Posted by
LaVolpe
Edited: I'll check back at half-time. Gonna watch a little Chicago Bears Monday Night Football. Just finished watching my Cubs take the 2-1 lead in the league series.
Hey, watch it, bro!!
Those are my Nats you are about to eliminate