An object is a sort of user interface element that may be added to a Visual Basic form using a toolbox control. In actuality, a form is an object in Visual Basic. Every Visual Basic control is made up of three key components −
- Properties: describe the object,
- Methods: make an object to do something
- Events: what happens when an object does something.
Control Properties
By changing their properties, all Visual Basic Objects can be moved, scaled, or altered. A property is a value or feature that a Visual Basic object, such as Caption or ForeColor, possesses.
The Properties window can be used to set properties at design time, or statements in the program code can be used during run time.
Object. Property = Value
Where
- Object: the name of the object that is being customized.
- Property: the characteristic that needs to be changed.
- Value: new property setting.
For example,
Form1.Caption = "Hello"
Using the Properties Window, you can modify any of the form's properties. The majority of the properties can be set or read while the application is running. A complete list of characteristics connected with various controls and limits applied to them may be found in Microsoft documentation.
Control Methods
A method is a procedure that is created as a class member and causes an object to do something. Methods are used to gain access to or manipulate the properties of an object or variable. You will mostly utilize two types of methods in your classes:
- You can call any of the public methods of control, such as one given by the Toolbox, if you are utilizing it. The prerequisites of such a method are determined by the class that is being utilized.
- If none of the current methods can do the job, you can add a method to a class.
The MessageBox control, for example, contains a method entitled Show, which is called in the code snippet below −
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
MessageBox.Show("Hello, World")
End Sub
End Class
Control Events
An event is a signal that alerts an application to the occurrence of a significant event. When a user clicks a control on a form, for example, the form can raise a Click event and invoke a method that handles the event. A Form can be associated with various events such as click, double click, close, load, resize, and so on.
The default structure of a form Load event handler subroutine is shown below. You can see this code by double-clicking it, which will give you a comprehensive list of all Form control events −
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'event handler code goes here
End Sub
Handles MyBase.Load specifies that the Load event is handled by the Form1 Load() subroutine. In a similar manner, you can examine stub code for click and double click. If you want to initialize variables such as properties, put such code inside the Form1 Load() subroutine. The name of the event handler, which is by default Form1 Load, is vital to remember here, but you can alter this name based on the naming convention you use in your application development.
Basic Controls
VB.Net includes a plethora of controls that can be used to create a complex user interface. All of these controls' functionalities are defined in their respective control classes. The System defines the control classes. Windows. Namespace is formed.
The System.Windows.Forms namespace defines the control classes. Commonly used controls are Forms, Label, Textbox, Button, ListBox, ComboBox, RadioButton, etc.