Imports System.ComponentModel
Imports System.Threading
Public Class Form1
Enum ListAction
ADD
REMOVE
End Enum
Public Class Device
Public Property DeviceKey As UInteger
Public Property Name As String
Public Property Progress As UInteger
End Class
Dim myDevice As New Device
Dim threadDevice As Device
Dim DeviceList As New BindingList(Of Device)
Dim bs As New BindingSource(DeviceList, Nothing)
Dim AddOrRemove As ListAction = ListAction.ADD
Dim worker As Thread
Private Delegate Sub DeviceListActionDelegate(ByVal deviceContext As Object)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' Bind DataSource List item properties to the DataRepeaterItem controls.
Label1.DataBindings.Add("Text", bs, "Name")
Label2.DataBindings.Add("Text", bs, "Progress")
Repeater1.DataSource = bs
' Create and populate 2 Devices and add them to the List.
myDevice = New Device
myDevice.DeviceKey = 0
myDevice.Name = "Test1"
myDevice.Progress = 50
Me.DeviceList.Add(myDevice)
myDevice = New Device
myDevice.DeviceKey = 1
myDevice.Name = "Test2"
myDevice.Progress = 50
Me.DeviceList.Add(myDevice)
' Show the Form.
Me.Show()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If AddOrRemove = ListAction.ADD Then
' Populate new device.
threadDevice = New Device()
threadDevice.DeviceKey = 3
threadDevice.Name = "ThreadDevice"
threadDevice.Progress = 40
' Start thread to add device.
worker = New Thread(AddressOf DoAdd)
worker.Start(threadDevice)
' Flip flag.
AddOrRemove = ListAction.REMOVE
Else
threadDevice = New Device
' Remove last item from the List.
worker = New Thread(AddressOf DoRemove)
worker.Start(threadDevice)
' Flip flag
AddOrRemove = ListAction.ADD
End If
End Sub
Private Sub DoAdd(ByVal deviceContext As Object)
AddToDeviceList(deviceContext)
End Sub
Private Sub DoRemove(ByVal deviceContext As Object)
RemovefromDeviceList(deviceContext)
End Sub
Private Sub AddToDeviceList(ByVal deviceContext As Object)
If Repeater1.InvokeRequired Then
Repeater1.Invoke(New DeviceListActionDelegate(AddressOf AddToDeviceList), deviceContext)
Else
DeviceList.Add(deviceContext)
End If
End Sub
Private Sub RemovefromDeviceList(ByVal deviceContext As Object)
If Repeater1.InvokeRequired Then
Repeater1.Invoke(New DeviceListActionDelegate(AddressOf RemovefromDeviceList), deviceContext)
Else
DeviceList.Remove(DeviceList.Last)
End If
End Sub
End Class