VB.Net Software
Address Manager
Address Manager is a VB.NET Windows Forms application designed to manage a list of address entries. You can add, edit, delete, search, and filter entries, as well as save and load data from a JSON file.
Snippets
- Permutation Search
- Binary Converter
- Text Formatter
- Collate-DeCollate
- Colmize
- Complex
- Currency Formatting
-
3 Ways To Find an Item by ID in a Visual Basic List
Module Module1
Sub Main()
' Define a list of users
Dim users As New List(Of User) From {
New User(1, "John"),
New User(2, "Jane"),
New User(3, "Bob")
}
' Using LINQ's Find method to find the user with ID 2
Dim user As User = users.FirstOrDefault(Function(u) u.Id = 2)
Console.WriteLine(user.ToString()) ' Output: User ID: 2, Name: Jane
' Using LINQ's Filter method to find the user with ID 2
user = users.Where(Function(u) u.Id = 2).FirstOrDefault()
Console.WriteLine(user.ToString()) ' Output: User ID: 2, Name: Jane
' Using ForEach loop to find the user with ID 2
Dim foundUser As User = Nothing
users.ForEach(Sub(item)
If item.Id = 2 Then
foundUser = item
End If
End Sub)
Console.WriteLine(foundUser.ToString()) ' Output: User ID: 2, Name: Jane
End Sub
' Define a User class
Public Class User
Public Property Id As Integer
Public Property Name As String
Public Sub New(id As Integer, name As String)
Me.Id = id
Me.Name = name
End Sub
Public Overrides Function ToString() As String
Return $"User ID: {Id}, Name: {Name}"
End Function
End Class
End Module
-
3 Ways To Get The Selected Value in a Visual Basic Dropdown List
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Populate the ComboBox with options
ComboBox1.Items.Add("Option 1") ' Value: option1
ComboBox1.Items.Add("Option 2") ' Value: option2
ComboBox1.Items.Add("Option 3") ' Value: option3
End Sub
' Function 1: Get selected value directly
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ComboBox1.SelectedIndex <> -1 Then
Dim selectedValue As String = ComboBox1.SelectedItem.ToString()
MessageBox.Show("Selected Value (Func 1): " & selectedValue)
Else
MessageBox.Show("No option selected.")
End If
End Sub
' Function 2: Get selected value using SelectedIndex
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If ComboBox1.SelectedIndex <> -1 Then
Dim selectedIndex As Integer = ComboBox1.SelectedIndex
Dim selectedValue As String = ComboBox1.Items(selectedIndex).ToString()
MessageBox.Show("Selected Value (Func 2): " & selectedValue)
Else
MessageBox.Show("No option selected.")
End If
End Sub
' Function 3: Iterate through all options to find selected value
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim selectedValue As String = ""
For i As Integer = 0 To ComboBox1.Items.Count - 1
If ComboBox1.Items(i).ToString() = ComboBox1.SelectedItem?.ToString() Then
selectedValue = ComboBox1.Items(i).ToString()
Exit For
End If
Next
If Not String.IsNullOrEmpty(selectedValue) Then
MessageBox.Show("Selected Value (Func 3): " & selectedValue)
Else
MessageBox.Show("No option selected.")
End If
End Sub
End Class