[英]Combobox not firing SelectedIndexChanged when programmatically selecting index 0 (from -1)
I have a combobox for wich I am using the SelectIndexChanged event to capture both user and programmatically changes. 我有一个组合框,我正在使用SelectIndexChanged事件捕获用户和程序更改。
Clearing and reloading the list bound to the combobox will fire the eventhandler with index -1 naturally. 清除并重新加载绑定到组合框的列表将自然触发索引为-1的事件处理程序。
But then with selectedindex=-1 但是然后selectedindex = -1
combobox1.SelectedIndex = 0 ; // will NOT fire the event.
but 但
combobox1.SelectedIndex = 1 ; // or higher number WILL fire the event.
In both cases I AM programmatically changing the selextedindex and expect the event to be fired. 在这两种情况下,我都是以编程方式更改selextedindex并期望事件被触发。
I verified the behavior in a simple form. 我以简单的形式验证了行为。
namespace cmbTest
{
public partial class Form1 : Form
{
private BindingList<string> items = new BindingList<string>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DataSource = items;
loadItems();
}
private void loadItems()
{
items.Add("chair");
items.Add("table");
items.Add("coffemug");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Fired with selected item index:" + comboBox1.SelectedIndex);
}
private void button1_Click(object sender, EventArgs e)
{
int index = comboBox1.SelectedIndex;
// Clear and reload items for whatever reason.
items.Clear();
loadItems();
// try select old item index; if it doesnt exists, leave it.
try { comboBox1.SelectedIndex = index; }
catch { }
}
}
}
The form has a combobox1 and a button1. 该表单具有一个combobox1和一个button1。
EDIT for clarity (I hope): 为清楚起见进行编辑(我希望):
I expect to get two messages when hitting button when "chair" is selected too, since I programmatically changes the index to 0. 我也希望在选择“椅子”时按一下按钮也会收到两条消息,因为我以编程方式将索引更改为0。
So, why is this not working as I expect it to do, and what will be an acceptable workaround? 那么,为什么这不能按我预期的那样工作?什么是可以接受的解决方法?
When your first item is added to the items collection the index is automatically changed to 0. That's why when your previous index is saved as 0, and then you set it again using this line comboBox1.SelectedIndex = index;
当您将第一个项目添加到项目集合时,索引会自动更改为0。这就是为什么当您之前的索引另存为0,然后使用此行
comboBox1.SelectedIndex = index;
再次对其进行设置的原因comboBox1.SelectedIndex = index;
, the index is not changed. ,索引不变。 That's why the event is not fired.
因此,该事件未触发。
Looking at the source code for the ComboBox, the event is not fired in 2 cases : Either an expcetion is thrown, or the index is set to the same value that it is was. 查看ComboBox的源代码,在两种情况下不会触发该事件:抛出了异常,或者索引设置为与原来相同的值。
If you really want some hack around this, you can do it this way: 如果您真的想对此进行一些破解,可以采用以下方式:
int index = comboBox1.SelectedIndex;
// Clear and reload items for whatever reason.
items.Clear();
loadItems();
if(comboBox1.SelectedIndex == index)
{
comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;
comboBox1.SelectedIndex = -1;
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
}
// try select old item index; if it doesnt exists, leave it.
try { comboBox1.SelectedIndex = index; }
catch
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.