如何防止ListBox.SelectedIndexChanged事件?

[英]How to prevent ListBox.SelectedIndexChanged event?


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

我在我的C#2.0 Windows窗体应用程序中使用了一个列表框。填充列表框的方法是

    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?

但是,当它执行该方法时,它也会调用列表框的SelectedIndexChanged事件处理程序,即使我没有在上面的方法中设置所选索引。如何阻止为上述代码调用SelectedIndexChanged事件处理程序?

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?

我希望仅在用户在ListBox中进行选择时才调用该事件。或者是否有任何其他事件仅供用户使用鼠标单击选择列表框项目?

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?

另外,即使用户单击列表框中的空白区域,也会触发SelectedIndexChanged事件。所以我想知道它与MouseClick事件有何不同?

7 个解决方案

#1


16  

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.AddRange()填充列表框,而不是设置数据源。除了不触发SelectedIndexChanged之外,这也不会预先选择第一个项目。

 lb.Items.Clear();
 lb.Items.AddRange(reportColumnList.ReportColumns.ToArray());

#2


18  

There are three ways you can go about this.

你有三种方法可以解决这个问题。

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

A)在调用此方法之后,不要挂钩到SelectedIndexChanged事件。

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.

B)在你的类中有一个私有布尔值,它最初设置为true。在PopulateListBox方法的开头,将其设置为false,最后将其设置为true。在SelectedIndexChanged的事件处理程序中,如果标志为false,则只返回并且不执行任何操作。

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.

C)在PopulateListBox方法的开头,取消事件(this.listbox1.SelectedIndexChanged - = myMethod;),然后在方法完成之前,重新挂钩到该事件。

#3


6  

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

这意味着,只需将选择模式设为“无”,并愉快地将数据绑定到控件。然后继续将模式更改为所需模式(此处我已将其更改为One,您也可以选择Multiple)。

#4


2  

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.

您应该知道,即使您不处理该事件,在分配DataSource属性时仍将自动选择列表中的第一项。为了强制用户进行选择,您需要取消选择自动选择的项目。

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:

如果SelectedIndexChanged事件处理程序在选择项目时触发某些功能,您可能仍希望阻止执行此操作。一种方法是跟踪代码当前是否填充列表框。如果在填充列表框时触发SelectedIndexChanged事件,则可以避免执行这些操作:

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
    }

}

#5


1  

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

在SelectedIndexChanged方法内部,只是在一个if语句中,如果SelectedIndex == -1不执行任何其他操作,请执行此方法中的任何操作,可能不是最有效的代码但对我有效:)

#6


0  

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

我正在使用Items.AddRange来填充我的ListBox而不是绑定。我不知道为什么,但是在PostBack上,SelectedIndex从-1变为0.这可能是因为我的ListBox是SelectionMode =“Single”。

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

我的修复是在填充后手动将SelectedIndex设置为0而不是-1:

listBox.Items.AddRange(items);
listBox.SelectedIndex = 0;

#7


0  

private void PopulateListBox(ListBox lb, ReportColumnList reportColumnList)
{

    lb.SelectedIndexChanged -= this.*MethodnamewhichhandlesthisEvent*;  //unset eventhandler

    lb.DataSource = reportColumnList.ReportColumns;
    lb.DisplayMember = "ColumnName";
    lb.ValueMember = "ColumnName";

    lb.SelectedIndexChanged += this.*MethodnamewhichhandlesthisEvent*;  //set the eventhandler
}

Basically remove the delegate from the event and then add once you are done.

基本上从事件中删除委托,然后在完成后添加。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2009/05/25/d1b9d8fbf7f539112a30e234f4361cf.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号