• Welcome to Valhalla Legends Archive.
 

[vb.net] listview item/subitem forecolor

Started by Mystical, January 27, 2010, 05:11 PM

Previous topic - Next topic

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

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



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.






Edit3:



^= 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.


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


soo here i added:


           lvChannel.Columns.Clear()
           lvChannel.Columns.Add("Username").Width = 100
           lvChannel.Columns.Add("Ping").Width = 60


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



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.

MyndFyre

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."
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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..

rabbit

Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Mystical

#5
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.

MyndFyre

Quote from: Mystical on January 29, 2010, 04:30 AM
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..
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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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.

rabbit

I haven't used 4.0 yet, but in 3.5 I never had problems with coloring subitems....
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

MyndFyre

Quote from: Mystical on January 30, 2010, 02:52 PM
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.

It just seems like your code is incredibly overcomplicated.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mystical


Mystical

#11
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

listview.clear


this is what should have been used, my code now works properly.

listview.items.clear


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


finished work;



    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