以编程方式选择索引 0(从 -1)时,组合框不触发 SelectedIndexChanged
Combobox not firing SelectedIndexChanged when programmatically selecting index 0 (from -1)
我有一个组合框,我正在使用 SelectIndexChanged 事件来捕获用户和编程更改。
清除并重新加载绑定到组合框的列表将自然触发索引为 -1 的事件处理程序。
但随后 selectedindex=-1
combobox1.SelectedIndex = 0 ; // will NOT fire the event.
但是
combobox1.SelectedIndex = 1 ; // or higher number WILL fire the event.
在这两种情况下,我都以编程方式更改 selextedindex 并期望事件被触发。
我以简单的形式验证了行为。
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 { }
}
}
}
表单有一个组合框 1 和一个按钮 1。
为清楚起见编辑(我希望):
- 运行 程序
- Select'chair'。留言 "Fired with selected item index:0"
- 点击按钮。留言 "Fired with selected item index:-1"
- Select'table'。留言 "Fired with selected item index:1"
- 点击按钮。消息 "Fired with selected item index:-1" 和
"Fired with selected item index:1".
当我也选择了 "chair" 时,我希望在点击按钮时收到两条消息,因为我以编程方式将索引更改为 0。
那么,为什么它没有像我预期的那样工作,什么是可接受的解决方法?
当您的第一个项目添加到项目集合时,索引会自动更改为 0。这就是为什么当您之前的索引保存为 0,然后您使用此行再次设置它时 comboBox1.SelectedIndex = index;
,索引没有改变。这就是事件未被触发的原因。
查看 ComboBox 的源代码,在 2 种情况下未触发事件:抛出异常,或者索引设置为与原来相同的值。
如果你真的想解决这个问题,你可以这样做:
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
{
}
我有一个组合框,我正在使用 SelectIndexChanged 事件来捕获用户和编程更改。
清除并重新加载绑定到组合框的列表将自然触发索引为 -1 的事件处理程序。
但随后 selectedindex=-1
combobox1.SelectedIndex = 0 ; // will NOT fire the event.
但是
combobox1.SelectedIndex = 1 ; // or higher number WILL fire the event.
在这两种情况下,我都以编程方式更改 selextedindex 并期望事件被触发。
我以简单的形式验证了行为。
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 { }
}
}
}
表单有一个组合框 1 和一个按钮 1。
为清楚起见编辑(我希望):
- 运行 程序
- Select'chair'。留言 "Fired with selected item index:0"
- 点击按钮。留言 "Fired with selected item index:-1"
- Select'table'。留言 "Fired with selected item index:1"
- 点击按钮。消息 "Fired with selected item index:-1" 和 "Fired with selected item index:1".
当我也选择了 "chair" 时,我希望在点击按钮时收到两条消息,因为我以编程方式将索引更改为 0。
那么,为什么它没有像我预期的那样工作,什么是可接受的解决方法?
当您的第一个项目添加到项目集合时,索引会自动更改为 0。这就是为什么当您之前的索引保存为 0,然后您使用此行再次设置它时 comboBox1.SelectedIndex = index;
,索引没有改变。这就是事件未被触发的原因。
查看 ComboBox 的源代码,在 2 种情况下未触发事件:抛出异常,或者索引设置为与原来相同的值。
如果你真的想解决这个问题,你可以这样做:
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
{
}