How to programmatically fire the SelectedIndexChanged event in a ListView?
I decided that the first item in my ListView will automatically become selected after the user performs a specific action. Code already exists in the SelectedIndexChanged event to highlight the selected item. The item is not only not highlighted, but the breakpoint set in SelectedIndexChanged never hits. Moreover, Debug.WriteLine cannot execute the output, so I am sure that the event has not been fired.
The following code does not fire an event:
listView.Items[0].Selected = false; listView.Items[0].Selected = true; listView.Select(); Application.DoEvents();
An additional call to the .Select () method was included for good measure .;) Cancel (.Selected = false) was included to deselect the ListViewItem in the .Items collection, if it might have been selected by default, and therefore setting it to "true" will not affect. The call to "Application.DoEvents ()" is another last ditch method.
Should this code not raise the SelectedIndexChanged event?
I should mention that the SelectedIndexChanged event fires properly when an item is selected from the keyboard or mouse.
James source share