C# 如何防止 ListBox.SelectedIndexChanged 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/905447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prevent ListBox.SelectedIndexChanged event?
提问by SO User
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 事件有何不同?
采纳答案by Kim Major
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());
回答by BFree
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;) 中解开,然后在方法完成之前,重新挂接到该事件。
回答by Fredrik M?rk
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
}
}
回答by Manish Andankar
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).
这意味着,只需将选择模式设为“无”,然后愉快地对控件进行数据绑定。然后继续将模式更改为所需的一种(这里我已将其更改为一种,您也可以选择多个)。
回答by steve
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
什么都不做,就在此方法中做任何你想做的事情,可能不是最有效的代码,但对我有用:)
回答by Dhananjay
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.
基本上从事件中删除委托,然后在完成后添加。
回答by Homer
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;