views:

3621

answers:

3

How can one programmatically fire the SelectedIndexChanged event of a ListView?

I've intended for the first item in my ListView to automatically become selected after the user completes a certain action. Code already exists within the SelectedIndexChanged event to highlight the selected item. Not only does the item fail to become highlighted, but a breakpoint set within SelectedIndexChanged is never hit. Moreover, a Debug.WriteLine fails to produce output, so I am rather certain that event has not fired.

The following code fails to fire the event:

listView.Items[0].Selected = false;
listView.Items[0].Selected = true;
listView.Select();
Application.DoEvents();

The extra .Select() method call was included for good measure. ;) The deselection (.Selected = false) was included to deselect the ListViewItem in the .Items collection just in case it may have been selected by default and therefore setting it to 'true' would have no effect. The 'Application.DoEvents()' call is yet another last ditch method.

Shouldn't the above code cause the SelectedIndexChanged event to fire?

I should mention that the SelectedIndexChanged event fires properly on when an item is selcted via keyboard or mouse input.

+1  A: 

Why can't you move the code that is currently inside your event handler's method into a method that can be called from the original spot and also from your code?

Something like this:

class Foo
{
    void bar(Object o, EventArgs e)
    {
        // imagine this is something important
        int x = 2;
    }

    void baz()
    {
        // you want to call bar() here ideally
    }
}

would be refactored to this:

class Foo
{
    void bar(Object o, EventArgs e)
    {
        bop();
    }

    void baz()
    {
        bop();
    }

    void bop()
    {
        // imagine this is something important
        int x = 2;
    }
}
Andrew Hare
+1  A: 

If you create a class derived from ListView, you can call the protected method OnSelectedIndexChanged. This will fire the SelectedIndexChanged event.

Matt Brunell
+2  A: 

Deselecting it by setting it to false won't fire the event but setting it to true will.

 public Form1 ()
 {
  InitializeComponent();
  listView1.Items[0].Selected = false; // Doesn't fire
  listView1.Items[0].Selected = true; // Does fire.
 }

 private void listView1_SelectedIndexChanged (object sender, EventArgs e)
 {
  // code to run
 }

You might have something else going on. What event are you running your selection code?

Joshua Belden
Interestingly enough this does not hilight the current selection similar to mouseclick.
Mikko Rantanen
And deselecting does trigger the selected index changed IF the item is selected. By default they are unselected so that is why your Selected = false fails to fire the event.
Mikko Rantanen
Joshua,The selection code is running within a RunWorkerCompleted event that is fired upon completion of a long-running thread: bw = new BackgroundWorker() bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(listViewSelectingEvent);Other code within this event is able to change various properties of other controls on the form, so I would assume it is not a threading issue.
James
I'm sorry to say I'm weak on threading. Are you using an invoke call to change the properties? Process has an EnableRaisingEvents property, I wonder if the worker thread you're using has something similar.Also, checkout this article, might have something valuable. http://weblogs.asp.net/justin_rogers/articles/126345.aspx
Joshua Belden
It turns out that it was indeed a threading issue. Thank you for the link. For others that find this thread in the future, be careful that your ListView will be able to receive events from the thread in which you intend to modify selection properties.
James