views:

306

answers:

3

Hi,

I'm currently working on a listview in winform c# and everytime I click on an empty space on the listview, the selected item is lost.

please help ed

+2  A: 

I thought there was a property that prevented this from happening, but now I can't find it.

You could try this:

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListView listView = sender as ListView;
    if (listView.SelectedItems.Count == 0)
        foreach (object item in e.RemovedItems)
            listView.SelectedItems.Add(item);
}
Jeffrey L Whitledge
This will only work in WPF.
Grammarian
@Grammarian - The question didn't specify whether it was WPF, WinForms, ASP, Silverlight or anything else. I just answered with the one that I happened to have open in the IDE at the moment. You should post an answer with your favorite C# display technology! :-)
Jeffrey L Whitledge
indeed your right. it's in winforms
EdgarGad
@Jeffery It wasn't a criticism. Just making it clear for a newbie why your correct answer may not work for him.
Grammarian
A: 

This is much harder to do in WinForms than in WPF. WinForms has a SelectedIndexChanged event which doesn't tell you anything about what was already selected, plus it is fired every time a row is selected or deselected.

So if a row is selected and you select a different row, you receive two SelectedIndexChanged events:

  1. one after the selected row is deselected
  2. another when the new row is selected.

The problem is that, during event #1, the ListView has nothing selected and you don't know if event #2 is coming that will select the second row.

The best you can do is wait until your application is idle (a few milliseconds after the selection has changed), and if the listview still has nothing selected, put back the last selected row.

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    ListView lv = (ListView)sender;
    if (lv.SelectedIndices.Count == 0)
    {
        if (!this.appIdleEventScheduled)
        {
            this.appIdleEventScheduled = true;
            this.listViewToMunge = lv;
            Application.Idle += new EventHandler(Application_Idle);
        }
    }
    else
        this.lastSelectedIndex = lv.SelectedIndices[0];
}

void Application_Idle(object sender, EventArgs e)
{
    Application.Idle -= new EventHandler(Application_Idle);
    this.appIdleEventScheduled = false;
    if (listViewToMunge.SelectedIndices.Count == 0) 
        listViewToMunge.SelectedIndices.Add(this.lastSelectedIndex);
}

private bool appIdleEventScheduled = false;
private int lastSelectedIndex = -1;
private ListView listViewToMunge;
Grammarian
A: 

I accomplished it like this:

private void lvReads_MouseUp(object sender, MouseEventArgs e)
    {
        if (lvReads.SelectedItems.Count == 0)
            if (lvReads.Items.Count > 0)
                lvReads.Items.Find(currentName, false)[0].Selected = true;
    }

and

private void lvReads_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lvReads.SelectedItems.Count == 1)
        {               
            selectedIndex = lvReads.SelectedIndices[0];

            if (currentName != lvReads.Items[selectedIndex].Name)
            {

                //load item
            }

            currentName = lvReads.Items[selectedIndex].Name;
        }
    }
Ready Cent