Tags:
tag this topic
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, <nop>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 <b>Label</b> in the toolkit, then go over to the <b>Form1</b> window, drag open a rectangle, and voila! You will get a box that says something like <b>Label1</b>. 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 <b>Properties</b> 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 <b>Text</b>. The right-hand side should say <b>Label1</b>; change that to say <b>Aardvarks rule!</b> Above the <b>Text</b> 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 <nop>MapQuest didn't convert miles to kilometers, let's make a quick tool to do that. * Add a <b><nop>Label</b> that says "Miles:". * Add a <b><nop>TextBox</b> * Change the <b>(Name)</b> property of the <nop>TextBox to <b>MilesTextEntry</b>. * Change the <b>Text</b> property to nothing (i.e. delete the characters "<nop>TextBox1"). * Add a <b>Label</b> that says "Kilometers:". * Change the <b>(Name)</b> property of the Label to <b>KilometersTextOutput</b>. * Change the <b>Text</b> property to "Kilometers:". * Add a <b>Button</b> that says "Convert!" * Change the <b>(Name)</b> property of the Button to <b>ConvertButton</b>. * Change the <b>Text</b> 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.) <table border=1 bgcolor="#ffccff" cellspacing="5" width="80%"> <tr> <th>Generated Code</th> </tr> <tr> <p> <td> Above Form1_Load, there will be something in grey that is collapsed that says <b>Windows Form Designer generated code</b>. 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 <b>Lemurs rule!</b> 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?) </p><p> 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. </p> </td> </tr> </table> 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: <verbatim> 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 </verbatim> Basically, the <nop>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 <nop>ConvertButton. All the junk about <nop>ByVal sender etc and e As <nop>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 <nop>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 -> <nop>WindowsApplication1 -> obj -> Release -> <nop>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!" -- Main.DuckySherwood - 25 Oct 2005 -- Main.AlexTung - 25 Oct 2005 (corrected code "EventArgs" to "System.EventArgs")
E
dit
|
A
ttach
|
Watch
|
P
rint version
|
H
istory
: r3
<
r2
<
r1
|
B
acklinks
|
V
iew topic
|
Ra
w
edit
|
M
ore topic actions
Topic revision: r3 - 2006-09-04
-
KarenParker
Home
Site map
BETA web
Communications web
Faculty web
Imager web
LCI web
Main web
SPL web
Sandbox web
TWiki web
TestCases web
Main Web
Users
Groups
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
P
P
P
View
Raw View
Print version
Find backlinks
History
More topic actions
Edit
Raw edit
Attach file or image
Edit topic preference settings
Set new parent
More topic actions
Account
Log In
Register User
E
dit
A
ttach
Copyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback