A Fun Game/Trick in Microsoft Visual Basic

by saintjimmy in Circuits > Microsoft

52428 Views, 5 Favorites, 0 Comments

A Fun Game/Trick in Microsoft Visual Basic

8.bmp
Hello everyone! In this Instructable, I will show you how to amaze your friends with a game based on the Internet site Peter Answers, a trick in which the user tells a friend to ask a question to the great "Peter", first petitions Peter with a greeting and then types the friend's question in the box. "Peter" will, amazingly, answer the question correctly every time... as long as the user knows the answer, that is. The trick is that the user secretly enters the answer to the question into the "petition" box by typing a period, then the answer, and then another period.  The program automatically continues with the pre-written petition so the friend can't see what is happening. This Instructable will show you how to make a program similar to this, but not entirely the same, to trick your own friends! 

To begin, all you need is Microsoft Visual Basic, a program that can be downloaded from Microsoft's web site. The Express version, which is what I have, is free, and offers most of the same features as the full version. Although it might help you to understand what's going on better if you know how to write Visual Basic code, you don't need to for this Instructable, as I will tell you exactly what to write and explain it. 

Starting a New Project and Setting Up the Form

1.bmp
2.bmp
First, start a new project in Visual Basic by clicking File > New. Give it a snappy name in the box down at the bottom (something better than mine) and then click "OK".

The first thing you want to do when you see a blank box appear that says "Form 1" at the top is click on the box. Then look at the box in the bottom right of the screen that says "Properties" above it, and "Form 1" in the drop-down menu. Scroll down on that, and in the box next to the words "Text" type the name of your game.

A little above that box is one that says "Size". Change this to 500, 300.

Change the "MaximizeBox" property to "False" and the "FormBorderStyle" property to either "FixedSingle" or "Fixed3D". Notice that when you change a property to one that it's not originally set to, the text becomes bold. That should set up your form nicely, but the last step is incredibly important, since if people can resize the form, then they might discover the trick. 

Adding Labels

3.bmp
In Visual Basic, a control is basically anything that can be seen by the user and affect the outcome of the program. You'll find a list of controls under the Common Controls menu in the Toolbox button on the left side of the screen. Double-click on a control to add it. For starters, add a LABEL to the form, and drag it to the top of the form in about the center. Change this label's Text property to whatever you named your program. You can also change its Font property. 

A bit below that, add another label, with its Font Size property set to around 10 and its Text property set to something like "First you must greet the amazing answerer".

Copy this label and paste it anywhere on the form. Move it so that it is on the left side of the box, about half way between the top and bottom of the form. Change its Text property to "Now ask your question". 




Adding TextBoxes

4.bmp
Now go to the Toolbox and add a TextBox control. Change its (Name) property to "hidden", but position it right below the "petition" label. 

Then add another TextBox and change its Font Size property to 14, its Name to "question", and put it right below the "ask a question" label. Set its Enabled property to False, as this will allow it to be seen, but not typed in. Users can't ask a question until they type a petition. Then stretch it so that it reaches from one side of the form to the other, like in the picture. 

Copy this TextBox by using the Ctrl + C keys and paste it by using Ctrl + V. Move it to the very bottom of the form and change its Name to "answer". Set its ReadOnly property to True, since users can only read what is in it, they can't write anything. Essentially, this will make it a label that people can copy and paste from. 


The Trickery

5.bmp
Press Ctrl + V again, which will paste in another textbox. Name this one "petition". Set its text property to "Type your petition, then press Tab." Make sure you also change its Enabled property to "True". 

Now, each textbox you have has a property called TabIndex, which sets in what order you can switch between them by pressing the Tab key. Set this property for each textbox like this:

hidden: 1
question: 2
petition: 3

There is another property called TabStop. For the textbox called "answer", set this to False. 

Now for the trick: move the "petition" textbox so that it covers the "hidden" textbox! Although you can't see it, it will automatically be the one that text is entered into when the program starts. However, text can still be entered into the "petition" box in case your friend decides to give it a try. 

Now that this is done, we can move on to add buttons. 



Adding Buttons

6.bmp
In the Toolbox under Common Controls, you'll find buttons. Add one of them to the form, set its Name to "start", its AutoSize property to True, its Font Size to 14, and its Text to something like "FIND THE ANSWER!". Position it right below the "question" textbox. Make its Enabled property False.

Then, copy it and paste another one next to it, changing its Name to "reset" and its Text to "Reset". Set its Enabled property to False. 

That's it for these buttons. 

Now, before you get on to writing the code, add one more label, and change its name to "sneaky". Put it wherever you want because it's going to be invisible. Set its Visible property to False. Although you can still see it now, you won't be able to when you run the program. Set its Text property to "I can't answer that question." 

Now press F5 to run the program. Everything should be there except for that label you just added. Nothing will happen yet, because you haven't done any of the coding. But that comes next!

Coding, Part I

7.bmp
If you just want the full code to copy and paste, go to the last step. Otherwise, this will explain what to do. 

On the Properties bar is a drop-down menu that shows what object you are editing. Drop this down and choose the textbox you called "hidden". You can't see this because it is hidden by the "petition" box. Underneath the drop-down menu should be a little button with a lightning bolt on it. Click this to bring up the Events for that object. Events are what happens when the user does something, like clicks a button or presses a key. And Event Handler is a section of code that tells what to do when an Event happens. Scroll down the Events list for "hidden" and find TextChanged. Double click these words to call up the code section of this form, with a blank event handler for you to write in. When you do, your screen should look like the picture. 

Coding, Part II

Don't worry about what just appeared for now. Instead, click above the words "Private Sub" and above that line, type:

Dim txtLen As Integer

"Dim" means that the next word will be a variable that can be changed, and it is of the type Integer, which is a number that cannot be a decimal. 

Now, go below the "Private Sub" line to write your Event Handler. Type or copy and paste these lines. Make sure they are after the "Private Sub" line and before "End Sub"

txtLen = Len(hidden.Text)
petition.Text = Microsoft.VisualBasic.Left("Please answer the following question:", txtLen)

Every time you type something into that hidden textbox now, the value of txtLen, which is a variable, will change to the number of characters (letters, spaces, etc.) in that box. If you type "Hello", then txtLen = 5. The next line changes the text in the petition box to "Please answer the following question." However, it only shows the number of characters that you have typed into the hidden box. If you typed "Hello", then the petition box will say "Pleas" because this is the first five characters of the sentence. 
Next, add this code:

If Microsoft.VisualBasic.Right(hidden.Text, 1) = "." Then
            sneaky.Text = Microsoft.VisualBasic.Left(hidden.Text, Len(hidden.Text) - 1)

With this code, when you type a period, the text in that hidden label "sneaky" will change to whatever is in the hidden textbox, without the period. So if you want a period at the end of the sentence, you'll have to type two periods. Next, add this code on a new line:

If petition.Text = "Please answer the following question:" Then
            question.Enabled = True
        End If

Now, if the petition is fully typed out, then the question box will allow you to type in it. 







Coding, Part III

The rest of the code you can just copy from here. It just has to do with when the buttons become enabled and how the answer appears, but it's pretty self explanatory. 

Private Sub question_TextChanged(sender As System.Object, e As System.EventArgs) Handles question.TextChanged
        If Microsoft.VisualBasic.Right(question.Text, 1) = "?" Then
            start.Enabled = True
        End If
    End Sub

    Private Sub start_Click(sender As System.Object, e As System.EventArgs) Handles start.Click
        Dim test As Boolean
        If InStr(hidden.Text, ".") Then
            test = True
        End If
        If test = False Then
            sneaky.Text = "I can't answer that question."
        End If
        start.Enabled = False
        reset.Enabled = True
        answer.Text = sneaky.Text
        question.ReadOnly = True
        petition.ReadOnly = True
    End Sub

    Private Sub reset_Click(sender As System.Object, e As System.EventArgs) Handles reset.Click
        reset.Enabled = False
        sneaky.Text = "I can't answer that question."
        petition.Text = "Type your petition, then press Tab."
        hidden.Text = Nothing
        question.Text = Nothing
        question.Enabled = False
        petition.ReadOnly = False
        question.ReadOnly = False
        answer.Text = Nothing
    End Sub

How to Play

8.bmp
Tell your friend that this program can answer any question about your or his/her life within reason, and ask him/her to ask it a question. Type the answer to that question right away, then type a period ("."). Continue to type out the rest of the petition phrase "Please answer the following question:". Make sure you put the colon at the end of it. Once that is right, the question box will become enabled, press the Tab key or click inside it to begin typing the question. THE QUESTION MUST END IN A QUESTION MARK! Once it does, the "Find the answer" button will work, just click that or press Enter and the bottom box will show the answer you typed in. If you never typed a period, it will just say "I can't answer that question". Amazing! 

The Full Code

Here is the completed code for the project:

Public Class Form1

    Dim txtLen As Integer
    Private Sub hidden_TextChanged(sender As System.Object, e As System.EventArgs) Handles hidden.TextChanged
        txtLen = Len(hidden.Text)
        petition.Text = Microsoft.VisualBasic.Left("Please answer the following question:", txtLen)
        If Microsoft.VisualBasic.Right(hidden.Text, 1) = "." Then
            sneaky.Text = Microsoft.VisualBasic.Left(hidden.Text, Len(hidden.Text) - 1)
        End If
        If petition.Text = "Please answer the following question:" Then
            question.Enabled = True
        End If

    End Sub

    Private Sub question_TextChanged(sender As System.Object, e As System.EventArgs) Handles question.TextChanged
        If Microsoft.VisualBasic.Right(question.Text, 1) = "?" Then
            start.Enabled = True
        End If
    End Sub

    Private Sub start_Click(sender As System.Object, e As System.EventArgs) Handles start.Click
        Dim test As Boolean
        If InStr(hidden.Text, ".") Then
            test = True
        End If
        If test = False Then
            sneaky.Text = "I can't answer that question."
        End If
        start.Enabled = False
        reset.Enabled = True
        answer.Text = sneaky.Text
        question.ReadOnly = True
        petition.ReadOnly = True
    End Sub

    Private Sub reset_Click(sender As System.Object, e As System.EventArgs) Handles reset.Click
        reset.Enabled = False
        sneaky.Text = "I can't answer that question."
        petition.Text = "Type your petition, then press Tab."
        hidden.Text = Nothing
        question.Text = Nothing
        question.Enabled = False
        petition.ReadOnly = False
        question.ReadOnly = False
        answer.Text = Nothing
    End Sub
End Class