CPSC
481 Assignment 3:
VB Intro Stuff part 1
Note: Lots more example programs (including these) on the 481 website at:
Contents:
Introduction to VB
Visual Basic is an event-driven language. So, when you talk about events, we're talking about things that happen in the program which cause little events to occur (similar idea to IRQ stuff you learned hopefully in 455). An example of this could be clicking a button causing a button_click event to be generated. You write code to react to these events and guide a user through your program.
You design your form completely visually, using a set of widget tools and drawing on the form directly just like you would in a paint program or something like this. This stuff is so easy, you'll love it ... or maybe not ... but it's quick and relatively pain free.
Okay, these examples were developed by James Tam, and I've just reproduced them here. His website has all his full details on what exactly these things entail, but I'll get into them a little bit myself. If it seems that there's something missing here, be sure to check out over there for more info.
Okay, the first thing you need to do with visual basic is basically just start it up. No problem. Go:
Start --- Programs --- Microsoft Visual Basic --- You get the picture .... :)
When you load it up for the first time, Microsoft Office might churn away for a couple of minutes. This is an issue with Office 2000 and you don't need to worry about it too too much. It will only happen the once. In any case, when VB is finally loaded up, and you pick Standard Exe from the new project dialog, here's what you'll be confronted with:
Click image for
a complete description...
This is your main programming environment. You do all your form design and coding from this window. Now, probably the best way to get right into this thing is to sit down and just hack out a quick program. Like I say, these are ones that James made, and they're perfect for our purposes so kudos to him. :)
Okay, great, now just exit VB without saving whatever you've done.
Open and Run the First Example Program
I figure the best way to get introduced to VB is to open a program and run it and play around with it, so that's going to be the order on this page. Open it, run it, then we'll design it from scratch. The first program is James' first example. Here's a link to download a zip file containing the example:
Click here to download |
Okay, the steps to follow:
This is what a VB Project File Icon looks like. FYI. :) |
With that going, you can push the play button (center of the top tool bar) and see what it looks like. There are a couple of things you can do with it:
here's the first
sample program window
Great, you've seen that VB works. Now what? Well, at this point there are a couple of things you can do. One is poking around with this program, looking at James' code, or you can close this up and design it from scratch (should take you like 5 - 10 minutes!!). I suggest building it. :)
Build the First Example from Scratch
Okay, this is going to be hella-easy. :) I'll insert little comments in this step by step process where you can run the program and try it out as you go along. Okay, on with the fun!!
'Note: comments have an apostrophe (') at the beginning of the line Private Sub cmdPressMe_Click() cmdPressMe.Caption = "You pressed me!" End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) lblCoordsB.Caption = "x = " & X & " y = " & Y End Sub
Private Sub chkBGColour_Click() ' If CheckBox checked then set background of form to red. If (chkBGColour.Value = Checked) Then Form1.BackColor = RGB(255, 0, 0) ' If CheckBox is unchecked then set background of form to grey. ElseIf (chkBGColour.Value = Unchecked) Then Form1.BackColor = RGB(210, 210, 210) End If End Sub
And that should pretty much do it for the program. If you run it now, it should do all the same stuff that the example you downloaded did.
Open and Run the Second Example Program
The second example program is a little more complicated, but not that much. It just shows you a bit about list boxes and throwing stuff back and forth. Cool, without further adue, check out the download link:
Click here to download |
Okay, the steps to follow:
The project file for the second example program. |
With that going, you can push the play button (center of the top tool bar) and see what it looks like. There are a couple of things you can do with it:
second example
main window
Hip fantastic, eh? :) Again, many thanks go out to James for making this example. Now, let's just build this thing from scratch to make sure you know what you're doing.
Build the Second Example Program from Scratch
Okay, this is going to be hella-easy. :) Okay, on with the fun!!
'if there's nothing in the listbox, disable the delete button... Private Sub Form_Load() If (lstTextList.ListCount = 0) Then cmdDelete.Enabled = False End If End Sub
Private Sub cmdAdd_Click() ' Add the string that is currently in the textBox to the List Box. lstTextList.AddItem txtAddText.Text ' delete button disabled? enable it. :) If (cmdDelete.Enabled = False) Then cmdDelete.Enabled = True End If End Sub
' This method contains the code for the click event for the delete button. Private Sub cmdDelete_Click() ' if the listbox isn't empty, remove an item ... disable delete button if necessary... If (Not (lstTextList.ListCount = 0)) Then lstTextList.RemoveItem (lstTextList.ListCount - 1) If (lstTextList.ListCount = 0) Then cmdDelete.Enabled = False End If End If End Sub
MSDN Visual Basic Documentation
The MSDN Library is available from a few different locations and contains a ton of information about Visual Basic and other Microsoft Visual Studio stuff. (I'm not pluggin' it, I'm just saying it's useful if you're using the products. :) ) Anyways, here's some ways to get at it:
Yeah. :) That's where you get it. If you load the program from VB or the start menu, this is what your browser looks like:
the MSDN library
window
Terrific, eh? :) Okay. You probably want to look up only stuff to do with Visual Basic, especially if you're using the search engine. You can automatically filter down what you're looking at by changing the active subset that MSDN's looking at to just look at Visual Basic. It's as easy as picking the Visual Basic subset from the dropdown list (see the picture, below)!! (damn that sounded cheesey, I should be in marketing...) In any case, now stuff that's specific to VB is black text in the index list and unrelated stuff is sorta greyed out.
choosing the
active subset
That's about all I have to say about the MSDN library. It's a very complete reference, but may take a little bit of getting used to. For a fun challenge, try finding the VB function MsgBox and throwing one up in an example programs ... just by clicking a button or something.
MSDN Sample Programs: The Location!!
The sample programs included in MSDN are also included online, but aren't attached to the Library Browser. Unfortunately they are not installed on each and every computer, but they exist on the server and there are a couple of ways you can go about getting access to them.
Mounting the Directory as a Drive
the map network
drive dialog
Make a Desktop Shortcut to the Samples
create a new
shortcut dialog