views:

747

answers:

3

When the program runs, there is a series of ListView forms. We populated one of them with items (as strings) and we check whether the state of selection has changed. Once it's changed, we grab the text of the selected item using FocusedItem.Text. The first time works just fine but when another selection is made, the selected item returns as null.

The only way we can temporarily get around this issue is to clear and repopulate the form. The disadvantage is that we lose the highlighted item. There got to be another way around this. Maybe we're not clear on how ListView really works?

Any ideas?

A: 

I have run a test program and see no instance where FocusedItem could be nulls.

Could you post some code?

Here is what I got after I selects the item 'a' then selects the item 'b' and then click on an empty space:

ItemSelectionChanged
    Item         : ListViewItem: {a}
    IsSelected   : True
    SelectedItem : ListViewItem: {a}
    FocusedItem  : ListViewItem: {a}
SelectedIndexChanged
    SelectedItem : ListViewItem: {a}
    FocusedItem  : ListViewItem: {a}
ItemSelectionChanged
    Item         : ListViewItem: {a}
    IsSelected   : False
    SelectedItem : null
    FocusedItem  : ListViewItem: {a}
SelectedIndexChanged
    SelectedItem : null
    FocusedItem  : ListViewItem: {a}
ItemSelectionChanged
    Item         : ListViewItem: {b}
    IsSelected   : True
    SelectedItem : ListViewItem: {b}
    FocusedItem  : ListViewItem: {b}
SelectedIndexChanged
    SelectedItem : ListViewItem: {b}
    FocusedItem  : ListViewItem: {b}
ItemSelectionChanged
    Item         : ListViewItem: {b}
    IsSelected   : False
    SelectedItem : null
    FocusedItem  : ListViewItem: {b}
SelectedIndexChanged
    SelectedItem : null
    FocusedItem  : ListViewItem: {b}

Noticed that FocusedItem is never null.

Is it the same in your case?

Would a simple if (listView.FocusedItem == null) check do for your case?

chakrit
+1  A: 

If you're after the item the user has selected, you should be using the first item in the SelectedItems collection. FocusedItem is the item that currently has the keyboard focus (and hence is showing the dotted focus rectangle); if the control doesn't have the focus, neither can an item.

Sunlight
A: 

Hi, I had the same problem while using listview. There seems to be some problem with respect to the event fired upon selection. 1: SelectedIndexChanged event where the focussed item,selected items and selected indies become null. this is the reason for the above problem. 2: ItemActivate event can be alternatively used without any glitches where the focussed item,selected items or selected indices are not null in the second or any other time.

while creating a listview with details, SelectedIndexChanged event is fired by default. so a change in the respective Designer class and a related event handler in the main class would do the job. Things to do. in the designer class, see the event that is subscribed example this.TaskslistView.SelectedIndexChanged +=new System.EventHandler(TaskslistView_SelectedIndexChanged); for which the respective TaskslistView_SelectedIndexChanged event handler method is present in the main class. replace this event with this.TaskslistView.ItemActivate += new System.EventHandler(this.TaskslistView_ItemActivate); and replace the respective TaskslistView_SelectedIndexChanged with TaskslistView_ItemActivate. This ought to solve the problem Hope this helps, Best Regards, mukunda

mukunda