Chris's Event Programming with Visual Basic course.
Contents
| Introduction | Tool Bars & Control Arrays |
From the File menu on the Main Menu bar, choose New Project. This will
bring up a new form as shown above. We will now select some components
to be placed on our form.
The controls shown in the Toolbox are dependant upon what is loaded
for a project, customised controls may appear here if they are available.
Shown here
is typically
the Microsoft provided set of components. The most commonly used components
are the selection pointer, labels, Text boxes, Command buttons, image and
picture controls and combo boxes. Other controls that we will be interested
in during this course will be the data component and the timer component.
Each component can be placed on the form by selecting it and then clicking
and dragging to size on the form. The properties for each component can
be viewed by selecting the component when it is present on the form. Many
components have similar properties but they differ in the range of properties
that they have.
Select the text box (highlighted opposite) control. Place a text box on your form. Examine the text box properties for Name and Text, these will have been given default values which you can change by selecting on the value, you will see that the value appears on the line below the name and component type of your component. Change the name on this line to MyText, when you click on the tick box (or lose focus) the name of your component changes. This name property is the name by which you reference that component from within the form. Similarly enter some text to replace the default Text. If you now save the project that you have made by selecting SaveProjectAs from the file menu, you will be presented with a dialogue asking you to save the form, click on the 'Yes' button then give your form the name 'aaForm1.frm' ,(where aa is your initials), after clicking on the OK button you will be prompted for a Project name. Call your project VB3aa1.mak, (where aa is your initials) and click the OK button. You can now run your application. Note the designation for a project file, in VB3 this is .MAK in VB5/6 this is .PRJ. If you open this file in notepad you will see that it is a list of all the component parts of your project!
We have already made an application above. To run it, open the project
from the file menu, (if it is not already open) and chose the run command
from the tool bar
This application doesn't do much! There are no events associated
with the control on our form. The events that normally change the status
of a control are caused by the effect of physical actions such as the clicking
of a mouse button or by externalities such as data inputs or by timer
tasks. We can examine the events associated with a control by clicking
on the 'View Code' button in the Project window, this opens a code window
where the object can be selected from a combo box. If you select MyText,
an appropriate code fragment is displayed for a sub procedure. Selecting
on the Proc: combo box displays all the associated procedures for the events
that are recognised by that object, (in this instance the object is a Text
Box control).
We will now apply this knowledge to your form. The objective of this
version of your application will be to display the MyText text box when
a button is clicked on the form. Click on the form. Select a Command button
from the tool box and apply it to the form. Change the Command button name
to 'ShowText' and it's caption to 'Display'. Change the visible property
on MyText to false, this will result in the control not being visible in
it's default state. To make the MyText control visible, you will amend
the visible property at run time in response to an event.
The event to use will be the click event associated with ShowText. Insert
code as shown below:-
Click on the Form, Select run from the tool bar; The display button
is the only visible control on the form,
if you click on the button,
the text box appears. Further clicking of the button will not change
the state of the text box as the only event we have coded only sets the
text box visible property to true, which value it now already holds. We
could toggle the text box visible property in a number of ways but essentially
we would need to make it respond to another event either from another control
or by another event on the command button or by modifying the command button
click event in some way. Our next objective will be to toggle the display
property using a global variable named Flag.
Going back to the code window, select general and declare the variable
Flag as below:-
now amend the code for the click event of ShowText to set the value
of the MyText.Visible property depending on the value of Flag. Note that
the default (un-initialised) value of Flag is 0.
Try running this now. One further modification would be to change the
ShowText.caption property depending upon the state of Mytext, further modify
the code as shown below:
When run the form now displays additional relevant information for
a user of the application.
Save the Project as VB3aa2.mak, (where aa is your initials).
In addition to the properties (which are attributes of an object) methods can be used which manipulate what an object does, these equate to built - in functions and tend to be specific to particular classes of object. The VB3 on-line help gives the syntax and examples for most of these.
Visual Basic and all other Microsoft products mentioned
in this series are trademarks of the Microsoft Corporation.