Click to See Complete Forum and Search --> : Minor but annoying problem with empty ListBox display.


mattst
October 3rd, 2009, 06:48 AM
I have a minor but annoying problem with the display of an empty ListBox.

During the course of an application a ListBox displays various to-do items which get consecutively removed programatically as the items are completed until, on completion, the ListBox is empty. The 'current item' is always the first/top item in the ListBox and that item is selected to get it highlighted so the user knows which is the currently active task.

On completion the ListBox is empty, however where the top item would be, if there was one, a dotted rectangle is displayed. See this screen capture:

http://www.zilefile.com/files/8746_vqpzd/Cap_01.jpg

How can I stop this dotted rectangle ('phantom selection') being shown?

I've tried several things like setting the SelectionMode to None, using ClearSelected(), both, and using Refresh(), disabling then re-enabling the control (Enabled = false/true with refreshing between them) - none of these remove the dotted selection.

I know this is a minor thing but it's now really annoying me - not so much the actual dotted selection, but my inability to get rid of it. Please help before things get out of hand and it causes me a nervous breakdown. :D

Thanks gurus.

Arjay
October 3rd, 2009, 12:52 PM
I know this is a minor thing but it's now really annoying me - not so much the actual dotted selection, but my inability to get rid of it. Please help before things get out of hand and it causes me a nervous breakdown. :DIt's the focus rectangle for the list box item. Unless you go to the extreme (like coding up an owner draw listbox) as far as I know, there isn't any way to get rid of it in Winforms.

This is settable in WPF. In WPF, you control the style of the focus rect (including removing it altogether).

mattst
October 3rd, 2009, 01:36 PM
Thanks for the info. Arjay.

You've given me a solution perhaps even without realizing it.

By hitting Tab after completion, the focus shifts to the next 'tab' item, in this case the 'Close' dialog button and the dotted focus rectangle in the ListBox vanishes. Perfect !!

Given that the most logical item to get the focus on completion is that Close button all I need do is to programmatically change the focus to the Close button after the final item is removed from the ListBox. No problem, or so I thought.

I added the line:

CloseButton.Focus();

at a point after the ListBox is empty but CloseButton does NOT receive the focus. If, while the program is running, I hit the Tab key after the ListBox is empty the focus jumps straight to the Close button - it just does not work programmatically.

I checked the value of CloseButton.CanFocus - immediately before setting Focus() - and it is true.

Any ideas why it's not working or how I should achieve this?

Thanks.