Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent ListBox.SelectedIndexChanged event?

Tags:

c#

.net

winforms

I am using a listbox in my C#2.0 windows forms application. The method to populate the list box is

    private void PopulateListBox(ListBox lb, ReportColumnList reportColumnList)
    {
        lb.DataSource = reportColumnList.ReportColumns;
        lb.DisplayMember = "ColumnName";
        lb.ValueMember = "ColumnName";
    }

However, when it executes the method it also calls the SelectedIndexChanged event handler of the listbox, even though I am not setting the selected index in the method above. How can I prevent the SelectedIndexChanged event handler from being called for the above code?

I want the event to be called only when the user makes the selections in the ListBox. Or is there any other event which is only for user selecting listbox items using mouse click?

On another note, even if the user clicks on an empty area in the list box the SelectedIndexChanged event gets fired. So I am wondering how different is it from the MouseClick event?

like image 461
SO User Avatar asked May 25 '09 05:05

SO User


People also ask

How do you prevent SelectedIndexChanged event when datasource is bound?

You may use Tag property of combobox. It can be empty or 0 integer value when it's empty or have not fulfilled yet. You have to set combobox's Tag as count of it's items after bounding. In SelectedValueChanged event if the Tag property is null or 0 you have to return from void.

What does SelectedIndexChanged mean?

The SelectedIndexChanged event occurs when the SelectedIndex has just changed. The SelectedIndexChanged event does not fire unless the AutoPostBack property is True . The SelectedIndexChanged event handler receives two arguments: The RadDropDownList that is loading items.

Which event is raised when a listbox item is selected?

If they are the same and the item is currently selected, then as the comment indicates, "for compatibility reasons", the SelectedIndexChanged event is manually raised.


4 Answers

There are three ways you can go about this.

A) Don't hook into the SelectedIndexChanged event until AFTER you've called this method.

B) Have a private boolean in your class that's initially set to true. At the start of your PopulateListBoxmethod, set it to false, and at the end set it back to true. In your event handler for SelectedIndexChanged , if the flag is false, just return and do nothing.

C) In the start of your PopulateListBoxmethod, unhook from the event (this.listbox1.SelectedIndexChanged -= myMethod;) and then right before the method is done, rehook into that event.

like image 53
BFree Avatar answered Sep 18 '22 01:09

BFree


You can populate the listbox using lb.Items.AddRange() instead of setting the datasource. In addition to not triggering the SelectedIndexChanged, this also won't pre-select the first item.

 lb.Items.Clear();  lb.Items.AddRange(reportColumnList.ReportColumns.ToArray()); 
like image 37
Kim Major Avatar answered Sep 18 '22 01:09

Kim Major


There is a simplest answer to this problem: A Code snippet below can suggest the work around.

lstBxState.SelectionMode = SelectionMode.None;
lstBxState.DataSource = lstStates;
lstBxState.ValueMember = "StateId";
lstBxState.DisplayMember = "StateName";
lstBxState.ClearSelected();
lstBxState.SelectionMode = SelectionMode.One;

This means, just Make the Selection Mode as "None", and happily Data Bind the control. Then go ahead and change the mode to the required one (Here I have changed it to One, you may select Multiple too).

like image 21
Manish Andankar Avatar answered Sep 19 '22 01:09

Manish Andankar


You should be aware that even if you do not handle the event, the first item in the list will still be selected automatically when assigning the DataSource property. In order to force the user to make a selection you will need to unselect the automatically selected item.

If your SelectedIndexChanged event handler triggers some functionality when an item is selected you may still want to block this from being executed. One way to do that is to keep track of whether the code is currently populating the listbox or not. If the SelectedIndexChanged event is triggered while the listbox is being populated, you can avoid performing those actions:

private bool _populating = false;
private void PopulateListBox(ListBox lb, ReportColumnList reportColumnList)
{
    _populating = true;
    lb.DataSource = reportColumnList.ReportColumns;
    lb.DisplayMember = "ColumnName";
    lb.ValueMember = "ColumnName";

    // clear the automatically made selection
    lb.SelectedIndex = -1;
    _populating = false;
}

private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    if (!_populating)
    {
        // perform the action associated with selecting an item
    }

}
like image 42
Fredrik Mörk Avatar answered Sep 19 '22 01:09

Fredrik Mörk