Events Methods and Properties in VB .Net
Common VB .Net Events
- In Visual Basic .NET (VB.NET), common events refer to the events that are frequently used to respond to user interactions and system notifications in Windows Forms applications.
- These events are associated with various common controls and are used to trigger specific actions or behaviors.
- These events are used and coded as per the requirement of the user’s application.
- Some of the common events in VB.NET are as follows:-
Click:
- Occurs when the user clicks a control, such as a button or a PictureBox.
DoubleClick:
- Occurs when the user double-clicks a control.
MouseEnter:
- Occurs when the mouse pointer enters the boundaries of a control.
MouseLeave:
- Occurs when the mouse pointer exits the boundaries of a control.
MouseHover:
- Occurs when the mouse pointer hovers over a control for a specified duration without moving.
MouseClick:
- Occurs when the user clicks a control using the mouse.
MouseDoubleClick:
- Occurs when the user double-clicks a control using the mouse.
KeyPress:
- Occurs when a key is pressed while the control has focus.
KeyUp:
- Occurs when a key is released while the control has focus.
KeyDown:
- Occurs when a key is pressed down while the control has focus.
TextChanged:
- Occurs when the text within a TextBox or ComboBox control is changed.
SelectionChanged:
- Occurs when the selected item in a ListBox or ComboBox changes.
ValueChanged:
- Commonly used with controls like NumericUpDown, TrackBar, and DateTimePicker to respond to changes in their values.
CheckedChanged:
- Occurs when the state of a CheckBox or RadioButton control changes.
ItemClicked (for MenuStrip):
- Occurs when a menu item is clicked in a MenuStrip control.
LinkClicked (for LinkLabel):
- Occurs when a link is clicked in a LinkLabel control.
FormClosing:
- Occurs when a form is about to close, allowing us to handle closing operations.
Load:
- Occurs when a form or control is loaded and becomes visible.
Resize:
- Occurs when the size of a form or control is changed.
Activated:
- Occurs when a form or control becomes the active window.
Deactivate:
- Occurs when a form or control loses focus.
- Timer Tick:
- Occurs at regular intervals when a Timer control is enabled.
Common VB .Net Methods
- Visual Basic .NET (VB.NET) is a versatile programming language that offers a wide range of in-built methods and functions for various tasks.
- VB.NET provides a rich set of in-built methods and functions for various tasks, and we can also create our own custom methods to encapsulate reusable functionality in our applications as per requirements.
- Some common in-built methods in VB.NET can be categorized into several areas, which are as follows:-
(A) String Manipulation Methods:
String.Concat():
- This method concatenates two or more strings into a single string.
- For example – Dim result As String = String.Concat(“Hello”, ” “, “India”)
- String.Length:
- This method returns the length (number of characters) present in a string.
- For example – Dim length As Integer = “Hello India”.Length
- String.Substring():
- This method extracts a portion of a string.
- For example – Dim subString As String = “Hello World”.Substring(0, 5) [Output = Hello]
- String.ToUpper() and String.ToLower():
- This method converts a string to uppercase or lowercase respectively.
- For example – (i) Dim uc As String = “Hello”.ToUpper() [Output = HELLO]
- (ii) Dim lc As String = “World”.ToLower() [Output = world]
(B.) Mathematical Methods:
Math.Abs():
This method returns the absolute value(+) of a number.
For example - Dim
AV As Double = Math.Abs(-5.5) [Output = 5.5]
Math.Round():
- This method rounds a floating-point number to the nearest integer.
- For example – Dim roundedValue As Integer = Math.Round(3.7) [Output = 4]
Math.Max() and Math.Min():
- This method returns the maximum or minimum of two numbers.
- For example – (i)Dim maxValue As Integer = Math.Max(10, 5)
[Output = 10]
- (ii)Dim minValue As Integer = Math.Min(10, 5)
[Output = 5]
(C) File and I/O Methods:
- System.IO.File.ReadAllText():
- This method reads the contents of a text file into a string.
- For example – Dim text As String = System.IO.File.ReadAllText(“file.txt”)
- System.IO.File.WriteAllText(path, text):
- Writes a string or contents to a text file, overwriting its contents.
- For example – System.IO.File.WriteAllText(“file.txt”, “Hello, World!”)
System.IO.File.Exists():
- This method checks if a file exists or not at the specified path.
- Syntax: File.Exists(path)
- For example – Dim exists As Boolean = System.IO.File.Exists(“file.txt”)
File.ReadAllLines(path):
Reads all lines from a text file into an array.
- Directory.CreateDirectory(path):
- Creates a directory at the specified path.
(D) Date and Time Methods:
- DateTime.Now(): Gets the current date and time.
- DateTime.Today(): Gets the current date without the time.
- DateTime.AddDays(days): Adds days to a date.
- DateTime.Parse(dateString): Parses a string into a DateTime object.
(E) Conversion Methods:
- Convert.ToInt32(value): Converts a value to an integer.
- Convert.ToDouble(value): Converts a value to a double.
- Convert.ToString(value): Converts a value to a string.
(F) MessageBox Methods:
- MessageBox.Show(message): Displays a message box with a message.
- MessageBox.Show(message, caption): Displays a message box with a message and caption.
- MessageBox.Show(message, caption, buttons): Displays a message box with specified buttons.
(G) User-Defined Methods:
- We can create our own methods in VB.NET to perform custom operations.
- These methods are defined using the Function or Sub keyword.
(H) Console Input/Output Methods:
- Console Input():
- Console.ReadLine() – Used to read user input from the console.
For Example - Dim userInput As String = Console.ReadLine()
- Console.ReadLine() – Used to read user input from the console.
- Console Output():
- Console.WriteLine() – Used to display text on the console.
For example - Console.WriteLine("Hello, World!")
- Console.WriteLine() – Used to display text on the console.
Common VB .Net Properties
- In Visual Basic .NET (VB.NET), properties are special member variables that provide controlled access to an object’s data.
- Properties allow us to get and set the values of private fields while encapsulating the internal details of the class.
- There are some common types of properties in VB.NET. Properties are essential for encapsulating data and providing controlled access to class members, promoting good object-oriented programming practices and data encapsulation.
- Some common VB.NET properties are:-
1. Get Property (Read-Only Property):
- A get-only property allows us to retrieve the value of a private field but doesn’t allow us to modify it.
- For Example:-
Public ReadOnly Property FirstName As String
Get
Return first_name
End Get
End Property
2. Set Property (Write-Only Property):
- A set-only property allows us to set the value of a private field but doesn’t allow us to retrieve it.
- For Example:
Public WriteOnly Property Age As Integer
Set(value As Integer)
age = value
End Set
End Property
3. Get-Set Property (Read-Write Property):
- A get-set property allows both getting and setting the value of a private field.
- For Example:
Public Property LastName As String
Get
Return last_name
End Get
Set(value As String)
Last_Name = value
End Set
End Property
Auto-Implemented Property:
- An auto-implemented property is a shorthand syntax for creating properties when we don’t need additional logic in the getter or setter. The compiler generates the private field for us.
- For Example:
Public Property Str As String
Read-Only Auto-Implemented Property:
- This property is similar to an auto-implemented property, but it’s read-only.
- For Example:
Public ReadOnly Property Str As String
Set-Only Auto-Implemented Property:
- This property is similar to an auto-implemented property, but it’s write-only.
- For Example:
Public WriteOnly Property IsEnabled As Boolean
Indexed Property:
- An indexed property allows us to access elements of an object as if it were an array.
- For Example:
Public Property Items(index As Integer) As String
Get
Return items(index)
End Get
Set(value As String)
items(index) = value
End Set
End Property
Default Property:
- A default property allows us to access an object using an index or key without specifying the property name.
- Example:
Default Public Property Item(index As Integer) As String
Get
Return items(index)
End Get
Set(value As String)
items(index) = value
End Set
End Property
Shared Property (Shared Member):
- A shared property is associated with the class itself rather than with instances of the class.
- It’s accessed using the class name.
- For Example:
Public Shared Property CompanyName As String
Custom Properties:
- We can create our own custom properties with additional logic in the getter or setter to perform validation, calculations, or other operations as needed.
0 Comments