Results 1 to 16 of 16

Thread: visual basic quiz

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    18

    visual basic quiz

    hi all, im new here and joined today as im stuck programming a visual basic quiz.

    I cannot get my questions to appear on the buttons on visual basic 2008 and do not not why not.

    thanks
    Code:
    Function randomquestion()
            Dim randomvalue As Integer
            randomvalue = 30
            randomvalue = Int(Rnd() * 20) + 1
    
            Select Case randomvalue
                Case 1
                    quebox.Text = "what is 100+29?"
                    Button1.Text = "129"
                    Button2.Text = "10029"
                    Button3.Text = "130"
                    Button4.Text = "1029"
                Case 2
                    quebox.Text = "what is 89+12?"
                    Button1.Text = "101"
                    Button2.Text = "100"
                    Button3.Text = "110"
                    Button4.Text = "102"
                Case 3
                    quebox.Text = "what is 10X6?"
                    Button1.Text = "60"
                    Button2.Text = "106"
                    Button3.Text = "600"
                    Button4.Text = "16"
                Case 4
                    quebox.Text = "what is 2X4?"
                    Button1.Text = "8"
                    Button2.Text = "24"
                    Button3.Text = "12"
                    Button4.Text = "4"
                Case 5
                    quebox.Text = "what is 100-40?"
                    Button1.Text = "60"
                    Button2.Text = "50"
                    Button3.Text = "70"
                    Button4.Text = "80"
                Case 6
                    quebox.Text = "what is 99+11?"
                    Button1.Text = "109"
                    Button2.Text = "110"
                    Button3.Text = "111"
                    Button4.Text = "120"
                Case 7
                    quebox.Text = "what is 44+44?"
                    Button1.Text = "80"
                    Button2.Text = "88"
                    Button3.Text = "84"
                    Button4.Text = "4444"
                Case 8
                    quebox.Text = "what is 50-30?"
                    Button1.Text = "10"
                    Button2.Text = "20"
                    Button3.Text = "30"
                    Button4.Text = "40"
                Case 9
                    quebox.Text = "what is 55+36?"
                    Button1.Text = "90"
                    Button2.Text = "91"
                    Button3.Text = "81"
                    Button4.Text = "101"
                Case 10
                    quebox.Text = "what is 109+12?"
                    Button1.Text = "120"
                    Button2.Text = "121"
                    Button3.Text = "116"
                    Button4.Text = "112"
                Case 11
                    quebox.Text = "what is 10X3?"
                    Button1.Text = "103"
                    Button2.Text = "13"
                    Button3.Text = "30"
                    Button4.Text = "40"
                Case 12
                    quebox.Text = "what is 50+40?"
                    Button1.Text = "80"
                    Button2.Text = "100"
                    Button3.Text = "90"
                    Button4.Text = "70"
                Case 13
                    quebox.Text = "what is 70+25?"
                    Button1.Text = "85"
                    Button2.Text = "115"
                    Button3.Text = "105"
                    Button4.Text = "106"
                Case 14
                    quebox.Text = "what is 80+25?"
                    Button1.Text = "85"
                    Button2.Text = "115"
                    Button3.Text = "105"
                    Button4.Text = "106"
                Case 15
                    quebox.Text = "what is 10X9?"
                    Button1.Text = "109"
                    Button2.Text = "19"
                    Button3.Text = "90"
                    Button4.Text = "100"
                Case 16
                    quebox.Text = "what is 100X2?"
                    Button1.Text = "1002"
                    Button2.Text = "102"
                    Button3.Text = "100"
                    Button4.Text = "200"
                Case 17
                    quebox.Text = "what is 100-55?"
                    Button1.Text = "55"
                    Button2.Text = "65"
                    Button3.Text = "35"
                    Button4.Text = "45"
                Case 18
                    quebox.Text = "what is 45+40?"
                    Button1.Text = "95"
                    Button2.Text = "75"
                    Button3.Text = "80"
                    Button4.Text = "85"
                Case 19
                    quebox.Text = "what is 60+34?"
                    Button1.Text = "84"
                    Button2.Text = "104"
                    Button3.Text = "90"
                    Button4.Text = "94"
                Case 20
                    questionlabel.Text = "what is 100+19?"
                    Button1.Text = "120"
                    Button2.Text = "109"
                    Button3.Text = "1019"
                    Button4.Text = "119"
                Case Else
                    MsgBox("cannot produce a question at this time")
            End Select
        End Function

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,047

    Re: visual basic quiz

    Welcome to the forums, then.

    So, what is not showing up? Just the button text? There doesn't appear to be anything wrong with that part (the bit about using the archaic Rnd method is another story). It would be really weird if something is showing up in the quebox, yet not on the buttons.

    Basically, I would guess that it is doing the right thing, but that something else is undoing it later on. The thing to do is to put a breakpoint in the first line of the method, then step through the code seeing what happens. If it does, indeed, assign some text to the buttons, step on out of the function (using F11, not F10) and see what happens. Perhaps you are triggering an event that immediately erases what you just wrote.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    18

    Re: visual basic quiz

    Hi again, nothing appears in the question box either. I think it could be to do with an error I get at the end of the function which says a value can't e returned on all paths, but I don't know how to stop this error

    Cheers again

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,047

    Re: visual basic quiz

    Nope, it has nothing to do with that error. The error comes from the fact that a Function has to return a value. You aren't returning anything, nor does it look like you need to be, in which case you should be using a Sub rather than a Function.

    If you are getting nothing in any of those controls, and you aren't getting the messagebox at the end, then the code isn't being called at all, which almost certainly means that the whole function isn't being called at all. You can test this quickly and easily by putting a breakpoint on the first executable line in the method, such as the one that creates the random number (that isn't first, but it is good enough). If the execution hits the breakpoint, then you know the function is being called, if it doesn't, then the problem is somewhere else other than in the code you showed.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    18

    Re: visual basic quiz

    when I use the breakpoints it says the breakpoints cannot be hit as no executable code is associated with the lines. as you can tell im new to VB as well and dont know how to solve this problem which is probably relatively simple

    thanks again

  6. #6
    New Member Omgitsme's Avatar
    Join Date
    Apr 2012
    Location
    Florida
    Posts
    1

    Re: visual basic quiz

    Did you try using a list, then a function that returns then random number, when the button is pressed quebox = functioname

    Declaring a listbox:

    vb Code:
    1. Dim GenerateNumber As New Random
    2.     Dim MyNumberList As New List(Of Integer)

    Then you would make a function such as this:

    vb Code:
    1. Function PrintRandomNumber(ByVal ListOfNumbers As List(Of Integer)) As String
    2.         Dim ChosenNumber As Integer = ListOfNumbers(GenerateNumber.Next(ListOfNumbers.Count))
    3.         ListOfNumbers.Remove(ChosenNumber)
    4.         Return ChosenNumber.ToString
    5.     End Function

    Then add a For statement in the button press to add the number of questions you have:

    vb Code:
    1. For AddToNumberList As Integer = 1 To 19
    2.             MyNumberList.Add(AddToNumberList)
    3.         Next

    Under that for statement you would make the quebox = that number:

    vb Code:
    1. quebox.Text = PrintRandomNumber(MyNumberList)

    Then add IF and ElseIF statments under that:

    vb Code:
    1. If quebox.Text = 1 then
    2. quebox.text = "your question"
    3. button1.text = "answer"
    4. End If

    And so on.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    18

    Re: visual basic quiz

    hi, would I have to do that last section 20 times for the questions?

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    18

    Re: visual basic quiz

    I think the main issue now is the fact that the breakpoints cannot be hit as no executable code is associated with the lines, if anyone knows how to get the code to run the section I posted in the first post it would be a major help, thanks!

  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: visual basic quiz

    well, what's the rest of the code look like? how are you calling the sub? Please remember we are not mind readers (as far as I know) and none of us are sitting in your lap (again, as far as I know) so we can't see what you are seeing. We have no concept of what your code looks like ... only what you post. OK, so the code you posted needs to be a Sub rather than a Function ... OK... so what does the rest of the code look like? where is that sub? is it in the form? is it in a class, a module? on a post-it note? on the back of a cocktail napkin?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    18

    Re: visual basic quiz

    hi again, I had hoped you were mind readers to call it ive got a button that when pressed generate the random question, it has Call questiongen("") as the coding behind the button but it now says argument not specified for parameter 'e'. once again its probably something incredibly simple but im new to vb, thanks again

  11. #11
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,543

    Re: visual basic quiz

    post the code.... don't tell us about it.... SHOW us... the error you're giving doesn't jive with anything you've posted so far....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    18

    Re: visual basic quiz

    hi again, the code is below, Im new to VB remember thank you once again



    Public Class matthewtheme
    'question counter
    Dim questioncounter As Integer
    Dim score As Integer
    call



    Sub questiongen(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



    Dim randomvalue As Integer
    randomvalue = 30
    randomvalue = Int(Rnd() * 20) + 1

    Select Case randomvalue
    Case 1
    quebox.Text = "what is 100+29?"
    Button1.Text = "129"
    Button2.Text = "10029"
    Button3.Text = "130"
    Button4.Text = "1029"
    Case 2
    quebox.Text = "what is 89+12?"
    Button1.Text = "101"
    Button2.Text = "100"
    Button3.Text = "110"
    Button4.Text = "102"
    Case 3
    quebox.Text = "what is 10X6?"
    Button1.Text = "60"
    Button2.Text = "106"
    Button3.Text = "600"
    Button4.Text = "16"
    Case 4
    quebox.Text = "what is 2X4?"
    Button1.Text = "8"
    Button2.Text = "24"
    Button3.Text = "12"
    Button4.Text = "4"
    Case 5
    quebox.Text = "what is 100-40?"
    Button1.Text = "60"
    Button2.Text = "50"
    Button3.Text = "70"
    Button4.Text = "80"
    Case 6
    quebox.Text = "what is 99+11?"
    Button1.Text = "109"
    Button2.Text = "110"
    Button3.Text = "111"
    Button4.Text = "120"
    Case 7
    quebox.Text = "what is 44+44?"
    Button1.Text = "80"
    Button2.Text = "88"
    Button3.Text = "84"
    Button4.Text = "4444"
    Case 8
    quebox.Text = "what is 50-30?"
    Button1.Text = "10"
    Button2.Text = "20"
    Button3.Text = "30"
    Button4.Text = "40"
    Case 9
    quebox.Text = "what is 55+36?"
    Button1.Text = "90"
    Button2.Text = "91"
    Button3.Text = "81"
    Button4.Text = "101"
    Case 10
    quebox.Text = "what is 109+12?"
    Button1.Text = "120"
    Button2.Text = "121"
    Button3.Text = "116"
    Button4.Text = "112"
    Case 11
    quebox.Text = "what is 10X3?"
    Button1.Text = "103"
    Button2.Text = "13"
    Button3.Text = "30"
    Button4.Text = "40"
    Case 12
    quebox.Text = "what is 50+40?"
    Button1.Text = "80"
    Button2.Text = "100"
    Button3.Text = "90"
    Button4.Text = "70"
    Case 13
    quebox.Text = "what is 70+25?"
    Button1.Text = "85"
    Button2.Text = "115"
    Button3.Text = "105"
    Button4.Text = "106"
    Case 14
    quebox.Text = "what is 80+25?"
    Button1.Text = "85"
    Button2.Text = "115"
    Button3.Text = "105"
    Button4.Text = "106"
    Case 15
    quebox.Text = "what is 10X9?"
    Button1.Text = "109"
    Button2.Text = "19"
    Button3.Text = "90"
    Button4.Text = "100"
    Case 16
    quebox.Text = "what is 100X2?"
    Button1.Text = "1002"
    Button2.Text = "102"
    Button3.Text = "100"
    Button4.Text = "200"
    Case 17
    quebox.Text = "what is 100-55?"
    Button1.Text = "55"
    Button2.Text = "65"
    Button3.Text = "35"
    Button4.Text = "45"
    Case 18
    quebox.Text = "what is 45+40?"
    Button1.Text = "95"
    Button2.Text = "75"
    Button3.Text = "80"
    Button4.Text = "85"
    Case 19
    quebox.Text = "what is 60+34?"
    Button1.Text = "84"
    Button2.Text = "104"
    Button3.Text = "90"
    Button4.Text = "94"
    Case 20
    quebox.Text = "what is 100+19?"
    Button1.Text = "120"
    Button2.Text = "109"
    Button3.Text = "1019"
    Button4.Text = "119"
    Case Else
    MsgBox("cannot produce a question at this time")
    End Select
    End Sub

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    18

    Re: visual basic quiz

    hello again, sorted it out now thanks, the code above works properly.

    now though when the question appears its the same one each time, question 15

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,047

    Re: visual basic quiz

    That is probably due to your use of Rnd. I don't see a call to Randomize anywhere, and without that I have no idea what Rnd will return. You shouldn't be using Rnd anyways, with or without Randomize, because Rnd is legacy. Instead, you should be using the Random object. Create just one instance at form scope, and call the .Next method to get the random number. It's easier than Rnd, anyways.
    My usual boring signature: Nothing

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    18

    Re: visual basic quiz

    hi again, would it be easier to store the questions as I have done above of would it be easier to declare each question and answer eg "dim q1 as string, q1="do you like cheese?"" and then when choosing a random question doing this "Case 1 quebox.Text = q1"?

    thanks

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,047

    Re: visual basic quiz

    I'd store them in a List(of String). After all, if you do it as a bunch of individual variables, you would have to add new variables and new Case statements if you wanted to add a single new question. With a List, you wouldn't need a Case statement, and adding a new item would be as easy as calling List.Add(<your new item here>)
    My usual boring signature: Nothing

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