Beginning Visual Basic Project Design, Forms, Command Buttons Review and Preview

|
Beginning Visual Basic Project Design, Forms, Command Buttons Review and Preview
|
bet | 1/3 | Sana | 25.12.2019 | Hajmi | 302 Kb. | | #4849 |
Beginning Visual Basic
4. Project Design, Forms, Command Buttons
Review and Preview
You have now learned the parts of a Visual Basic project and the three steps involved in building a project:
Place controls on the form.
Set control properties.
Write desired event procedures.
Do you have some ideas of projects you would like to build using Visual Basic? If so, great. Beginning with this class, you will start to develop your own programming skills. In each class to come, you will learn some new features of the Visual Basic environment, some new controls, and elements of the BASIC language. In this class, you will learn about project design, the form and command button controls, and build a complete project.
Project Design
You are about to start developing projects using Visual Basic. We will give you projects to build and maybe you will have ideas for your own projects. Either way, it’s fun and exciting to see ideas end up as computer programs. But before starting a project, it’s a good idea to spend a little time thinking about what you are trying to do. This idea of proper project design will save you lots of time and result in a far better project.
Proper project design is not really difficult. The main idea is to create a project that is easy to use, easy to understand, and free of errors. That makes sense, doesn’t it? Spend some time thinking about everything you want your project to do. What information does the program need? What information does the computer determine? Decide what controls you need to use to provide these sets of information. Design a nice user interface (interface concerns placement of controls on the form). Consider appearance and ease of use. Make the interface consistent with other Windows applications, if possible. Familiarity is good in Windows based projects, like those developed using Visual Basic.
Make the BASIC code in your event procedures readable and easy to understand. This will make the job of making later changes (and you will make changes) much easier. Follow accepted programming rules - you will learn these rules as you learn more about BASIC. Make sure there are no errors in your project. This may seem like an obvious statement, but many programs are not error-free. Windows 95 has several hundred errors floating around!
The importance of these few statements about project design might not make a lot of sense right now, but they will. The simple idea is to make a useful, clearly written, error-free project that is easy to use and easy to change. Planning carefully and planning ahead helps you achieve this goal. For each project built in this course, we will attempt to give you some insight into the project design process. We will always try to explain why we do what we do in building a project. And, we will always try to list all the considerations we make.
Saving a Visual Basic Project
In Class 1, you learned how to open, run, and close a previously-saved Visual Basic project, but we never talked about how a project is saved for future use. Now that you are starting to build your own projects, you need to see how to save them. It’s really quite easy. We will use the Visual Basic main window toolbar. Look for a button that looks like a small floppy disk. (With writeable CD ROM’s coming out, how much longer do you think people will know what a floppy disk looks like? - the new Apple iMac doesn’t even have a floppy disk drive!) This is the Save Project button:
Clicking this button will have different results, depending on when you click it.
If you are working on a new project that has never been saved, the following window will appear:
This window asks you where you want to save your form and what you want to name it (recall this is a file with a frm extension). Move to the desired folder, assign a name to your form file (something that is meaningful) and click Save.
After saving your form, another window will appear:
This window asks where you want to save your project file and what you want to name it (a vbp extension). Again, pick a folder (usually the same folder your form file is saved in) and project name (again, make it meaningful) and click Save. At this point, your project is saved in two files: the form file and the project file.
If you are working on a project that has been saved previously and you click on the Save Project button, Visual Basic automatically saves both the form file and project file, using the same names, without asking you any questions. It is suggested you occasionally save your project as your work on it. And, always save your project before running it or before leaving Visual Basic. When you want to open a saved project, just click on the Open Project button on the Windows toolbar and select the project file, then click Open. The project file will open and the associated form will appear.
On-Line Help
Many times, while working in the Visual Basic environment, you will have a question about something. You may wonder what a particular control does, what a particular property is for, what events a control has, or what a particular term in BASIC means. A great way to get help when you’re stuck is to ask someone who knows the answer. Others are usually happy to help you - they like the idea of helping you learn. You could also try to find the answer in a book and there are lots of Visual Basic books out there! Or, another great way to get help is to use the Visual Basic On-Line Help system.
Most Windows applications, including Visual Basic, have help files available for your use. To access the Visual Basic help system, click the Help item in the main menu, then Contents. At that point, you can search for the topic you need help on or scroll through all the topics. The Visual Basic help system is just like all other Windows help systems. If you’ve ever used any on-line help system, using the system in Visual Basic should be easy. If you’ve never used an on-line help system, ask someone for help. They’re pretty easy to use. Or, click on Start on your Windows task bar, then choose Help. You can use that on-line help system to learn about how to use an on-line help system!
A really great feature about the Visual Basic on-line help system is that it is ‘context sensitive.’ What does this mean? Well, let’s try it. Start Visual Basic and start a new project. Go to the properties window. Scroll down the window displaying the form properties and click on the word BackColor. The word is highlighted. Press the <F1> key. A screen of information about the BackColor property appears. The help system has intelligence. It knows that since you highlighted the word BackColor, then pressed <F1> (<F1> has always been the key to press when you need help), you are asking for help about BackColor. Anytime you press <F1> while working in Visual Basic, the program will look at where you are working and try to determine, based on context, what you are asking for help about. It looks at things like highlighted words in the properties window or position of the cursor in the code window. As you work with Visual Basic, you will find you will use ‘context-sensitive’ help a lot. Many times, you can get quick answers to questions you might have. Get used to relying on the Visual Basic on-line help system for assistance.
That’s enough new material about the Visual Basic environment. Now, let’s look, in detail, at two important controls: the form itself and the command button. Then we’ll start our study of the BASIC language and build a complete project.
The Form Control
We have seen that the form is the central control in the development of a Visual Basic project. Without a form, there can be no project! Let’s look at some important properties and events for the form control. The form appears when you begin a new project.
Caption
Icon
Properties
Like all controls, the form has many (over 40) properties. Fortunately, we only have to know about some of them. The properties we will be concerned with are:
Property Description
Name Name used to identify form. Three letter prefix for form names is frm.
Caption Text that appears in the title bar of form.
Icon Reference to icon that appears in title bar of form (we’ll look at creating icons in Class 7).
Left Distance from left side of computer screen to left side of form.
Top Distance from top side of computer screen to top side of form.
Width Width of the form in twips.
Height Height of form in twips.
BackColor Background color of form.
BorderStyle Form can either be sizable (can resize using the mouse) or fixed size.
Example
To gain familiarity with these properties, start Visual Basic and start a new project with just a form. Set the Top, Left, Height and Width property values and see their effect on form position and size. Resize and move the form and notice how those values are changed in the properties window. Set the Caption property. Pick a new background color using the selection techniques discussed in Class 3. To see the effect of the BorderStyle property, set a value (either 1-Fixed Single or 2-Sizable; these are the only values we’ll use in this course) and run the project. Yes, you can run a project with just a form as a control! Try resizing the form in each case. Note the difference. Stop this example project.
Events
The form primarily acts as a ‘container’ for other controls, but it does support events. That is, it can respond to some user interactions. We will only be concerned with two form events in this course:
Event Description
Click Event executed when user clicks on the form with the mouse.
Load Event executed when the form first loads into the computer’s memory. This is a good place to set initial values for various properties and other project values.
One word about form naming. Recall we saw in a past class that control names are used in event procedures. This is not true for forms. All form event procedures have the format:
Form_EventName
That is, no matter what Name property you assign to the form, event procedures are listed under the word Form. So, when looking for form event procedures in the code window, scroll down the Object List until you find Form. Try this with the example just used to play with properties. Note if we assign the name frmFirstCode to the form, the code window will be::
VB4:
VB5, VB6:
Note that the word Form appears in the object list, not frmFirstCode. We always need to be aware of this peculiarity when working with form event procedures. All other controls will appear in the object list by their assigned Name property.
|
| |