Tags:
create new tag
view all tags

Introduction to Visual Basic .NET

Launching VB

Here is how you get started with Visual Basic .NET 7.1 as it lives on the x360 computers.

Select Start->Programs->Microsoft Visual Studio 2005->Microsoft Visual Studio 2005.

When that loads, click on the New Project button. Select the Visual Basic folder and the Windows Application icon.

The Visual Studio will open up, which has buttons and lists all over the place. In the center will be a grey sub-window with black dots in it labeled "Form1". The tab "Form1.vb [Design]" above it will be selected (i.e. darker).

On the left is something that says "Toolbox". If you click on that, you'll see a list of GUI elements (what in this context are called controls.) You'll probably see Pointer, Label, Button, CheckBox, etc.

Click on the little push-pin on the Toolkit window to keep it "always visible."

Pretty Pictures

The basic idea behind Visual Studio is that you do a lot of the GUI building graphically. For example, you can click on Label in the toolkit, then go over to the Form1 window, drag open a rectangle, and voila! You will get a box that says something like Label1.

Label1. Does that text suck, or what? Let's make it say something more interesting.

Go look in the bottom right-hand pane of Visual Studio. It should say Properties at the top of that little windowpane. (If it doesn't, select View->Properties Window and the Properties pane should appear somewhere.)

The Properties pane is mostly filled with a two-column table. Look for the left-hand cell that says Text. The right-hand side should say Label1; change that to say Aardvarks rule!

Above the Text line are a bunch of lines having to do with the appearance of the text: font (which includes size and boldness and italicness), color, background color, alignment, font direction, etc. You can make it hot pink text on a cyan background. Go wild! Take a few minutes to have some fun with the text -- I can wait.

Code

Okay, so you've made pretty fonts in all kinds of whizzy colors. That's nice, but surely there's more to VB than this.

Since MapQuest didn't convert miles to kilometers, let's make a quick tool to do that.

  • Add a Label that says "Miles:".
  • Add a TextBox
    • Change the (Name) property of the TextBox to MilesTextEntry.
    • Change the Text property to nothing (i.e. delete the characters "TextBox1").
  • Add a Label that says "Kilometers:".
    • Change the (Name) property of the Label to KilometersTextOutput.
    • Change the Text property to "Kilometers:".
  • Add a Button that says "Convert!"
    • Change the (Name) property of the Button to ConvertButton.
    • Change the Text property to "Convert!".

Double click on Form1, and boom! suddenly you'll get a code window, with a cursor sitting inside a subroutine Form1_Load. That's the code that gets run when the form starts up. (You don't care about that right now.)

Generated Code
Above Form1_Load, there will be something in grey that is collapsed that says Windows Form Designer generated code. That section is all the code to generate the thing you just drew. Open that section now (by clicking on the "+"), and you should be able to find Lemurs rule! buried in there somewhere, surrounded by a bunch of crud that sets the size and color and all kinds of teios stuff. (Aren't you glad you didn't have to enter that by hand?)

NOTE! DO NOT MODIFY THE GENERATED CODE. You'll regret it, because when you do graphics editing, then Visual Studio will overwrite what you just modified. DO NOT MODIFY THE GENERATED CODE.

We don't care about doing something when Form1 is loaded, but you do want something to happen when you click on the Convert! button. So paste this in to the code:


    Private Sub ConvertButton_Click(ByVal sender As System.Object, 
                                    ByVal e As System.EventArgs) Handles ConvertButton.Click
        Dim miles As Double
        Dim km As Double
        miles = Val(MilesTextEntry.Text)
        km = miles / 0.6
        KilometersTextOutput.Text = "Kilometers: " + Str$(km)

    End Sub

Basically, the Foo_Click method is called when there is a click in the element named "Foo". In this case, it executes every time there is a click in ConvertButton.

All the junk about ByVal sender etc and e As EventArgs, etc? You don't need to worry about right now. Basically, it's passing around events in a way that isn't that different from how Java does it. You might need to use it once you start building prototypes for your project, but you might not. Learn it when you need it.

After the Private Sub line is some pretty basic miles-to-kilometers conversion math. Pretty simple.

Finally, you set KilometersTextOutput.Text to a new string, and the new string magically gets plopped in the right place in the windo and refreshed. It's magic, it does it all for you.

Running the code

To see that this does in fact work,
  • click on the little right-hand arrow (below and to the right of the Help menu item)
  • OR Debug -> Start
  • OR press F5

Error while trying to run project

If you get a message "Error while trying to run project: Unable to start debugging", well, that's a security thing. Not all computers are "unlocked". (The ones in the x360 lab should NOT give you this message!)

If you do get "unable to start debugging" error message, all is not lost: go find your application on the file system. It's probably somewhere like My Documents -> Visual Studio Projects -> WindowsApplication1 -> obj -> Release -> WindowsApplication1. Click on file, and you should see a wonderful little window that says "Lemurs rule!" and has a miles to kilometers conversion tool.

Final notes

I (Ducky) wrote this tutorial very quickly, so there are probably things that you could improve upon. This is a wiki, so if you want to add screenshots, reword sentences, add clarification, etc., go for it! As a bonus, every time you add something of value, you have my permission to change the wording from "Lemurs rule!" to "(your team name)s rule!"

-- DuckySherwood - 25 Oct 2005 -- AlexTung - 25 Oct 2005 (corrected code "EventArgs" to "System.EventArgs")

Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View |  Raw edit | More topic actions
Topic revision: r3 - 2006-09-04 - KarenParker
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback