CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2008
    Posts
    10

    Help with adding items to ListView

    Maybe someone here can shed some light.

    I created a Windows Mobile application with a ListView control. I want to add items to this ListView.

    Code:
    Public Class Form1
    
        Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
            Me.Close()
        End Sub
    
        Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
            Dim i As Short
            For i = 1 To 5
                Dim listViewItem As New ListViewItem("ItemSuccess" + i.ToString)
                listViewItem.SubItems.Add("SubItemSuccess" + i.ToString)
                ListView1.Items.Add(listViewItem)
                'listViewItem = Nothing
            Next
        End Sub
    
        Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
            Dim i As Short
            Dim listViewItem As New ListViewItem()
            listViewItem.SubItems.Add("")
            For i = 1 To 5
                listViewItem.SubItems.Item(0).Text = "ItemFail" + i.ToString
                listViewItem.SubItems.Item(1).Text = "SubItemFail" + i.ToString
                ListView1.Items.Add(listViewItem)
            Next
        End Sub
    End Class
    While sub Menu2 succeeds, every run a new ListViewItem instance is created. What if the counter doesn't run to 5, but to say, 50,000?
    Every new instance of the ListViewItem class generates a new instance in memory, not to mention the amount of time it will take. Even if I set the instance of ListViewItem to Nothing at the end of each run, I still lose precious cpu cycles.

    So, I wrote sub Menu3. Here I create the instance outside of the loop and reuse it every time. Would be great if it worked, but it doesn't. Actually on the second run (i.e. i=2), ListView1.Items.Add(listViewItem) throws an exception, "Value does not fall within the expected range.". During debugging I found out, that the property ListView1.Index is set to "1", whereas during the first run it is set to "-1".

    Does anyone have a solution to make the sub Menu3 work?

  2. #2
    Join Date
    Sep 2009
    Location
    Copenhagen, Denmark
    Posts
    2

    Re: Help with adding items to ListView

    I'm not quite sure what your trying to do but i would do

    Code:
            For i = 1 To 5
                listViewItem.SubItems.Item(i).SubItem(0).Text = "ItemFail" + i.ToString
                listViewItem.SubItems.Item(i).SubItem(1).Text = "SubItemFail" + i.ToString
                ListView1.Items.Add(listViewItem)
            Next

  3. #3
    Join Date
    Sep 2009
    Location
    Copenhagen, Denmark
    Posts
    2

    Re: Help with adding items to ListView

    For not doing to much memory as you stated i would use Items.Add and SubItems.Add and use 0 to 4 instead of 1 to 5 so you can actually use the i variable

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured