Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Part 6.2Control properties, methods and events

Part 6.2Control properties, methods and events

Properties

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:

  • Age
  • Breed
  • Color
  • Size

Properties can be declared in the class of a control using:

VB.NET
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
		

Methods

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:

  • Crouch
  • Jump
  • Run
  • Walk

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).

VB.NET
Dim btn As Button = Button1
btn.PerformClick()
		

In this instance the PerformClick method is being called.

Events

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:

  • Enter House
  • Eat
  • Drink
  • Bark

An event leads to code being processed, so when the dog raises the event Enter House it may call the code for the method Bark.

Events must be handled by an event handler. VB.NET makes it easy to capture events that may occur by using the Handles keyword:

VB.NET
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 finished:

VB.NET
Private Sub EventRaiser()
	For(i As Integer = 0 To 100000)
		RaiseEvent Done()
	End For
End Sub
		

Examples

The following are examples for a Button control.

Properties of a button

  • BackColor
  • ForeColor
  • Location
  • Size
  • Text
  • Visible
  • (name)

Methods of a button

  • CreateGraphics
  • Show
  • Hide
  • PerformClick

Events of a button

  • GotFocus
  • KeyDown
  • MouseClick
  • MouseDown
  • MouseMove
  • MouseUp
Feedback 👍
Comments are sent via email to me.