Hack Forums
[.NET] [Snippet] ListView Extension Method - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [Snippet] ListView Extension Method (/showthread.php?tid=617683)



[Snippet] ListView Extension Method - ubbelol - Aug 17, 2010

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


* - GoldStar - Aug 17, 2010

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.


* - DarkSeven46 - Aug 17, 2010

Makes the listview look better. Nice share.


* - ubbelol - Aug 17, 2010

(Aug 17, 2010 12:18 PM)Miharbi-DoNo 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