Uploaded August 2019 | Updated September 2026, 1 week ago
Steps to add multiple panels at runtime that contain other controls such as labels and buttons in VB.NET using a flow layout panel
#vbdotnet
Visual Studio Community 2017
.NET Framework 4.6.1
Windows 10
Partial Code:
---------------------
'Indicates current contact panel to add controls to
Private _CurrentContactPanelName As String = Nothing
'Used to give unique control names such as label1, label2 etc
Private _ContactPanelsAddedCount As Integer = 0
'Add contact panel to flow layout panel
Public Sub CreateContactPanel()
Dim contactPanel As Panel
contactPanel = New Panel()
'Set panel properties
With contactPanel
.BackColor = Color.White
.Size = New Size(420, 50)
.Name = "pnlContact" + (_ContactPanelsAddedCount + 1).ToString
End With
'Add panel to flow layout panel
flpMain.Controls.Add(contactPanel)
'Update panel variables
_CurrentContactPanelName = contactPanel.Name
_ContactPanelsAddedCount += 1
End Sub
'Add new delete button to contact panel
Private Sub CreateContactDeleteBtn(ByVal panelName As String)
Dim contactDeleteButton As Button
contactDeleteButton = New Button
'Set button properties
With contactDeleteButton
.AutoSize = False
.Size = New Size(90, 30)
.BackColor = Color.Silver
.ForeColor = Color.Black
.Location = New Point(300, 10)
.Name = "btnContactDelete" + (_ContactPanelsAddedCount).ToString
.Text = "Delete"
End With
'Add button to panel
For Each controlObject As Control In flpMain.Controls
If controlObject.Name = panelName Then
controlObject.Controls.Add(contactDeleteButton)
End If
Next
'Add handler for click events
AddHandler contactDeleteButton.Click, AddressOf DynamicButton_Click
End Sub
'Remove handlers and contact panel
Public Sub DynamicButton_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim parentPanelName As String
parentPanelName = Nothing
'Remove handler from sender
For Each controlObj As Control In flpMain.Controls
For Each childControlObj As Control In controlObj.Controls
If childControlObj.Name = sender.name Then
RemoveHandler childControlObj.Click, AddressOf DynamicButton_Click
parentPanelName = childControlObj.Parent.Name
End If
Next
Next
'Remove contact panel
For Each controlObj As Control In flpMain.Controls
If controlObj.Name = parentPanelName Then
flpMain.Controls.Remove(controlObj)
controlObj.Dispose()
End If
Next
End Sub
Steps to add multiple panels at runtime that contain other controls such as labels and buttons in VB.NET using a flow layout panel
#vbdotnet
Visual Studio Community 2017
.NET Framework 4.6.1
Windows 10
Partial Code:
---------------------
'Indicates current contact panel to add controls to
Private _CurrentContactPanelName As String = Nothing
'Used to give unique control names such as label1, label2 etc
Private _ContactPanelsAddedCount As Integer = 0
'Add contact panel to flow layout panel
Public Sub CreateContactPanel()
Dim contactPanel As Panel
contactPanel = New Panel()
'Set panel properties
With contactPanel
.BackColor = Color.White
.Size = New Size(420, 50)
.Name = "pnlContact" + (_ContactPanelsAddedCount + 1).ToString
End With
'Add panel to flow layout panel
flpMain.Controls.Add(contactPanel)
'Update panel variables
_CurrentContactPanelName = contactPanel.Name
_ContactPanelsAddedCount += 1
End Sub
'Add new delete button to contact panel
Private Sub CreateContactDeleteBtn(ByVal panelName As String)
Dim contactDeleteButton As Button
contactDeleteButton = New Button
'Set button properties
With contactDeleteButton
.AutoSize = False
.Size = New Size(90, 30)
.BackColor = Color.Silver
.ForeColor = Color.Black
.Location = New Point(300, 10)
.Name = "btnContactDelete" + (_ContactPanelsAddedCount).ToString
.Text = "Delete"
End With
'Add button to panel
For Each controlObject As Control In flpMain.Controls
If controlObject.Name = panelName Then
controlObject.Controls.Add(contactDeleteButton)
End If
Next
'Add handler for click events
AddHandler contactDeleteButton.Click, AddressOf DynamicButton_Click
End Sub
'Remove handlers and contact panel
Public Sub DynamicButton_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim parentPanelName As String
parentPanelName = Nothing
'Remove handler from sender
For Each controlObj As Control In flpMain.Controls
For Each childControlObj As Control In controlObj.Controls
If childControlObj.Name = sender.name Then
RemoveHandler childControlObj.Click, AddressOf DynamicButton_Click
parentPanelName = childControlObj.Parent.Name
End If
Next
Next
'Remove contact panel
For Each controlObj As Control In flpMain.Controls
If controlObj.Name = parentPanelName Then
flpMain.Controls.Remove(controlObj)
controlObj.Dispose()
End If
Next
End Sub





![VB.NET - Sort listview by any column
Steps to sort vb.net listview by any column (dates, ints, string or doubles)
#vbdotnet
Code:
Public Class ListViewItemComparer
Implements IComparer
Private col As Integer
Private order As SortOrder
Public Sub New()
col = 0
order = SortOrder.Ascending
End Sub
Public Sub New(column As Integer, order As SortOrder)
col = column
Me.order = order
End Sub
Public Function Compare(x As Object, y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim returnVal As Integer
Try
Attempt to parse the two objects as DateTime
Dim firstDate As System.DateTime = DateTime.Parse(CType(x, ListViewItem).SubItems(col).Text)
Dim secondDate As System.DateTime = DateTime.Parse(CType(y, ListViewItem).SubItems(col).Text)
Compare as date
returnVal = DateTime.Compare(firstDate, secondDate)
Catch ex As Exception
If date parse failed then fall here to determine if objects are numeric
If IsNumeric(CType(x, ListViewItem).SubItems(col).Text) And
IsNumeric(CType(y, ListViewItem).SubItems(col).Text) Then
Compare as numeric
returnVal = Val(CType(x, ListViewItem).SubItems(col).Text).CompareTo( _
Val(CType(y, ListViewItem).SubItems(col).Text))
Else
If not numeric then compare as string
returnVal = [String].Compare(CType(x, _
ListViewItem).SubItems(col).Text, CType(y, ListViewItem).SubItems(col).Text)
End If
End Try
If order is descending then invert value
If order = SortOrder.Descending Then
returnVal *= -1
End If
Return returnVal
End Function
End Class
Value to track which column was previously sorted
Dim sortColumn as Integer = -1
Private Sub Listview1_ColumnClick(sender As Object, e As System.Windows.Forms.ColumnClickEventArgs) Handles Listview1.ColumnClick
If current column is not the previously clicked column
Add
If Not e.Column = sortColumn Then
Set the sort column to the new column
sortColumn = e.Column
Default to ascending sort order
Listview1.Sorting = SortOrder.Ascending
Else
Flip the sort order
If Listview1.Sorting = SortOrder.Ascending Then
Listview1.Sorting = SortOrder.Descending
Else
Listview1.Sorting = SortOrder.Ascending
End If
End If
Set the ListviewItemSorter property to a new ListviewItemComparer object
Me.Listview1.ListViewItemSorter = New ListViewItemComparer(e.Column, Listview1.Sorting)
Call the sort method to manually sort
Listview1.Sort()
End Sub
End Class VB.NET - Sort listview by any column](https://i.ytimg.com/vi/fRMztyQ06xI/mqdefault.jpg)




