How to prevent ListBox.SelectedIndexChanged event?

后端 未结 7 1458
时光说笑
时光说笑 2020-12-16 11:37

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, ReportColum         


        
相关标签:
7条回答
  • 2020-12-16 11:56

    Inside the SelectedIndexChanged method, just pit in an if statement that says, if SelectedIndex == -1 do nothing else, do whatever you want to do in this method, probably not the most efficient code but worked for me :)

    0 讨论(0)
  • 2020-12-16 12:02

    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).

    0 讨论(0)
  • 2020-12-16 12:05

    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
        }
    
    }
    
    0 讨论(0)
  • 2020-12-16 12:09

    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());
    
    0 讨论(0)
  • 2020-12-16 12:11

    My situation might be unique, but I'll post it here just in case it helps someone.

    I was doing the Items.AddRange to populate my ListBox instead of binding. I don't know why, but on PostBack, the SelectedIndex was going from -1 to 0. This might be because my ListBox was SelectionMode="Single".

    My fix was to manually set the SelectedIndex to 0 instead of -1 after population:

    listBox.Items.AddRange(items);
    listBox.SelectedIndex = 0;
    
    0 讨论(0)
  • 2020-12-16 12:14

    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.

    0 讨论(0)
提交回复
热议问题