Sign In
Link to us
Home
1. Introduction
2. Introducing VB
3. Projects
4. Containers
5. Controls
 5.1 Using Controls
 5.2 Standard Controls
 5.3 Adding Controls
 5.4 Control Arrays
 5.5 Superclassing

6. Code
7. Forms
8. Debugging Code
9. Error Handling
10. Objects
11. User Controls
12. Data Projects
13. API
14. Distribution

Other Resources


Affiliated Links:
www.freefunfings.com


5. Controls

5.1 Using Controls

The Controls used within Visual Basic have many regular characteristics. It is not the intention for this document to be a reference for every Property, Method and Event associated with each of the Controls in Visual Basic.

Using a control is usually quite straightforward. Once the control is drawn onto a Container, it's Properties can be seen in the Properties Window. Although some Properties can only be set whilst in the design environment, the majority of them are available during development and at run time.

Exercise:
  • Create a new "Standard EXE" project in Visual Basic.
  • Drop a Command Button onto the Form and double-click the Form to open the Code View window.
  • Type in the name of the CommandButton followed by a dot (".").
	e.g. Command1.
You will notice that Visual Basic will now prompt you for the Property or Method that you want to use. A drop-down list appears, showing all possible Properties and Methods for that control (this also applies to other Objects, not just controls).

The effect of modifying a Property or invoking a Method depends upon which type of object is being used.

5.1.1 Properties
A Control's Properties are those characteristics which determine it's appearance or modify it's functionality in some way. In general, Control properties can be both read and written. An example of a Property is "BackColor", used to set the background colour for the Control.

Properties can be changed programmatically as follows:

        Object.PropertyName = NewValue

e.g.    Form1.Caption = "My Program Form 1"
Properties can be read programmatically as follows:

        MyVariable = Object.PropertyName

e.g.    strCapMain = Form1.Caption
Although Control Properties must be specified as above, each Control has an assigned Default Property, so that an error is not encountered if omitted.

For example, the Label control's Default Property is "Caption", so the following two lines of Visual Basic code have the same effect:

Label1 = "This is the Caption"
Label1.Caption = "This is the Caption"
It is recommended that you explicitly specify which property you are accessing, as strange results can be produced if you are not careful about modifying the default property for a given object.

5.1.2 Methods
An Object's Methods are the procedures (Subroutines and/or Functions) embedded within the Object. Methods are invoked from within the Visual Basic code. An example of a Method is the "Show" Method which, as we saw, belongs to the Form object. Methods can also pass and return parameters - the "Show" Method had a parameter of "1" after it (this is the parameter for the "Modal" status of the form once shown - a "1" tells the form that it is modal - it must be closed before the application can continue).

Methods which return no value are called Subs (short for subroutines) and are invoked as follows:

        Object.MethodName Parameter1, -etc-, ParameterN

e.g.    Command1.Move 100, 200, 700, 330
Methods which do return a value are called Functions and are invoked as follows:

        MyVariable = Object.MethodName (Parameter1, -etc-, ParameterN)

e.g.    lngPointColor = Picture1.Point (100, 200)
In addition to using an object's existing Methods, you can write your own Subs and Functions for objects in your application. This is explained in more detail later in the course.

Note:  Note that a Function's parameters must be contained within parentheses. Whether a Method returns a value or not, it may still receive values. Methods may receive any number of parameters, but may only return one, this return value is of the Type declared in the Function (see later).

5.1.3 Events
Events happen asynchronously to application execution. This means that an Event can occur whilst your program is in the middle of doing something else. This is handled by the Windows environment, which invokes the object's Event Method if the Event is triggered.

In the example above, we saw that the CommandButton had a "Click" Event. The Visual Basic code we typed into the Event Method was executed only when the Event occurred - i.e. The Form appeared only when we clicked the button. There are many different types of Event, which depend upon the Object being referred to. For example, if we wanted the Form to appear even when we moved the mouse over the CommandButton, we would remove the Visual Basic code in the "Click" Event Method and add it to the CommandButton's "MouseMove" Event Method.

Events may contain parameters, just like any other Method. You can use these parameters within the Event Method to react in different ways to an Event.

Exercise vi:
  • Copy, rename and re-open our example Project and open the Code View for Form1.
  • At the moment, we only have an Event Method for a click of "Command1". We need to add the "MouseMove" Event.
  • At the top of the Code View window there are two combo-list boxes. The right-hand one lists all Methods declared in the current object. Drop the list down and select "MouseMove".
  • Move the "Form2.Show 1" line out of the "Click" Event and into the "MouseMove" Event.
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Form2.Show 1
End Sub
  • Run the application and test it out.
Note:  "MouseMove" Event parameters:

The "Button" parameter contains the mouse button states (if any), "Shift" contains the Shift/Ctrl/Alt key states and the X and Y parameters are the mouse location on the object.

  • Try adjusting the code again, so that the form only loads if the Shift key is held when the mouse moves over the CommandButton.

<<Page 4  :   Top  :   Page 5.2>>
©vbtraining.co.uk