|
Name: Galaxy Viewer
Author: Burt Abreu Updated: August 20, 1998
Description: Create a 'viewer' that will show a picture based on a list box selection. Topics covered include adding items to a list box; loading pictures into an image box; using IF..THEN..ELSE.
Before you start create a sub-directory in your VB directory and name it explore; you can store all your tutorials here. Next create a sub-directory in the explore directory called galaxy for this project; unzip the galaxy.zip file to this directory, it contains the pictures you'll use for this project. 1. Open a new project and add a list box, 2 image boxes and 2 command buttons to the form (when your done it should look roughly like the image of the completed project above).
2. Now go to the properties box and set the following by typing in these new values; you can select the picture for imgLogo by clicking on the button in the Picture property The picture property for Form1 will place a picture directly on the form background rather than in the title bar, convenient if you want to add a custom background, though it does not support the stretch property so you may prefer stretching an imagebox on the form first. When you learn more you can write or find some code on the net to tile an image on the background similar to what is done on a webpage.
3. Save the project and form as "Galaxy".
4. Ok, now let's warm up with a little coding. Double click on the 'Quit' command button and add the following statement;
The Me keyword refers to the current form -you can find more info in online help. I changed this to use Unload Me instead of the End statement. In a simple program like this End gives you a quick out, and is probably ok, but in a more complicated program you would probably have to unload forms and perform other clean-up before ending the program or risk leaving stuff in memory which might cause unexpected reactions or degrade the performance of your application. Mick Lambino, a fellow newbie, suggested that I might want to explicitly free the resources by putting something like the following in the Form_Unload event;
Private Sub Form_Unload(Cancel As Integer) '--------------------------------- 'Free all resources explicitly '--------------------------------- Set imgShow.Picture = nothing Set imgLogo.Picture = nothing Set Form1.Icon = nothing Set Form1 = nothing End Sub Feel free to try this if you want, VB will actually clean up a lot of stuff on its own and this is a simple program -but there are times when you will have to do this so you may want to try it now. Every form in VB also has what is known as a Control Collection which acts something like an array of all the controls on the form; using this it should be possible to write code to loop through all items in the collection and unload them. You may want to check online help and the resources on my Great Links page for more information on this.
'-------------------------------------- 'So you can create comments like this! 'Visual Basic ignores all of this so I 'can write this... 'VB can't see me nah nah :-P ' 'Add information about sections of code 'whose purpose might not be easily 'understood by another programmer (or 'even you if looked at much later). '-------------------------------------- Most will also provide additional information in a header in each module or form if needed, something like this... '-------------------------------------- 'PROGRAM: Galaxy Viewer '-------------------------------------- 'Programmer: Burt Abreu 'Version: 1a 08/20/98 ' 'Description: Picture viewer demo that 'also shows some basic control usage. 'Some programmers will even include a 'version history, bug fix index or in 'the case of a sub or a function info 'on how to call it like this... '-------------------------------------- 'Update History: '-------------------------------------- '7/20/97: Version 1 ' '8/20/98: Version 1a modifies the xyz 'file and update abc. '-------------------------------------- 'Usage Example: '-------------------------------------- 'Call MySub(para1,para2) ' para1 = who knows ' para2 = who cares '-------------------------------------- I think that you'll agree that you have a much better chance of figuring out the purpose of a program or its variables and code if you add sufficient commenting. 5. Next double-click the About command button. We'll create a simple About box with your name, using the MsgBox function. Add this code;
6. Now save your project in the galaxy directory and run the program by pressing F5 or selecting Run and then Start from the menu; now try out your About and quit buttons. If you get an error message of any type re-check your code for syntax errors. 7. Ok, now let's add items to the list box; we'll use the AddItem method for this. Type the following code in the Form_Load event procedure;
If you run your program now you'll see that the list box 'lists' all the items you've added; of course it doesn't do anything yet, put its starting to look more like a program right? Code in the Form_Load gets executed as soon as the form loads.
8. Let's add some code to the lstPlanets_Click event procedure which will provide the action when our user selects something in the listbox;
Run the program and test all the features, then save your program. Congratulations! You have completed your first project. Review the code carefully and feel free to experiment. You learned some basic uses of the ListBox, AddItem, ListIndex, ImageBox, LoadPicture, MsgBox, Command Buttons, IF..THEN..ELSE and a few other things like creating and assigning a value to a string variable and commenting code. If you want to experiment, you can open the project and choose Save Project As... then save the project as Galaxy11 for example, in case you make a mistake while your learning.
In our next project we'll add some additional features to the Galaxy project. We'll add a descriptive text box, our first function, a module and we'll explore some ways of making the program more efficient and readable with Select Case structure.
To download the pictures for the galaxy tutorial click here.
|
Quick searches: Site Search | Advanced Site Search |
|
By using this site you agree to its terms and conditions VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc. |