dcsimg
Results 1 to 11 of 11
  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Resolved [RESOLVED] ListView Alternate itunes Row Color style

    Hello i have a question im new to vb.net how can you have your listview have auto alternate colors on load?


    like this below thanks

    one white
    one light grey


    Last edited by sinner0636; Dec 19th, 2017 at 11:34 AM.

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: ListView Alternate itunes Row Color style

    For some reason, this isn't native even in WPF.

    In Windows Forms to pull it off, it looks like there's a handy shortcut trick to the longer version I feared. Let's explain the longer version, then talk about the shorter.

    The ListView has an OwnerDraw property that is normally set to False. If it is set to True, it changes how it draws itself. When that property is true, when the ListView paints itself, it will raise its DrawItem event for each item it redraws. That makes you responsible for drawing the items yourself, which can be fairly complex. Part of drawing the item can be deciding "I'll let even-numbered rows be one color and odd-numbered rows be another."

    But it looks like you can talk the control into doing a lot of the drawing itself.

    The DrawItem event takes a DrawListViewItemEventArgs parameter. It has a handful of properties that the ListView checks after your event handler finishes and can tell it to do some other things. If, in DrawItem, you set the parameter's DrawDefault property to True, the ListView assumes you didn't draw anything and pretends like OwnerDraw is False for this item. Convenient!

    Each ListViewItem has a BackColor property. Normally you change that so the ListView knows what color to draw if you want to customize one. If you OwnerDraw then you get to decide if you care. But if you OwnerDraw THEN set DrawDefault to True, you're back to the ListView looking at it again.

    Each ListViewItem also has a UseItemStyleForSubItems property. If it's False, then the ListView only uses properties like BackColor to draw the first column. That sounds weird but makes sense in the greater context of ListView.

    So, if you set the control to OwnerDraw, but make sure to set DrawDefault to True for every item, you get to do something for each item but still leave the details of drawing up to the ListView. That means you can set the BackColor for all even-numbered or odd-numbered items and get alternating colors!

    The VB version of the linked C# code looks like:
    Code:
    Private Sub ListView1_DrawItem(sender As Object, e As DrawListViewItemEventArgs)
        ' "Hey, draw this item for me."
        e.DrawDefault = True
    
        ' Do something for the odd-numbered items.
        If e.ItemIndex % 2 = 1 Then
            ' "Something" means "Change the background color and please use it for all of the columns."
            e.Item.BackColor = Color.Grey
            e.Item.UseItemStyleForSubItems = True
        End If
    End Sub
    You could also loop through the Items and set every other item's properties, but that's not quite as automatic as letting OwnerDraw do the work for you.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,527

    Re: ListView Alternate itunes Row Color style

    After loading the ListView call the method SetAlternateColor, try this
    vb.net Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Public Class Form1
    5.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    6.         ListView1.FullRowSelect = True
    7.         ListView1.View = View.Details
    8.         ListView1.Columns.Add(New ColumnHeader With {.Text = "Col 1", .Width = 100})
    9.         ListView1.Columns.Add(New ColumnHeader With {.Text = "Col 2", .Width = 400})
    10.         For j = 1 To 20
    11.             ListView1.Items.Add(New ListViewItem({"A Row", "A Row"}))
    12.         Next
    13.         SetAlternateColor(ListView1, Color.FromArgb(178, 214, 245), Color.FromArgb(219, 237, 252))
    14.     End Sub
    15.  
    16.     Private Sub SetAlternateColor(lv As ListView, c1 As Color, c2 As Color)
    17.         For j = 0 To lv.Items.Count - 1
    18.             If (j Mod 2) = 0 Then
    19.                 lv.Items(j).BackColor = c1
    20.             Else
    21.                 lv.Items(j).BackColor = c2
    22.             End If
    23.         Next
    24.     End Sub
    25.  
    26. End Class



  4. #4

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: ListView Alternate itunes Row Color style

    I tried the code above i got it working the way i want but i want it auto fill the alternate row colors without having to add items automatically load alternate row colors on app load without adding items is this possible?

    this code adds the alternate row colors for items added

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Adding ListView Columns
            ListView1.View = System.Windows.Forms.View.Details
            ListView1.CheckBoxes = True
            ListView1.FullRowSelect = True
            ListView1.Columns.Add("Name", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("size", 100, HorizontalAlignment.Left)
            ListView1.Columns.Add("path", 3000, HorizontalAlignment.Left)
    
            Dim arr As String() = New String(3) {}
            Dim itm As ListViewItem
            'add items to ListView
            arr(0) = "test1"
            arr(1) = "test2"
            arr(2) = "test3"
            itm = New ListViewItem(arr)
            Me.ListView1.Items.Add(itm)
            Me.ListView1.Items(Me.ListView1.CheckedItems.Count).Checked = True
    
            'add items to ListView
            arr(0) = "test4"
            arr(1) = "test5"
            arr(2) = "test6"
            itm = New ListViewItem(arr)
            Me.ListView1.Items.Add(itm)
            Me.ListView1.Items(Me.ListView1.CheckedItems.Count).Checked = True
    
            'add items to ListView
            arr(0) = "test7"
            arr(1) = "test8"
            arr(2) = "test9"
            itm = New ListViewItem(arr)
            Me.ListView1.Items.Add(itm)
            Me.ListView1.Items(Me.ListView1.CheckedItems.Count).Checked = True
    
            'add items to ListView
            arr(0) = "test10"
            arr(1) = "test11"
            arr(2) = "test12"
            itm = New ListViewItem(arr)
            Me.ListView1.Items.Add(itm)
            Me.ListView1.Items(Me.ListView1.CheckedItems.Count).Checked = True
    
    
            For i As Integer = 0 To ListView1.Items.Count - 1 Step 1
                If i Mod 2 = 0 Then
                    ListView1.Items(i).BackColor = Color.White
                Else
                    ListView1.Items(i).BackColor = Color.GhostWhite 'light grey
                End If
            Next i
        End Sub

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,527

    Re: ListView Alternate itunes Row Color style

    I don't understand, if the ListView is empty, how can we set alternate colors?



  6. #6

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: ListView Alternate itunes Row Color style

    I coded in RealBasic for years i used to be able to do this with my apps i coded a alarm clock few years ago that did this!

    this is a app i coded
    https://sourceforge.net/projects/pcmicroalarm/

    Name:  alarm clock.jpg
Views: 226
Size:  24.7 KB

  7. #7
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,527

    Re: ListView Alternate itunes Row Color style

    I see, AFAIK it isn't possible for an empty ListView. Maybe API call or OwnerDraw listview can do.
    Last edited by 4x2y; Dec 19th, 2017 at 09:22 PM.



  8. #8
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: ListView Alternate itunes Row Color style

    I'm not even sure it's possible for an OwnerDraw ListView, because OwnerDraw draws items. Odds are whatever environment it was used a more specialized control with the behavior of a ListView. Heck, it might even be a background image they generate? Either way, it's not baked into WinForms and I can't find many examples of it except for "with items". I'll try looking more but I think the answer is "find a custom ListView".

    I guess you could fudge it with some dummy items but then you have to deal with managing selection logic for them.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  9. #9
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,083

    Re: ListView Alternate itunes Row Color style

    Hi,

    just add some Text with the same ForeColor as the rows. Or add a Dummy Column and Set the Widht to 0

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  10. #10
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,527

    Re: ListView Alternate itunes Row Color style

    It is possible to do by auto generating a background image contains required colors

    vb.net Code:
    1. Private Sub SetListViewAlternateColors(lv As ListView, color1 As Color, color2 As Color)
    2.  
    3.         Dim bmp As Bitmap
    4.         If lv.Items.Count = 0 Then
    5.             bmp = New Bitmap(1, lv.Font.Height * 2) 'estimate Height
    6.         Else
    7.             bmp = New Bitmap(1, lv.GetItemRect(0).Height * 2)
    8.         End If
    9.  
    10.         Using gfx As Graphics = Graphics.FromImage(bmp)
    11.             If lv.HeaderStyle = ColumnHeaderStyle.None Then
    12.                 gfx.FillRectangle(New SolidBrush(color2), New Rectangle(New Point(0, 0), bmp.Size))
    13.                 gfx.FillRectangle(New SolidBrush(color1), New Rectangle(New Point(0, 0), New Size(bmp.Width, CInt(bmp.Height / 2))))
    14.             Else
    15.                 gfx.FillRectangle(New SolidBrush(color1), New Rectangle(New Point(0, 0), bmp.Size))
    16.                 gfx.FillRectangle(New SolidBrush(color2), New Rectangle(New Point(0, 0), New Size(bmp.Width, CInt(bmp.Height / 2))))
    17.             End If
    18.         End Using
    19.  
    20.         lv.BackgroundImage = bmp
    21.         lv.BackgroundImageTiled = True
    22.  
    23.     End Sub

    Note:
    * SetListViewAlternateColors should be called each time something cause the ListView item height change, e.g. Font change, icon size change, etc...

    * It is look perfect if the Header is hidden or if the header's Height equals Item's Height (when using large font size)



  11. #11

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: ListView Alternate itunes Row Color style

    thanks 4x2y thats what i was looking for your awesome !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width