VB Tutorial 2
VB Tutorial 2
Part 2
Contents:
• Download and run example program 3
• Build example program 3 from scratch
o The Database Design
o The Software Design
• Download and run example program 4
• Build example program 4 from scratch
• Download and run example program 5
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:
• You can add CDs to the Database by entering some info in the textboxes and
pushing the "Add This Info" button
• You can select a row from the grid and hit the "Remove Selected" button to
remove it permanently from the database
The Cheap CD Collector program window
This is just a very simple, brain-dead database and app, so it's not meant to be
sophisticated or correct in terms of database design in any way!! The first thing you
need to do is just make yourself up a database table in Access and save it to an mdb
file. Sooooooo simple:
Okay, now you've got the database file, and remember where you saved that mdb
file at the start of the process. That's where you'll save your vb project too, just to
keep things simple. You'll probably eventually want your program and data living in
the same folder for simplicity's sake, so it makes sense to just start out like that.
Okay, let's build this app:
1. Start up VB
2. Pick Standard EXE from the new project list
3. Add the MSFlexGrid control to your project:
1. Go to the Project menu, pick Components.
2. Scroll down the list of components until you find "Microsoft FlexGrid
Control 6.0 (sp3)". Select that checkbox and hit the OK button to add
the control to your project.
4. Add a FlexGrid to your form by picking the tool and drawing it on your
main form.
5. Add a data source to the form using the tool and drawing on the form.
Change its visibility property to False.
6. Add two frames to the form using the tool and drawing them on the form.
7. Change the caption of one to Add new entry and the caption of the other to
Remove Entry
8. Draw the following controls in the Add new entry frame (yes, actually in the
frame):
1. A text box with the (name) txtArtistName
2. A label above that text box with the caption Artist Name
3. A text box with the (name) txtAlbumTitle
4. A label above that text box with the caption Album Title
5. A text box with the (name) txtTrackCount
6. A label above that text box with the caption Number of Tracks
7. A command button with the (name) cmdAddEntry and the caption
Add this info
9. Now, to the Remove Entry frame, add the following controls:
1. A command button with the (name) cmdRemoveEntry and the
caption Remove Selected
2. A label with the caption Select the entry you want to remove and
click the button:
10. Now, the most complicated part is formatting the FlexGrid (which is called
MSFlexGrid1) to do what you want. It's fairly customizable, but here's all I did
for this example program:
1. the AllowUserResizing property was set to 1
2. the Cols property was set to 3
3. the DataSource property was set to Data1 (** this is required **)
this hooks the data source up to the FlexGrid.
4. the FixedCols property was set to 0 while the FixedRows property
was set to 1. (this is recommended)
5. the FocusRect property was set to 0.
6. the HighLight property was set to 1.
7. the ScrollTrack property was set to True.
8. the SelectionMode property was set to 1 (selection by row only).
9. the WordWrap property was set to True.
11. Okay, next the code. In the form design window, double click the form, which
should bring up the code window with a blank Form_Load() subroutine. Here's
the code for it:
Private Sub Form_Load()
'the format string just lets you define a format for how
'your flexgrid will appear
MSFlexGrid1.FormatString = "Artist Name |" & _
"Album Name | Tracks"
'make sure the search path to the db is always in the right spot
Data1.DatabaseName = App.Path & "\CDCollection.mdb"
Example programs 4 and 5 were both written by Saul and have the basic goal of
teaching some more about event handling in VB as well as teaching a little bit about
how to go about using a canvas-style widget. Without further adue, let's check out
the first simple example:
With that going, you can push the play button (center of the top tool bar) and see
what it looks like. Theres only one thing you can do with this one!!:
• Click and hold the mouse on the canvas and roll it around to draw a cute blue
line
the sketchpad form in action (yes I'm rad)
back to the top!!
Okay, let's actually get to work building this thing. This one is actually
quite simple to build and shouldn't take too long at all!! Check it out:
1. Start up VB with a Standard EXE project (this should be familiar terminology
to you by now).
2. Add a picturebox to the main form using the tool and drawing on the
form. Just make it mostly the same size as the entire form. :)
3. Change the picturebox's (name) property to read picCanvas.
4. Now double-click on the form (not on the picturebox) to get access to a shell
for the Form_Load() subroutine. The code for that looks like:
Private Sub Form_Load()
'Redraws the image when the window is covered/uncovered
picCanvas.AutoRedraw = True
That should be it! This is pretty simple, all it does is listen for a button click with the
left mouse button on the picturebox. If it gets a mousedown followed by a drag, it
draws a blue line (as was set up in the form_load subroutine). Simple use of events
makes a fun little app!!
back to the top!!
With that going, you can push the play button (center of the top tool bar) and see
what it looks like. There's tons of stuff you can do with this one:
• You can draw a line on the canvas
• you can change the colour of the line you draw on the canvas
• you can change the thickness of the line you draw on the canvas
• you can clear your canvas and start from scratch (although witha picture like
this, why would you clear it??? :))