Results 1 to 8 of 8

Thread: [RESOLVED] VB2010 Listview: change backcolors

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    new zealand (kiwi)!
    Posts
    202

    Resolved [RESOLVED] VB2010 Listview: change backcolors

    How can I access a Listview's subitem, (to change backcolor, text ,etc)
    I know the item & subitem index's...

    something like this ??
    ListView1.listitem(1).SubItems(2).BackColor = Color.Black

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: VB2010 Listview: change backcolors

    What you have posted in exactly the code you would use. You have to set the item's UseItemStyleForSubItems property to False first though, or setting subitem properties like that has no effect.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: VB2010 Listview: change backcolors

    your code is right, but you need to set the listviewitem's .UseItemStyleForSubItems to False first for it to work

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    new zealand (kiwi)!
    Posts
    202

    Re: VB2010 Listview: change backcolors

    this doesn't work:
    ListView1.listitem(1).SubItems(2).BackColor = Color.Black

    the IDE complains about the .listitem(1)

    Error 2 'listitem' is not a member of 'System.Windows.Forms.ListView'.

    ALSO:
    what else disables the .backcolor of a subitem?
    I had it working, now no color shows after altering some the Listviews properties.
    I can't seem to get it back to what worked!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: VB2010 Listview: change backcolors

    Ah, didn't notice that. It should be Items. Of course, you could always have opened the MSDN documentation and had a look for yourself.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    new zealand (kiwi)!
    Posts
    202

    Re: VB2010 Listview: change backcolors

    Still can't get this to work!
    I get an 'invalid index' on the last line at .items(1) when I run this
    Code:
            Dim item2 As New ListViewItem("item1", 0)
            item2.SubItems.Add("pink")
            item2.SubItems.Add("blue")
            item2.SubItems.Add("biege")
            item2.UseItemStyleForSubItems = False
            item2.SubItems(1).BackColor = Color.HotPink
            item2.SubItems(2).BackColor = Color.Blue
            item2.SubItems(3).BackColor = Color.Green
            ListView1.Items.Add(item2)
    
            ListView1.Items(1).SubItems(1).BackColor = Color.Black

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: VB2010 Listview: change backcolors

    Just like all .NET collections, the ListView.Items collection is zero-based. The index of the first item is 0.

    Perhaps the fact that you use 1 as the index for the first subitem is confusing you, but SubItems is zero-based also. SubItems(0) gives you the item itself as a subitem. This means that myListViewItem.Text and myListViewItem.SubItems(0).Text will give you the same text.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    new zealand (kiwi)!
    Posts
    202

    Re: VB2010 Listview: change backcolors

    Thanks, that fixed it.
    I had read about 0 based, but didnt realise it applied to listview index....


    PS re my other post, the .GetItemAt seems to work to identify the on screen list index's...

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