Events Methods and Properties in VB .Net

Common VB .Net Events

  • In Visual Basic .NET 2013 (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 requirements of the user’s application.
  • Some of the common events in VB.NET are as follows:-
  1. Click:

    • This event occurs when the user clicks a control, such as a button or a PictureBox.
  2. DoubleClick:

    • This event occurs when the user double-clicks a control.
  3. MouseEnter:

    • This event occurs when the mouse pointer enters the boundaries of a control.
  4. MouseLeave:

    • This event occurs when the mouse pointer exits the boundaries of a control.
  5. MouseHover:

    • This event occurs when the mouse pointer hovers over a control for a specified duration without moving.
  6. MouseClick:

    • This event occurs when the user clicks a control using the mouse.
  7. MouseDoubleClick:

    • This event occurs when the user double-clicks a control using the mouse.
  8. KeyPress:

    • This event occurs when a key is pressed while the control has focus.
  9. KeyUp:

    • This event occurs when a key is released while the control has focus.
  10. KeyDown:

    • This event occurs when a key is pressed down while the control has focus.
  11. TextChanged:

    • This event occurs when the text within a TextBox or ComboBox control is changed.
    • SelectionChanged:

      • This event occurs when the selected item in a ListBox or ComboBox changes.
    • ValueChanged:

      • This event is commonly used with controls like NumericUpDown, TrackBar, and DateTimePicker to respond to changes in their values.
    • CheckedChanged:

      • This event occurs when the state of a CheckBox or RadioButton control changes.
    • ItemClicked (for MenuStrip):

      • This event occurs when a menu item is clicked in a MenuStrip control.
    • LinkClicked (for LinkLabel):

      • This event occurs when a link is clicked in a LinkLabel control.
    • FormClosing:

      • This event occurs when a form is about to close, allowing us to handle closing operations.
    • Load:

      • This event occurs when a form or control is loaded and becomes visible.
    • Resize:

      • This event occurs when the size of a form or control is changed.
    • Activated:

      • This event occurs when a form or control becomes the active window.
    • Deactivate:

      • This event occurs when a form or control loses focus.
    • Timer Tick:
      • This event 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 built-in methods and functions for various tasks.
    • VB.NET provides a rich set of built-in 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 built-in methods in VB.NET can be categorized into several areas, which are as follows:-

    (A) String Manipulation Methods:

    1. String.Concat():

      • This method concatenates two or more strings into a single string.
      • For example: 

    Module Module1
    Sub main()
    Dim result As String = String.Concat(“Hello”, ” “, “India”)
    Console.WriteLine(result)
    Console.ReadKey()
    End Sub
    End Module

    —————  OR  —————

    Module Module1
    Dim x, y As String
    Sub main()
    x = “Hello”
    y = “India”
    Dim result As String = String.Concat(x, ” “, y)
    Console.WriteLine(result)
    Console.ReadKey()
    End Sub
    End Module

    1. String.Length:
      • This method returns the length (number of characters) present in a string.
      • For example – 

    Module Module1
    Sub main()
    Dim length As Integer = “Hello India”.Length
    Console.WriteLine(length)
    Console.ReadKey()
    End Sub
    End Module

    —————  OR  —————

    Module Module1
    Dim x As String
    Sub main()
    x = “Hello”
    Dim result As Integer = x.Length
    Console.WriteLine(result)
    Console.ReadKey()
    End Sub
    End Module

    1. String.Substring():
      • This method extracts a portion of a string.
      • For example –

    Module Module1
    Sub Main()
    ‘Dim SS As String = “Hello World”.Substring(0, 2) ‘= He
    ‘Dim SS As String = “Hello World”.Substring(3, 5) ‘= lo Wo
    ‘Dim SS As String = “Hello World”.Substring(6, 5) ‘= World
    Dim SS As String = “Hello World”.Substring(2, 7) ‘= llo Wor
    Console.WriteLine(SS)
    Console.ReadKey()
    End Sub
    End Module

    1. String.ToUpper() and String.ToLower():
      • These methods convert 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]

    Method / Property

    Purpose

    Example

    Dim str As String = “Hello World”

    Trim()

    Removes spaces from both ends

    str.Trim()

    TrimStart()

    Removes spaces from the beginning

    str.TrimStart()

    TrimEnd()

    Removes spaces from the end

    str.TrimEnd()

    Replace()

    Replaces text

    str.Replace(“World”, “VB.NET”)

    Contains()

    Checks whether text exists

    str.Contains(“World”)’=True

    StartsWith()

    Checks the beginning of a string

    str.StartsWith(“Hello”)

    EndsWith()

    Checks the end of a string

    str.EndsWith(“World”)

    IndexOf()

    Finds the first position of text

    str.IndexOf(“World”)’=6

    LastIndexOf()

    Finds the last occurrence

    str.LastIndexOf(“o”)’=7

    Insert()

    Inserts text at a position

    str.Insert(5, ” VB”)’=Hello VB World

    Remove()

    Removes characters from the beginning

    str.Remove(8)’=Hello Wo

    Equals()

    Compares two strings

    str.Equals(“Hello World”)

    PadLeft()

    Adds padding on the left

    str.PadLeft(15)’=    Hello World

    PadRight()

    Adds padding on the right

    str.PadRight(15)

    (B.) Mathematical Methods:

    1. Math.Abs():

      • This method returns the absolute value(+) of a number.

        For example - Dim AV As Double = Math.Abs(-5.5) [Output = 5.5]
    2. 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]

    Dim roundedValue As Integer = Math.Round(3.2) [Output = 3]

    1. Math.Max() and Math.Min():

      • These methods return 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] 

    Method

    Purpose

    Example

    Result

    Math.Sqrt()

    Square root

    Math.Sqrt(25)

    5

    Math.Pow()

    Raises a number to a power

    Math.Pow(2, 3)

    8

    Math.Floor()

    Rounds downward

    Math.Floor(12.89)

    12

    Math.Ceiling()

    Rounds upward

    Math.Ceiling(12.11)

    13

    Math.Truncate()

    Removes the fractional part

    Math.Truncate(12.89)

    12

    Math.Exp()

    Calculates e raised to a power

    Math.Exp(2)

    ≈ 7.389

    Math.Log()

    Natural logarithm

    Math.Log(10)

    ≈ 2.303

    Math.Log10()

    Base-10 logarithm

    Math.Log10(100)

    2

    Math.Sin()

    Gives Sine value

    Math.Sin(Math.PI / 2)

    1

    Math.Cos()

    Gives Cosine value

    Math.Cos(0)

    1

    Math.Tan()

    Gives Tangent value

    Math.Tan(0)

    0

    (C) File and I/O Methods:

    1. 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”)
    2. 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!”)
    3. System.IO.File.Exists():

      • This method checks whether 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”)
    4. File.ReadAllLines(path):

      • Reads all lines from a text file into an array.

    5. Directory.CreateDirectory(path):
      • Creates a directory at the specified path.

    (D) Date and Time Methods:

    • DateTime.Now(): Gets the current date and time.

    Console.WriteLine(DateTime.Now()) [12-09-2026 08:35:34]

    • DateTime.Today(): Gets the current date without the time.

    Console.WriteLine(DateTime.Today()) [12-09-2026 00:00:00]

    • DateTime.Now.AddDays(days): Adds days to a date.

    Console.WriteLine(DateTime.Now.AddDays(10)) [adds 10 days in current date]

    • DateTime.Parse(dateString): Parses a string into a DateTime object.

    Property / Method

    Purpose

    Output

    Console.WriteLine(DateTime.Now.Date)

    Date portion

    12-09-2026 00:00:00

    DateTime.Now.Day

    Displays the day value of the date

    12

    DateTime.Now.Month

    Displays the month number of a Date

    9

    DateTime.Now.Year

    Displays the year value of a Date

    2026

    DateTime.Now.Hour

    Displays the hour value from a Time

    10

    DateTime.Now.Minute

    Displays the minute value from a Time

    42

    DateTime.Now.Second

    Displays the second value from a Time

    37

    DateTime.Now.DayOfWeek

    Value of the weekday as a number

    6(=saturday)

    DateTime.Now.DayOfYear

    Day number of the year

    255

    DateTime.Now.AddDays(10)

    Adds the days value to the current date

    22-09-2026 10:30:40

    DateTime.Now.AddMonths(2)

    Adds a month value to the current date

    12-11-2026 10:31:22

    DateTime.Now.AddYears(1)

    Adds a year value to the current date

    12-09-2027 10:33:05

    DateTime.Now.AddHours(5)

    Adds hours value to the current time

    12-09-2026 15:33:57

    DateTime.Now.AddMinutes(30)

    Adds a minutes value to the current time

    12-09-2026 11:04:45

    DateTime.Now.AddSeconds(200)

    Adds a seconds value to the current time

    12-09-2026 10:39:07

    DateTime.Now.ToShortDateString()

    Short date representation

    12-09-2026

    DateTime.Now. ToLongDateString()

    Long date representation

    12 September 2026

    DateTime.Now. ToShortTimeString()

    Short time representation

    10:37 AM

    DateTime.Now. ToLongTimeString()

    Long-time representation

    10:38:14

    DateTime.Now. ToString(“dd-MM-yyyy”)

    Formats date/time

    12-09-2026

    DateTime.Now. ToString(“dd-MMM-yyyy”)

    Formats date/time

    12-Sep-2026

    (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 Output():
      • Console.WriteLine() – Used to display text on the console.
      • For example - Console.WriteLine("Hello, World!")

    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

      Leave a Reply

      Your email address will not be published. Required fields are marked *

      This site uses Akismet to reduce spam. Learn how your comment data is processed.