|
|
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
|
5. Controls5.1 Using ControlsThe 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:
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 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 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 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:
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Form2.Show 1
End Sub
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.
<<Page 4 : Top : Page 5.2>> |