Programming: Select Case Tutorial In VB6

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
[CL]9mm-Man




Wordewatician 500

Posts: 773
Joined: Tue May 04, 2004 7:14 am
Location: Uranus

Programming: Select Case Tutorial In VB6

Post by [CL]9mm-Man »

Hi, welcome to my tutorial :D I learnt this stuff 2 days ago so I thought I'd make a tutorial out of it.

Basically, Select and case are like "If" and "End If's" however you dont need as many, it can cut the coding down alot which is fab.

So now heres an example of coding. Sorry for it not being Halo related, I'm not too sure how you would make it Halo related, anyways.

Open up Visual Basic 6, and create a new Standart Exe.

In this you won't need any objects on the form, its ALL coding. You can use other ways to do a few things, like hidding the form but I like to show it in coding, so it helps you.

Double click the Form, so it comes upto the Form_Load section. This is where the coding will be! :P

Firstly you need to Dim the type of data you will be using, this also makes the coding shorter keeping the file size down too.

Code: Select all

Dim Age As Single
This basically tells the program that when you use "Age" in your coding, you'r going to be using the output of the inputbox.
**Additional piece of code**

Code: Select all

Form1.Visible = False
Just means that when you run the program, it will hide the form
****

Next we will be creating an input box so you can input the data. When we Declared that Age was a single it also made the computer rememer what you input.

Code: Select all

Age = InputBox("How old are you?", "Age Test")
Select Case Age
How we have made the input box, its asking for your age, also we have started the Select case so it knows what we are going to be using Select case for (age)

Code: Select all

Case Is >=18
This basically means, If the number you input is 18 or above.
***For this please press the Tab key in visual basic, this will now indent the coding properly, so the coding is within the "Case" command, basically it means "Case" is like brackets, and the indent is just another line.

Code: Select all

    MsgBox "You are old enough to drive!"
This will display a message box saying your old enough, only if your age is 18 or over (as its in the case)

Code: Select all

Case 17
Now we are making it so if the number is "17" then all this will happen (whats in the case)

Code: Select all

    MsgBox "You can drive next year!"
<< please note that is indeted as well, using tab key in VB6.
Just displays a messege box

Code: Select all

Case Else
Means, if its any number thats Else from the last cases, the action it will take is in this case, for this example, it means if the number put in is NOT 18 or over, or 17, then it will run this code (means 0 - 16)

Code: Select all

    MsgBox "You are too young to drive"

Code: Select all

End Select
Tells the program to end the select case, IE closing the "Select Case" brackets.

Code: Select all

Unload Me
Just tells the program, once all the coding above has happened, it will close the program.

Here is the coding from my program

Code: Select all

Private Sub Form_Load()
Dim Age As Single
Form1.Visible = False
Age = InputBox("How old are you?", "Age Test")
Select Case Age
Case Is >= 18
    MsgBox "You are old enough to drive!"
Case 17
    MsgBox "You can drive next year!"
Case Else
    MsgBox "You are too young to drive"
End Select
End
End Sub
Private Sub Form_Unload(Cancel As Integer)
    Dim Msg, Style, Title, Response, MyString
    Msg = "Are you sure you want to terminate this program
Your image cannot exceed 400x200 or 50kb
User avatar
Phenomena





Posts: 1510
Joined: Thu Jul 01, 2004 3:25 pm
Location: Training my ducks
Contact:

Post by Phenomena »

nice tut! but you only need to be 16 to drive here >_>
Image
And if they don't accept Jesus as their Personal Savior, you can kill them later. How cool is that!
TfAv1228




Miner Logistician Droplet

Posts: 276
Joined: Sun Sep 05, 2004 4:40 pm

Post by TfAv1228 »

same here only 16(with restrictions like only 1 passenger unless family or going to do soemthing for school) when u turn 18 you get a full liscense
Sig breaks rules, read the rules before reposting.
Onetoomanysodas





Posts: 325
Joined: Mon Mar 22, 2004 3:59 pm
Location: Everywhere but nowhere
Contact:

Post by Onetoomanysodas »

Here it's 16 with 6 months of limitations such as driving with more than 1 passengers unless it's like you said getting family to school.
Onetoomanysodas





Posts: 325
Joined: Mon Mar 22, 2004 3:59 pm
Location: Everywhere but nowhere
Contact:

Post by Onetoomanysodas »

By the way sorry for double post but it's completely different from previous but Select Case also works with multiples

Ex:

Code: Select all

Case 16, 17, 18
     MsgBox "You get your license if you live somewhere cool"
Post Reply