Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change full row select back colour or list view
#1
Hi Team Of Programmers,
I need your help of a problem that is frustrating me I have a list view box and I want to change to full row select back colour I have already achieved this for list box but the settings are different this is what I am wanting to do for list view any help will be gratefully taken on board

[Image: untitled.jpg]

Code

Code:
Private Sub listBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        Dim clrSelectedText As Color = Color.Black 'Our color for selected(text)
        Dim clrHighlight As Color = Color.LightGreen 'Our background for selected items
        If e.State = DrawItemState.Selected Then
            'This item is selected
            'e.DrawBackground()
            e.Graphics.FillRectangle(New SolidBrush(clrHighlight),
            e.Bounds) 'Fill the item's rectangle with our highlight
            e.Graphics.DrawString(ListBox1.Items.Item(e.Index),
            e.Font, New SolidBrush(clrSelectedText), e.Bounds) 'Draw the text for the item
        ElseIf e.State = DrawItemState.None Then
            'This item has no state
            e.DrawBackground() 'Draw our regular background
            e.Graphics.DrawString(ListBox1.Items.Item(e.Index),
            e.Font, Brushes.Black, e.Bounds) 'Draw the item text in its regular(color)
        End If
        'Draws a focus rectangle around the item if it has focus
        e.DrawFocusRectangle()
    End Sub


Kind Regards

Bradley
#2
Hm, well, looking at your existing code.. I don't really know what you're doing. Sorry about that.

So, I wrote my own method which should do what you intended. I don't know if it's what you wanted, but you can take a look at it.

Code:
'This method colors all of the list view items according to the following data
    'Selected items: backcolor = light green
    'Unselected items: backcolor = white
    '@param: the listview to be searched and colored
    Public Sub RecolorListView(ByVal lstView As ListView)
        'Scroll through each listview item
        For Each item In lstView.Items

            If lstView.SelectedItems.Contains(item) Then 'The item is selected
                CType(item, ListViewItem).BackColor = Color.LightGreen 'Color it green!
            Else 'The item is not selected
                CType(item, ListViewItem).BackColor = Color.White 'Color it white
            End If

        Next
    End Sub

Pastebin version: <!-- m --><a class="postlink" href="http://pastebin.com/jhmyty3t">http://pastebin.com/jhmyty3t</a><!-- m -->

Just call this method every time the SelectedIndex is changed on the ListView control.
My Blog | My Setup | My Videos | Have a wonderful day.
#3
brandonio21,
Once again many thanks for you code and efforts and it is much appreciated this was not originally what I was after but I used to my advantage and adapted my idea thanks for the code I have added this to the project and the list of codes to my collection for future reference

Kind Regards

Bradley
#4
Fantastic! I am glad that I could help!
My Blog | My Setup | My Videos | Have a wonderful day.


Possibly Related Threads…
Thread Author Replies Views Last Post
  View 3D Models in VB .NET Worf 2 13,703 10-14-2014, 04:47 AM
Last Post: Worf
  NullReferenceException when adding item to generic list brco900033 1 9,908 06-17-2014, 12:16 AM
Last Post: brandonio21
  Full screen game overlay? Ecnarf 1 8,601 01-29-2013, 09:14 AM
Last Post: xolara

Forum Jump:


Users browsing this thread: 1 Guest(s)