specopkirbs
BANNED
- Nov 28, 2008
- 920
- 752
could anybody tell me how to populate listview subitems from either mulitple listboxes or multiple arrays im having great difficulty and will pay $5 if someone can provide me a quick and simple solution.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim obj(3) As TestObject
obj(0) = New TestObject With {.Item = "one", .Item2 = "test"}
obj(1) = New TestObject With {.Item = "two", .Item2 = "test2"}
obj(2) = New TestObject With {.Item = "three", .Item2 = "test3"}
dg.DataSource = obj
End Sub
Public Class TestObject
Private _Item As String
Private _Item2 As String
Public Property Item As String
Get
Return _Item
End Get
Set(ByVal value As String)
_Item = value
End Set
End Property
Public Property Item2 As String
Get
Return _Item2
End Get
Set(ByVal value As String)
_Item2 = value
End Set
End Property
End Class
Dim items As New List(Of ListViewItem)
' process an item
Dim lvItem = New ListViewItem("the item")
lvItem.SubItems.Add("subitem1")
lvItem.SubItems.Add("subitem2")
lvItem.SubItems.Add("subitem3")
lvItem.SubItems.Add("subitem4")
items.Add(lvItem)
' end process an item
' this adds all the items to the listview
listview.Items.AddRange(items.ToArray)
'' build data
Dim arrays As New List(Of String())
For i = 1 To 5
Dim data(5) As String
data(0) = "subitem1"
data(1) = "subitem2"
data(2) = "subitem3"
data(3) = "subitem4"
data(4) = "subitem5"
arrays.Add(data)
Next i
'' setup listview
ListView1.View = View.Details
ListView1.Columns.Add("mainitem", 35, HorizontalAlignment.Left)
ListView1.Columns.Add("sub1", 55, HorizontalAlignment.Left)
ListView1.Columns.Add("sub2", 55, HorizontalAlignment.Left)
ListView1.Columns.Add("sub3", 55, HorizontalAlignment.Left)
ListView1.Columns.Add("sub4", 55, HorizontalAlignment.Left)
ListView1.Columns.Add("sub5", 55, HorizontalAlignment.Left)
'' fill listview
For Each arr As String() In arrays
Dim lvItem As ListViewItem = ListView1.Items.Add("item")
For Each piece As String In arr
lvItem.SubItems.Add(piece)
Next
Next
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Integer = 0 To ListBox1.Items.Count
Dim work As ProcessItemDelegate = New ProcessItemDelegate(AddressOf ProcessItem)
work.BeginInvoke(i, New AsyncCallback(AddressOf ProcessItemComplete), work)
Next
End Sub
Private Delegate Sub ProcessItemDelegate(ByVal itemIndex)
Private Sub ProcessItem(ByVal itemIndex As Integer)
' do some item processing
AddItemToListview(itemIndex)
' could process subitems here too i think
End Sub
Private Sub ProcessItemComplete()
' do some stuff each time you finished an item from the listbox
End Sub
Private Delegate Sub AddItemToListviewDelegate(ByVal itemIndex)
Private Sub AddItemToListview(ByVal itemIndex As Integer)
If ListView1.InvokeRequired Then
ListView1.BeginInvoke(New AddItemToListviewDelegate(AddressOf AddItemToListview), itemIndex)
Else
Dim lvItem As New ListViewItem
' do som processing then add the first column to the listview
' i just copied the the item from the listbox straight to the listview item instead
lvItem.Text = ListBox1.Items(itemIndex)
lvItem.SubItems.Add("")
lvItem.SubItems.Add("")
lvItem.SubItems.Add("")
lvItem.SubItems.Add("")
' add item to listview
ListView1.Items.Add(lvItem)
' lvitem.index is the index of the last listview item that was added
ProcessSubItem1(lvItem.Index, itemIndex)
' ProcessSubItem2(lvItem.Index, itemIndex)
' ProcessSubItem3(lvItem.Index, itemIndex)
'ProcessSubItem4(lvItem.Index, itemIndex)
End If
End Sub
' need a sub / delegate for each sub item i think
Private Delegate Sub ProcessSubItem1Delegate(ByVal lvIndex, ByVal itemIndex)
Private Sub ProcessSubItem1(ByVal lvIndex As Integer, ByVal itemIndex As Integer)
If ListView1.InvokeRequired Then
ListView1.BeginInvoke(New ProcessSubItem1Delegate(AddressOf ProcessSubItem1), lvIndex, itemIndex)
Else
' process the listview sub item
' may need the itemindex to process listbox items thats why i passed it
ListView1.Items(lvIndex).SubItems(0).Text = "some processed value"
End If
End Sub
End Class
Dim i2 As Integer = 0
For Each item2 In ListBox1.Items
Dim lvItem As New ListViewItem
Dim lvitem2 As New ListViewItem.ListViewSubItem
lvItem.Text = ListBox1.Items(i2)
lvItem.SubItems.Add(ListBox2.Items(i2))
lvItem.SubItems.Add(ListBox3.Items(i2))
lvItem.SubItems.Add(ListBox3.Items(i2))
ListView1.Items.Add(lvItem)
i2 = i2 + 1
Next