Valhalla Legends Forums Archive | .NET Platform | [vb.net] listview item/subitem forecolor

AuthorMessageTime
MyStiCaL
now this is just an example code i wrote up to add items to a listview, so this sub works perfectly fine except for the item/subitem coloring.
i've done a google search and found that quite a bit of people have problems with this, i even set UseItemStyleForSubItems to false. still no luck..

if anyone has any ideas thanks,

listview is in details view, using visual studio 2010 beta 2; 4.0 framework
[code]
Public Delegate Sub DelegateChannelListAdd(ByVal UserName As String, ByVal Ping As Long)

   Public Sub AddTo_Channel(ByVal UserName As String, ByVal Ping As Long)
       Dim CallBack As New DelegateChannelListAdd(AddressOf AddTo_Channel)

       If lvChannel.InvokeRequired Then
           lvChannel.Invoke(CallBack, UserName, Ping)
       Else
           Dim Item As ListViewItem  = lvChannel.Items.Add(UserName)
           Item.UseItemStyleForSubItems = False
           Item.ForeColor = System.Drawing.Color.Green
           Dim SubItem As ListViewItem.ListViewSubItem = Item.SubItems.Add(Ping)
           SubItem.ForeColor = System.Drawing.Color.Red
       End If
   End Sub
[/code]


Edit:
found this funny same way i did it, and that's by the book, still no luck. this problem is only in details view, the color works in others but details is what i need.

Edit2:
Starting to think it isn't my code but possibly my compiler or an error that derives from it, as users on msdn can use my same code and say that it works for them, I can't quite understand it, I guess that's the purpose of beta. or maybe it's my os/theme destroying the colors and i wasted a week on this small portion for nothing lol. (:

just another tip, when i use details mode the columns will go dim like to a gray disabled looking state and well actually lose there text to when i add items, unless i create the columns during runtime then it'll work, but if i manually set the columns no text will show period.

[IMG]http://img39.imageshack.us/img39/4981/listviewissue.png[/img]




Edit3:

[IMG]http://htzrww.bay.livefilestore.com/y1pO6cwfkIkpAbBcYklTh41DATTxDTwQyb0FaRpsoLz-qQhvqvjTy8vyL9OtCzq280YzuW4mV1lFYonB5feQo3ZZorLCpgU3sUz/screenListview.bmp[/img]

^= normal form no new instance created. code works fine. :(

I've pin pointed it down just a bit, I've tested the code on a normal form, it works great! but if i initalize or say create a new instance of the form with those controls on it and try it, it doesn't work?! intresting it will do this both on winforms and usercontrols. anyways added a lil more info/input incase anyone might have a couple suggestions to try.

Edit4:
I figured out a way to make it work, but is not at all a good work around.

[code]
Public Delegate Sub DelegateChannelListAdd(ByVal UserName As String, ByVal Product As String, ByVal Ping As Long, ByVal Flags As Long)

   Public Sub AddTo_Channel(ByVal UserName As String, ByVal Product As String, ByVal Ping As Long, ByVal Flags As Long)
       Dim CallBack As New DelegateChannelListAdd(AddressOf AddTo_Channel)

       If lvChannel.InvokeRequired Then
           lvChannel.Invoke(CallBack, UserName, Product, Ping, Flags)
       Else
           lvChannel.View = View.Details
           lvChannel.Columns.Clear()
           lvChannel.Columns.Add("Username").Width = 100
           lvChannel.Columns.Add("Ping").Width = 60
           Dim Item As ListViewItem = lvChannel.Items.Add(UserName)
           Item.UseItemStyleForSubItems = False
           Item.ForeColor = Color.White
           Dim SubItem As ListViewItem.ListViewSubItem = Item.SubItems.Add(Ping)
           SubItem.ForeColor = GetPingColor(Ping)
       End If
   End Sub
[/code]

soo here i added:

[code]
           lvChannel.Columns.Clear()
           lvChannel.Columns.Add("Username").Width = 100
           lvChannel.Columns.Add("Ping").Width = 60
[/code]

which happends to make it work,  that is definity not a good work around.

[IMG]http://img145.imageshack.us/img145/2959/list1.png[/img]
January 27, 2010, 11:11 PM
MyStiCaL

last conclusion i have is that even though the listview can be invoked and works fine adding items, the colors will not come with it, as if i add the items and color them on the same thread the UI was created on it works, if it has to be invoked even though put back onto the same thread it does not want to work.

best explanation i can think of is this is a problem with the visual studio and not the code, how ever im sure there is still a better work around then mine posted.


hopefully this will be somewhat helpful for others ever coming across this problem. :P or atleast when a finished/fixed result is found.
January 29, 2010, 6:54 AM
Myndfyr
Overcomplicated.

Try this out: http://robpaveza.net/pub/LVSubItemColoring.zip

It can be done in C#, it can be done in VB.  Half the code I wrote for that was to create the sample set.

This was done in VStudio 2008.  If there's a problem in 2010, well, that's interesting, but it is labeled "beta."
January 29, 2010, 9:35 AM
MyStiCaL
I'll have a look at that, the interesting thing was if i create a loop to add items on the ui thread like form_load event, all the colors add perfectly, but on an incoming thread and invoking the colors don't work at all.

I'll have a look at your snipplet..
January 29, 2010, 10:30 AM
rabbit
Use .Refresh?
January 29, 2010, 1:39 PM
MyStiCaL
wish it was that easy, like vb6 was lol =P i'm just going to assume this is a bug with the beta maybe if i fully subclass the listview but that's just over complicating things for such a simple thing.


great example though myndfyre, that will possibly come in use, i think its just a problem with my incoming thread. it shouldn't matter that the thread comes from an external library.
January 29, 2010, 10:08 PM
Myndfyr
[quote author=Mystical link=topic=18151.msg184017#msg184017 date=1264761028]
I'll have a look at that, the interesting thing was if i create a loop to add items on the ui thread like form_load event, all the colors add perfectly, but on an incoming thread and invoking the colors don't work at all.

I'll have a look at your snipplet..
[/quote]
I didn't create a loop on the UI thread - note that it is on another thread and all the actions are marshaled back to the UI thread with BeginInvoke.
January 30, 2010, 12:06 AM
MyStiCaL
complicated on how i'd explain it, playing around more with it, as long as it doesn't have to be invoked the sub works perfectly fine, once invoke,  the columns are removed.. which i'd assume is the problem.
January 30, 2010, 8:52 PM
rabbit
I haven't used 4.0 yet, but in 3.5 I never had problems with coloring subitems....
January 30, 2010, 10:17 PM
Myndfyr
[quote author=Mystical link=topic=18151.msg184028#msg184028 date=1264884741]
complicated on how i'd explain it, playing around more with it, as long as it doesn't have to be invoked the sub works perfectly fine, once invoke,  the columns are removed.. which i'd assume is the problem.
[/quote]

It just seems like your code is incredibly overcomplicated.
January 31, 2010, 6:24 AM
MyStiCaL
it actually isn't, pretty simple..
January 31, 2010, 8:41 PM
MyStiCaL
Update Fix:

Mike@USEast i guess made the same mistake before so already knew the solution.


this caused the listview to clear everything including the columns
[code]
listview.clear
[/code]

this is what should have been used, my code now works properly.
[code]
listview.items.clear
[/code]

thanks myndfyre for your example may come in use when i decide to add a queue system to my channel list filters.


finished work;
[IMG]http://img526.imageshack.us/img526/8788/listviewil.png[/img]

[code]
    Public Delegate Sub DelegateChannelListAdd(ByVal ChannelList As ListView, ByVal UserName As String, ByVal Product As String, ByVal Ping As Long, ByVal Flags As Long)

    Public Sub AddTo_Channel(ByVal ChannelList As ListView, ByVal UserName As String, ByVal Product As String, ByVal Ping As Long, ByVal Flags As Long)

        Dim CallBack As New DelegateChannelListAdd(AddressOf AddTo_Channel)

        If ChannelList.InvokeRequired Then
            ChannelList.BeginInvoke(CallBack, ChannelList, UserName, Product, Ping, Flags)
        Else
            Dim Item As ListViewItem = ChannelList.Items.Add(UserName)
            Item.UseItemStyleForSubItems = False
            Dim SubItem As ListViewItem.ListViewSubItem = Item.SubItems.Add(Ping)
            SubItem.ForeColor = GetPingColor(Ping)
        End If

    End Sub
[/code]
February 5, 2010, 12:14 AM

Search