Question

In a ListView control, the focus is on some Item within that control - that is, the control maintains its own internal notion of what is in focus, which can be retrieved using the FocusedItem property.

I would like no items to be focused. In other words, I want the FocusedItem property to set to null. Any idea how I might accomplish this?

Was it helpful?

Solution

To expand on Vanuan's answer:

if (listView1.FocusedItem != null)
{
    listView1.FocusedItem.Focused = false;
}

Something tells me that you also want to un-select the item. So, you probably want to do this as well:

if (listView1.SelectedItems.Count != 0)
{
    listView1.SelectedItems[0].Selected = false;
}

OTHER TIPS

I think, it is

listView1.FocusedItem.Focused=false;

Make sure that listView1.FocusedItem is not null.

(Thanks to brianpeiris for expanding)

When new items are added to a list view control, it draws dotted box around the items. There are some situations where you don't want this visual cue. Simply having the selected item(s) hightlighted is sufficient. Here is one way to prevent dotted box from showing up when adding new items to a list view control.

ListViewItem item = new ListViewItem();
myListView.Items.Add(item);
myListView.SelectedItems.Clear();
myListView.FocusedItem = null;
myListView.Refresh();

UPDATE:
My solution does not fix the problem. I wish there was a simple way to "unfocus" a control but after googling for several hours I couldn't find any. I too was looking for a way to prevent those dotted lines from showing up. When I click a button to add list view items there's no problem. It's only when I press enter after tabbing into a button (receiving input focus) does this show up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top