CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2002
    Posts
    278

    Add items to ListView?

    I have the code below which puts two column headers at the top of my ListView control, (Title) and (Description). . .

    Code:
    Private Sub Form_Load()
        
        Me.ListView1.View = lvwReport
        
        Me.ListView1.ColumnHeaders.Clear
        Me.ListView1.ColumnHeaders.Add , , "Title", ListView1.Width / 2
        Me.ListView1.ColumnHeaders.Add , , "Description", ListView1.Width / 2
    End Sub
    Can anyone show me the code I can add to this that will add one item in the list view control with the text (Item Title) under the ‘Title’ column and (Item Description) under the ‘Description’ column?


    .

  2. #2
    Join Date
    Oct 2007
    Posts
    29

    Re: Add items to ListView?

    Try this:

    Code:
    Private Sub Form_Load()
        
        Me.ListView1.View = lvwReport
        
        Me.ListView1.ColumnHeaders.Clear
        Me.ListView1.ColumnHeaders.Add , , "Title", ListView1.Width / 2
        Me.ListView1.ColumnHeaders.Add , , "Description", ListView1.Width / 2
    
        With Me.ListView1.ListItems
            .Add , , "Item Title 1"
            .Add , , "Item Title 2"
        End With
    
        Me.ListView1.ListItems(1).SubItems(1) = "Item Description 1"
        Me.ListView1.ListItems(2).SubItems(1) = "Item Description 2"
    
    End Sub

  3. #3
    Join Date
    Nov 2002
    Posts
    278

    Re: Add items to ListView?

    Dam I feel dumb. That was easy. How about adding icons to the items in the list? I have a handful of icons (.ICO files) I need to programmatically add (from a network share or hard drive) to the items in the list. Is that pretty easy to do too?
    Last edited by DinoVaught; October 17th, 2007 at 04:46 PM.

  4. #4
    Join Date
    Oct 2007
    Posts
    29

    Re: Add items to ListView?

    Icons are very easy to add too. First you need to add an ImageList control to your form, for this example we'll call it imlSmallIcons. Then add all the small icons you want to use. Next, view the property sheet, and click on the ImageLists tab. Click the dropdown next to Small and select imlSmallIcons.

    Now, when you add a new item to the ListView control, you can specify an index, or its key. The following code adds a new item, with its small image set to the first image in the imlSmallIcons Image List:

    Code:
    ListView1.ListItems.Add , , "Test Image Item", , 1
    Note that to see the small icons, the View property needs to be set to lvwSmallIcon

    To add icons to column headers, simply select the image list you want to use in the ColumnHeaders drop down, and add the Image Index in the Report Property:

    Code:
    ListView1.ColumnHeaders.Add , , "Name", , , 1

  5. #5
    Join Date
    Oct 2007
    Posts
    1

    Re: Add items to ListView?

    This may be another stupid question, however when I am testing the code above for my application - I get all types of errors. Specifically that the columnheaders property or the listitems property is not part of the listview object.

    I have just pulled the ListView control from toolbox in VB 2005 Express Edition - do I have an incorrect reference for this object.

    Where have I gone wrong? Thanks.

  6. #6
    Join Date
    Oct 2007
    Posts
    29

    Re: Add items to ListView?

    The ListView control is different in VB.Net so there are different methods resulting in different syntax.
    John

    If this was helpful, then please Rate This Post!

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