[VBS.NET] Tutorial Part 1 - Your First Program

Discussion in 'Mixed Languages' started by jlgager, Nov 30, 2010.

  1. jlgager

    jlgager MDL Developer

    Oct 30, 2009
    365
    3,230
    10
    #1 jlgager, Nov 30, 2010
    Last edited by a moderator: Apr 20, 2017
    [VBS.NET] Tutorial Part 1 - Your First Program​


    In this series I will show you everything from the basics to the most advanced. If you would like me to cover a specific topic, just PM me or post below. If it is a topic I wasn’t planning on covering I will put a star next to its name so check back a lot for updates.

    Your First Program – Launching the Software

    To get started, launch your Visual Basic .NET or Visual Studio software. (In all of the following tutorials I will be using Visual Studio 2010 Express because I like using updated software lol. Also it is free. ) When it opens you will see on the left side a New Project button. Click It.

    On the new window, you'll normally want the option selected: "Windows Forms Application", in the "Installed Templates/Visual Basic" folder. This option lets u create a program with a form window. If you choose the “Console Application” you will be designing a CMD program. We will only be covering the “Windows Forms Application” version right now so select that one if not already selected.
    Next you will need to name your program. You can name it whatever you want but for consistency we will be naming it: “Demo1”. Replace whatever name you choose with the “WindowsApplication1” text listed in the “Name:” textbox and click ok.

    Next you will be going into debug mode you can do this in 3 ways
    1.At the top center of the screen click the green diamond
    2.Press the F5 key on your keyboard
    3.Select from the menu bar “Debug” then from the drop down select “Start Debugging”

    Any of these options will bring up a window like this:

    [​IMG]

    Congratulations! You have now created your very first program.
    Click the Red X on the form or the blue square at the top of the page to stop it from running. You will then be returned to the software environment.
    If you compare the first form with the one above, you'll see that they look very similar. The one above is actually a real program, something you could package and sell to unsuspecting village idiots. So why are there two different views? Well, Visual Basic has two distinct environments, a Design environment and a Debug environment. Design Time is where you get to play about with the form, spruce it up, add textboxes, and buttons, and labels (and code, of course). Debug is where you can test your program and see how well it performs or doesn't perform, as is usually the case.

    Your First Program – Adding a control

    Now you need to add a control to your boring program to actually make it do something lol.

    Things like buttons, textboxes, combo boxes, list boxes and labels are all things that you can add to your Forms.

    The Toolbox can be found on the left of the screen. You can hover over it every time you need it but I find it easier to check the pushpin on the top of the window that pops out so it stays there. (If you want to later hide it just click on the pushpin again. I turn it off when coding to give me more room.)
    First find the object called “Button” in the menu and double click it. A button will then show on the form and it will be selected if not click on it once to reselect it. Now on the right side should be a panel called properties. In this section you can change almost everything about that button except what it does when you click on it which we will get to later.

    Next click the properties window and scroll up to “Name” and type in “btnDemo” and press enter.

    Now the name of the button has changed but not the text. Scroll down to “Text” and type “Demo!” and press enter. Now if you look over at your form the text on the button has now changed. Yah…

    Press F5 or Select Debug and a window like this should appear:

    [​IMG]

    Ok moving on… so now that you have a button let’s make it do something now when you click on it.

    In design mode double click on the button and a new tab will come up.
    In the new tab you will have some new code:

    Code:
    Public Class Form1
        Private Sub btnDemo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDemo.Click
    (CODE GOES HERE)
        End Sub
    End Class
    Now in the spot I added is where you will be adding your code. The most basic thing we can have a button do is display a message box. To do this, in between the area I described above type the following:
    MsgBox("Hello World")

    From there, go into debug and click the button now a message box will appear saying “Hello World”.
    Now to examine the code that we have so far:

    Code:
    Private Sub btnDemo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles btnDemo.Click
            MsgBox("Hello World")
        End Sub
    The part of the code we're interested in is highlighted in red in the code above. Notice, too, that the underscore character ( _ ) has been used to spread the code over more than one line. You can do this in your own code, too, if it becomes too long:

    Private
    Private means that no other part of the program can see this code except for our button. (You can replace this with “Public” if you would like. Public allows you to share variables between subs later which makes reusing variables easier.)

    Sub
    Short for Subroutine. The "Sub" word tells VB that some code follows, and that it needs to be executed.

    btnDemo
    This is the name of our button. The Name property of the control is the important one. If you change the Name property, which we have, VB will change this button name for you.

    _Click ( )
    This is something called an Event. In other words, when the button is clicked, the Click Event will fire, and the code we're going to write will be executed. (by the way the text in the parentheses is the same for all buttons so you could hard code every button but just make sure the handle is right as I explain below and the button should run the code.)

    Handles btnDemo.Click
    This allows the same subroutine to be used for all kinds of event calls. Suppose, for example, that you wanted a click in a Textbox and a Button to be processed by exactly the same code. Without this the button won’t work. btnDemo_Click is just naming the Sub that groups the code. You can add any button to the list in the form of “ButtonName.Click”. Each button you add to the list will display the msgbox or whatever code you have in the sub.

    Msgbox(“Hello World”)
    This is what makes your msgbox work. If you are entering a text string it has to be in double quotes but if you want to put a number in quotes that's ok but they are not needed.

    End Sub
    The subroutine ends right here. This signifies the end of our code.

    Congratulations! You have now created your very first program and now it displays a message box.

    Now we should save it:
    Select from the file menu, Save All or Press Ctrl + Shift + S
    The window that appears will allow you to decide where to save it to. The defaults are usually fine. If they are ok then click "Save" and it is saved.
    Next time you want to open the program, when it opens you will see on the left side a Open Project button. When u click select the program folder and then the only file in that folder named "Demo1.sln".

    In the next lesson, I will introduce you to variables.

    [VBS.NET] Tutorial Part 2 - Variables and Labels

    [​IMG]
    Want to thank me?
    Then just hit the thanks button on this post!
    Also post any ideas for future topics and any typos please.
     
  2. jlgager

    jlgager MDL Developer

    Oct 30, 2009
    365
    3,230
    10
    #2 jlgager, Nov 30, 2010
    Last edited: Nov 30, 2010
    (OP)
  3. jlgager

    jlgager MDL Developer

    Oct 30, 2009
    365
    3,230
    10
    Thanks BobSheep I was going to add that link but forgot lol :)
     
  4. timesurfer

    timesurfer MDL Developer

    Nov 22, 2009
    8,527
    4,112
    270
    Wow really great job jlgager :eek:
     
  5. seyamco

    seyamco MDL Novice

    May 7, 2008
    25
    0
    0
    thanks jlgager