简体   繁体   English

以编程方式选择索引0(从-1)时,组合框未触发SelectedIndexChanged

[英]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): 为清楚起见进行编辑(我希望):

  1. Run program 运行程序
  2. Select 'chair'. 选择“椅子”。 Message "Fired with selected item index:0" 消息“使用选定的项目索引:0触发”
  3. Hit button. 点击按钮。 Message "Fired with selected item index:-1" 消息“与所选项目索引一起发射:-1”
  4. Select 'table'. 选择“表格”。 Message "Fired with selected item index:1" 消息“与选定的项目索引一起发射:1”
  5. Hit button. 点击按钮。 Messages "Fired with selected item index:-1" AND "Fired with selected item index:1". 消息“使用所选项目索引:-1触发”和“使用所选项目索引:1触发”。

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.

相关问题 使用jQuery组合框时不触发SelectedIndexChanged - Not firing the SelectedIndexChanged when using jquery combobox 填充时如何防止ComboBox触发另一个ComboBox的SelectedIndexChanged事件 - How to prevent a ComboBox from firing another ComboBox's SelectedIndexChanged event when filling it ComboBox SelectedIndexChanged 事件未触发 - ComboBox SelectedIndexChanged event not firing SelectedIndexChanged不触发ComboBox - SelectedIndexChanged not firing for the ComboBox 为Winform中的组合框选择OwnerDrawFixed模式时,不会触发SelectedIndexChanged事件 - SelectedIndexChanged event is not firing when OwnerDrawFixed mode selected for Combobox in Winform 以编程方式更改索引时,不会触发ComboBox.SelectedIndexChanged - ComboBox.SelectedIndexChanged does not fire when programatically changing the index WinForms ComboBox SelectedIndexChanged在键入少量字符后跟Alt + Down时不触发 - WinForms ComboBox SelectedIndexChanged not firing when typing few chars followed by Alt+Down 从列表中选择项目时,列表框selectedIndexChanged不会触发 - Listbox selectedIndexChanged doesn't fire when selecting an item from the list asp.net c#在从服务器端代码更改索引时阻止触发selectedindexchanged事件 - asp.net c# prevent firing selectedindexchanged event while changing index from serverside code ComboBox SelectedIndexChanged - ComboBox SelectedIndexChanged
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM