listboxes and or arrays to listview vb.net

specopkirbs

BANNED
Joined
Nov 28, 2008
Messages
920
Reaction score
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.
 
would a datagrid be a better option perhaps? been on this all day and still cant find the solution to add multiple arrays from different threads into a listview
 
I need a little more information on what you're trying to accomplish. It would help if you showed the data you are trying to place inside the listview, and the desired visual outcome you are looking for.

A Datagrid would be better if you are trying to bind a custom class object. Here is an example:

Code:
    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
Where dg is the DataGridView control I added to the form.

One thing to note is that you must use public properties in order to get this type of binding result, where the grid headers are the name of the property.
 
I use a collection of ListViewItem like below

Code is off the top of my head so may not be exactly right but it's close.

Code:
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)
 
Last edited:
I'm more interested in what the data looks like that you are going to put into those subitems. You are trying to take multiple arrays of subitem1,subitem2,etc and you need an algorithm to build a listview off that?

Code:
        '' 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
 
basically what i have is a listbox that is populated indipendantly from scraping google, i then have 3 other listboxes that are populated by pulling further google queries based on each item in listbox1.
so each listbox has the same number of entries and what i need is this:
the items in listbox1 to populate the first column of the listview or datagridview
the items in listbox2 to populate the second comumn of the listview or dg
etc etc
each does its relevant task on a seperate thread and then populates the relevant listbox.
so effectivly what i need to do as mentioned above is populate the listview or datagrid columns from either the listbox or an array generated from the listbox.

you with me?
 
Ok I think I got you. Each listbox has the same number of rows, but it is not a static number of rows, correct? I would use my data grid code above, and build yourself a custom object either based from your listboxes, or instead of them. Once you set the object to the data grids binding source, you can just tell it to .Bind().

Another way perhaps is to keep an index of the last added item to the list boxes. this way after you add your items to each listbox, you can add a new row to the listview and the subitems as listbox1.items(index), listbox2.items(index) etc
 
Here is some code something like i might use.

The form contains 4 listboxes, a listview control, and a button to start the processing.

I just did enough to show a listview item getting added and updating its sub item. need more code for each sub item.

I am not an expert programmer.
I am sure the code can be written a number of ways besides this.


Code:
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
 
thanks guys i will look into it this afternoon cheers for the advice and code, ill let you know how i get on
 
Code:
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

this is what i ended up using, thanks guys. The number of items in listbox2,3 and 4 is relative to listbox1 so it works a treat
 
Hi specopkirbs, You have provide nice programming which i am searching for a little while. Thanks to you.
 
Back
Top