Results 1 to 15 of 15

Thread: [RESOLVED] Cutting off part of a label caption for a multiple choice question game.

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Resolved [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:
    1. Public Sub GetQuestionFromLabel()
    2. Dim intPos As Integer
    3.  
    4. Dim strQ As String
    5.  
    6. intPos = InStr(PickLBL.Caption, "?")     ' Locate the position of the "?" in PickLBL.Caption
    7. strQ = Mid$(PickLBL.Caption, 1, intPos - 1)     ' Copy up to but not including the "?" sign to strQ
    8.  
    9. QuestionLBL.Caption = strQ & "?"
    10. 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!

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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:
    1. RightANS.Caption = Trim$(Mid$(PickLBL.Caption, intPos + 1))

    So, now I am looking for how to get the 4 possible choices separated by commas.

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Last edited by LaVolpe; Oct 9th, 2017 at 12:33 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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:
    1. sQuestions = Split(Trim$(Left$(strQ, intPos - 1)), ",")

    Do you know where I went wrong?

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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!!

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Cutting off part of a label caption for a multiple choice question game.

    Quote Originally Posted by LaVolpe View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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:
    1. Public Sub GETCHOICES()
    2. Dim sQuestions() As String
    3. Dim intPos As Integer
    4. Dim strQ As String
    5.  
    6.     intPos = InStr(PickLBL.Caption, "?")     ' Locate the position of the "?" in PickLBL.Caption
    7.     strQ = Mid$(PickLBL.Caption, 1, intPos - 1)
    8.  
    9.     intPos = InStrRev(strQ, "*")
    10.    RightANS.Caption = Mid$(strQ, intPos + 1)
    11.  
    12.     sQuestions() = Split(Trim$(Left$(strQ, intPos - 1)), ",")
    13.  
    14. ' now you can loop 0 to 3 to get the 4 individual questions and display them individually how you'd like
    15.  
    16. For intPos = 0 To 3
    17.     ChoicesLBL(intPos).Caption = Trim$(sQuestions(intPos))
    18. Next
    19.  
    20. 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:
    1. sQuestions = Split(Trim$(Left$(strQ, intPos - 1)), ",")

    I am not sure why that is a problem?

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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:
    1. Dim lIndex As Long
    2.    If Qlist.ListCount Then
    3.       lIndex = Int(Rnd * Qlist.ListCount)
    4.       PickLBL.Caption = Qlist.List(lIndex)
    5.      Call GetQuestionFromLabel
    6.     Call GETCHOICES
    7.      
    8.     End If


    The GetQuestionFromLabel sub is this I dont think it is affecting the other sub...


    VB Code:
    1. Public Sub GetQuestionFromLabel()
    2. Dim intPos As Integer
    3.  
    4. Dim strQ As String
    5.  
    6. intPos = InStr(PickLBL.Caption, "?")     ' Locate the position of the "?" in PickLBL.Caption
    7. strQ = Mid$(PickLBL.Caption, 1, intPos - 1)     ' Copy up to but not including the "?" sign to strQ
    8.  
    9. QuestionLBL.Caption = strQ & "?"
    10.  
    11.  
    12. '''''''''''''''''
    13.  
    14.  
    15. End Sub

    This code gets the question from PickLBL.caption and put that question in QuestionLBL.caption

  10. #10
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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



  11. #11

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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!

  12. #12
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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



  13. #13

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    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!

  14. #14
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: [RESOLVED] Cutting off part of a label caption for a multiple choice question gam

    Thanks



  15. #15
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: Cutting off part of a label caption for a multiple choice question game.

    Quote Originally Posted by LaVolpe View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width