Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, June 21, 2009 6:53 PM
In WinForms (Vb.Net / Framework 3.5), I've created a BindableListView control that has a DataSource property that accepts IDataReader's, DataTable's, Array's, Generic Lists and Single objects. I also added the ability to alternate colors when the data is bound to the ListView (I put in sorting also by default with support for numeric & date time).
What I have not been able to figure out yet is how to change the selected item's color from the System color (without changing the system color which I don't want to do). I've used the ItemSelectionChanged and SelectedIndexChanged to change the BackColor of the ListViewItem but the selected color always overrides the BackColor (HideSelection doesn't work because it only does that when the control loses focus).
My question, Is there a way to change the selected items color (without drawing it myself)? I searched for a while but couldn't find a good example of this in any language.
All replies (3)
Tuesday, June 23, 2009 4:25 AM âś…Answered
Hi bpell,
You can set the listview's ownerdraw property to true. Then add the code as below:
Private Sub listView1_DrawItem(ByVal sender As Object, _
ByVal e As DrawListViewItemEventArgs) _
Handles ListView1.DrawItem
If Not (e.State And ListViewItemStates.Selected) = 0 Then
' Draw the background for a selected item.
e.Graphics.FillRectangle(Brushes.Azure, e.Bounds)
e.DrawFocusRectangle()
End If
' Draw the item text for views other than the Details view.
If Not Me.listView1.View = View.Details Then
e.DrawText()
End If
End Sub
If you want to draw more items, you can refer to this article: http://msdn.microsoft.com/en-us/vstudio/system.windows.forms.listview.ownerdraw(VS.85).aspx
Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.
Best Regards
Yichun Feng
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Sunday, June 21, 2009 7:37 PM
try this
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
For Each listviewItem As ListViewItem In ListView1.SelectedItems
Dim index As Integer = ListView1.Items.IndexOf(listviewItem)
ListView1.Items(index).BackColor = Color.Red
ListView1.Items(index).ForeColor = Color.Purple
Next
End Sub
kaymaf
I hope this helps, if that is what you want, just mark it as answer so that we can move on
Wednesday, June 24, 2009 12:40 AM
Thanks for the responses, it's much appreciated. Yichun, that's exacly what I was looking for, for some reason I was thinking drawing it myself was going to be more of a problem, that's very straight forward. Thanks again. :)