Listview Colors Changing when they lose focus...

W

wooster11

Hi All,
Hopefully you can help me out with this. I'm finding some really
odd behavior with a listview that I have on a form.
I have a list view that lists items in different colors based on the
status of that item (finished = black, error = red, etc...)
When my list view has multiple items of different colors, those that
are of the changed color (red) will switch to black when I click on an
empty row of the list view. For example, let's say I have one red item
and one black item, rows 1 and 2 respectively. I'm in the details view
(but it can be recreated using the list view also). When the list view
loads, everything is displayed properly with row 1 being red, and row 2
being black. If I click on an empty row (I don't allow editing), or
actually the empty part of the list view, the color of the first item
(currently red), changes to the default color, which is black or Window
Text. The funny thing is this. If I then click on the 2nd item (the
one that's supposed to be black), the first item turns back to it's red
color. I can't explain it. It only loses its color when the list view
is clicked where there are no items in it.

I've found a bug that's somewhat similar on MSDN
(http://support.microsoft.com/default.aspx?scid=kb;en-us;814734), but
it's not the same issue. It could definitely be related though.

Any help would be appreciated. If you want, I can send some screen
shots of the behavior. Just send me an email at (e-mail address removed)
and I will reply back with the images. Thanks.

Derek Woo
 
W

wooster11

I think I actually figured out this problem. I do believe it's a bug
in the .NET framework, but I found a way to get around it.

The issue really has to do with the differences between the item that
has focus and the item that is selected.

By default, the listview loads with the first item having focus, but no
items selected.
When an item has focus, it loses the custom colors or settings that you
assigned to it and uses the colors of the main listview. I consider
this to be a bug because it doesn't make sense to me why the colors
would change unless we tell it to do so. (Plus in the debugger, it
still believes the color is that of the custom color that I set). So
when I clicked on the empty rows, the first item (With a custom color)
ended up still maintaining focus. Because it was maintaining focus and
the listview had focus too, the custom color for the focused item was
lost. To handle this problem, I had to make use of the MouseDown
event, like so.

Private Sub ListView1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles lvwQueuedItems.MouseDown
If Not Me.ListView1.FocusedItem Is Nothing Then
Me.ListView1.FocusedItem.Focused = False
End If
End Sub

So what happens here is that if an item has focus, I make sure that it
no longer has focus (which would cause no items to have focus) whenever
the mouse is pressed down anywhere in the control. This may cause a
problem if you need to know which item has focus, but you can easily
solve this by checking if there's an item where the user pressed the
mouse button. You'll just need to use the GetItemAt function
[Me.ListView1.GetItemAt(e.x, e.y)] to check to see if an item was
clicked.

Note that you can't use the Click event because the click event
requires that an item is actually click in order for that event to be
fired. But the MouseDown and MouseUp events are captured no matter
where the control is clicked.

Another note: If you change the selection during runtime (which I do),
make sure that you change the focus too, otherwise you'll run into the
same problem because when doing it form the runtime, the selection only
changes and not the focus. Only when users click on a single item,
does that item also get focus when it's selected.

For example there may be an up or down button and the user clicks it to
move up and down through the list view.

ListViewItem.Selected = True
ListViewItem.Focused = True

I hope that this comes in handy for some of you as it was a real
headache for me today. I spent nearly the whole day figuring this out.

Derek Woo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top