Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[Snippet] ListView Extension Method

So I read miharbis thread about making extensions for types (https://hackforums.net/showthread.php?tid=606044), and I couldn't find anyone releasing an extension (which I find very weird) so I thought, why not make one :p

This is a very simple extension, I had a mate on MSN today who asked for this just thing though.

Hopefully this will inspire more people to release extensions 😉

Alright so here's a little preview of what it does:

[IMG]http://i350.photobucket.com/albums/q408/ubbe12/backclr.gif[/IMG] (Color looks much better in reality)

Here's the code (2 alternatives, they both serve the same function)

Don't forget to import "System.Runtime.CompilerServices"!

Code
<Extension()> Public Sub colorLv(ByVal lV As System.Windows.Forms.ListView, _
  Optional ByVal backClr As Color = Nothing, _
  Optional ByVal modVal As Integer = 2)

  If Not lV.View = 1 Then Exit Sub
  If backClr = Nothing Then backClr = Color.WhiteSmoke
  For i = 0 To lV.Items.Count - 1
    If (i Mod modVal = 0) Then lV.Items(i).BackColor = backClr
  Next

End Sub

Code
<Extension()> Public Sub colorLv(ByVal lV As System.Windows.Forms.ListView, _
  Optional ByVal backClr As Color = Nothing, _
  Optional ByVal modVal As Integer = 2)

  If Not lV.View = 1 Then Exit Sub
  If backClr = Nothing Then backClr = Color.WhiteSmoke
  For i = 0 To lV.Items.Count - 1 Step modVal
    lV.Items(i).BackColor = backClr
  Next

    End Sub

Now you can use this method accordingly:

Code
listview1.colorLv(System.Color, Integer)

So say you want to color each 4th item black, you would do it like this:

Code
listview1.colorLv(Color.Black, 4)

If you want it to color each 2nd item with the color whitesmoke (in the example above), just leave it blank

Code
listview1.colorLv()

Have fun and please post your own extensions. 😁
Probably coding some useless shit.
that's kinda coool mate. makes the listview looks good. you know what you can add? you can scan the backcolor and check if it's too dark, if it is change the forecolor to white. hehe nice.
Makes the listview look better. Nice share.
(Aug 17, 2010 12:18 PM)GoldStar Wrote:  that's kinda coool mate. makes the listview looks good. you know what you can add? you can scan the backcolor and check if it's too dark, if it is change the forecolor to white. hehe nice.

Mhm yeah that's a nice idea, i'll make it tomorrow :p
Probably coding some useless shit.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by еlixirSep 9, 2022 06:31 AM 0 132 Sep 9, 2022 06:31 AM
Last Post: еlixir
[Python] by datguy.devNov 28, 2019 10:37 PM 4 299 Nov 28, 2019 10:37 PM
Last Post: Eternal Darkness
[C#] by gigajewDec 1, 2018 05:55 PM 3 250 Dec 1, 2018 05:55 PM
Last Post: gigajew