Your current position:
Home 
  Visual Basic
    Programming with objects
      Working with controls

Working with controls. Using Properties, Methods and Events 

You use controls to get user input and to display output. Some of the controls you can use in your applications include text boxes, command buttons, and list boxes. Other controls let you access other applications and process data as if the remote application was part of your code. Each control has its own set of properties, methods, and events that can be modified from the properties window or from the code  Properties are simple to understand. Here's an example. A human being, he/she has eyes, legs, arms, etc. They are all properties of the human being.
A property describes an object, its behavior and its look. If you wanted to change the color of the eyes you would just do this:

Human.Eyes = Brown
Human.Weight = 156

If you wanted to change any other properties, you could do that too. As long as the property is available. For example, you cannot do this: Human.Beak = True. The nice thing about properties is that you do not have to know what it takes to actually change the human's eyes or weight. You just indicate that you want it to change.

An example of this is the Label control.

The label control has an important property, Caption. Without it, it would be almost useless. The Caption property sets or returns the text presently contained in the Label control. The only drawback is that you cannot directly enter text into the label. The text control is virtually identical except that you can type in text.

Label.Caption = "This is really easy"

Methods

In some cases, properties aren't enough to tell an object what it should do. It would be nice to give the object direct commands as well. Visual Basic lets you do this. Let's go back to the Human example. If you wanted the human to move its arm a method is a nice and easy way of doing that. The human could have a method called MoveArm, which we could use like an ordinary command:


Human.MoveArm


As you can see, you don't give a method a value, it will execute like any other Visual Basic command. As far as this is concerned, you really don't care what the human has to do to move its arm. All you want it to do is move its arm.

Events

Q:Ok we used the properties and the methods of an “human” object. In the previous example we have used the MoveArm method now how will we know when the human has ended the MoveArm method?

A:By using an event procedure.

An event procedure is simple to understand. These procedures are automatically declared by Visual Basic for each object that we use and are fired when that event occurs.

Example:

Draw a button on your form and then double click it, you will now see a procedure that looks something like this:

Private Sub Command1_Click()

End Sub

This is an event procedure and it’s fired when Command1 is clicked. You can observe the parts in the sub’s name: