Control properties, methods and events6.2
A property is used to describe the state of an object that is active in memory. For example, a dog, clearly has the following properties:
Properties can be declared in the class of a control using:
Dim background As Color = Color.Black <Category("Appearance")> Public Property backgroundColour As Color Get Return background End Get Set(ByVal value As Color) background = value End Set End Property
As the VB.NET
Control type is a class that inherits from the
Object type, controls can have methods. A method is what
the object can do. A dog can:
The dog can be told to perform a
jump, or make it run, so it is known as a method. By using these
methods they are being called.
Call can be placed at the front of
a method to show that it is being called (although it is not strictly necessary).
Dim btn As Button = Button1 btn.PerformClick()
In this instance the
PerformClick method is being called.
An event is what reaction something has when the object does something. For instance, a dog may have the event Eat. When the Eat event is triggered, the dog is told to Drink. Generally speaking, events are similar to methods, so some example events for the dog could be:
An event leads to code being processed, so when the dog raises
Enter House it may call
the code for the method
Events must be handled by an event handler. VB.NET makes it easy to capture events that may occur by using
Private Sub RichTextBox_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkClickedEventArg) Handles Me.LinkClicked MsgBox("Link clicked!") End Sub
Events themselves can be fired through the
RaiseEvent method. This can be particularly
useful when defining a custom control and using graphics to draw controls (raise the event when a part of the window
is clicked or hovered). It can also be used when a background task is completed to inform the application that it has
Private Sub EventRaiser() For(i As Integer = 0 To 100000) RaiseEvent Done() End For End Sub
The following are examples for a Button control.
Properties of a button
Methods of a button
Events of a button