Solution – Experienced Challenge 3: Are You Smarter Than a PowerPoint Developer?




What could be more fun than taking a test? OK, good point: pretty much everything is more fun than taking a test. Fortunately, in Challenge 3 you didn’t have to take the test, all you had to do was write the test. And writing a test is a whole bunch of fun.


Uh, isn’t it?


Well, fun or not, duty calls, and in Challenge 3 our duty was to create a simple one-question test using Microsoft PowerPoint. Here’s the subroutine we came up with that did just that:


Private Sub CommandButton1_Click()  


    If CommandButton1.Caption = "Start the Quiz" Then


        ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Text = _


            "Which of the following is not one of the famous Marx Brothers?"


                       


        OptionButton1.Caption = "   Chico"


        OptionButton2.Caption = "   Gummo"


        OptionButton3.Caption = "   Lando"


        OptionButton4.Caption = "   Zeppo"


           


        OptionButton1.Value = False


        OptionButton2.Value = False


        OptionButton3.Value = False


        OptionButton4.Value = False


   


        OptionButton1.Visible = True


        OptionButton2.Visible = True


        OptionButton3.Visible = True


        OptionButton4.Visible = True


           


        CommandButton1.Caption = "Continue Quiz"


      Else


           


        If OptionButton3.Value = True Then


            MsgBox "That's correct. Lando is a character from Star Wars."


        Else


            MsgBox "Sorry, that is not correct. The correct answer is C." & _


               " Lando is a character from Star Wars."


        End If


           


        SlideShowWindows(1).View.Exit


    End If


End Sub


 


One thing we wanted to do with this subroutine was keep all the code within the subroutine itself; we didn’t want to have to use global variables or anything else like that outside CommandButton1_Click. But that immediately introduced a problem: the user needs to click the command button twice, once to start the quiz and once to submit their answer. If we don’t use global variables, how can we tell which click is which?


Our solution – which happened to be the same solution most everyone else came up with – was to rely on the command button caption. The first time you click the command button the caption reads Start the Quiz. Therefore, the first thing we do is check the value of the caption; if it’s equal to Start the Quiz then this must be the first time the button has been clicked:


If CommandButton1.Caption = "Start the Quiz" Then


Of course, that also means that we’ll have to change the caption each time the button gets clicked. But don’t worry; for perhaps the first time in our lives, we’re way ahead of you.


Our next step is to replace the Welcome message with the first (and only) question in our quiz; that’s what this line of code does:


ActivePresentation.Slides(1).Shapes(1).TextFrame.TextRange.Text = _


    "Which of the following is not one of the famous Marx Brothers?"


 


That brings us to this chunk of code:


OptionButton1.Caption = "   Chico"


OptionButton2.Caption = "   Gummo"


OptionButton3.Caption = "   Lando"


OptionButton4.Caption = "   Zeppo"


           


OptionButton1.Value = False


OptionButton2.Value = False


OptionButton3.Value = False


OptionButton4.Value = False


   


OptionButton1.Visible = True


OptionButton2.Visible = True


OptionButton3.Visible = True


OptionButton4.Visible = True


 


All we’re doing here is configuring the radio buttons that serve as the choices in our multiple-choice quiz. In the first chunk of code we set the Caption for each button; in the second chunk, we set the Value of each button to False. (Setting the Value of a radio button to False means that the button will not be selected. If you set the Value of a radio button to True then the button will be selected.) In our last block of code, we set the Visible property of each button to True. That ensures that we’ll be able to see each choice onscreen; as a general rule, it’s better when all the choices in a multiple-choice quiz can actually be seen onscreen.


 And then – finally – we change the Caption of the command button:


CommandButton1.Caption = "Continue Quiz"


All that takes place the first time the button gets clicked. But what happens the second time the button gets clicked? And how do we know that it’s the second time that the button has been clicked?


We’ll answer the second question first, because it’s the easy one: if the command button Caption is not equal to Start the Quiz then this must be the second time through. In that case, we check to see if radio button 3 has been selected:


If OptionButton3.Value = True Then


As it turns out, option 3 is the correct choice; therefore, we display a message to that effect:


MsgBox "That's correct. Lando is a character from Star Wars."


And if radio button 3 hasn’t been selected? Well, then the answer is wrong, and we display a message to that effect:


MsgBox "Sorry, that is not correct. The correct answer is C." & _


    " Lando is a character from Star Wars."


 


At that point, the quiz is over . (Don’t you wish the SAT test would have gone that quickly?)  With that in mind, we call the Exit method and exit the slide show:


SlideShowWindows(1).View.Exit


Now see, that wasn’t so bad. Was it?

Skip to main content