【问题标题】:Combobox not firing SelectedIndexChanged when programmatically selecting index 0 (from -1)以编程方式选择索引 0(从-1)时,组合框未触发 SelectedIndexChanged
【发布时间】:2016-07-18 10:33:47
【问题描述】:

我有一个组合框,我使用 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 { }
        }





    }
}

表单有一个combobox1 和一个button1。

为清楚起见进行编辑(我希望):

  1. 运行程序
  2. 选择“椅子”。消息“使用选定的项目索引触发:0”
  3. 点击按钮。消息“使用选定的项目索引触发:-1”
  4. 选择“表格”。消息“使用选定的项目索引触发:1”
  5. 点击按钮。消息“使用选定的项目索引触发:-1”和 “使用选定的项目索引:1 触发”。

我希望在选择“椅子”时点击按钮时收到两条消息,因为我以编程方式将索引更改为 0。

那么,为什么这没有像我预期的那样工作,什么是可接受的解决方法?

【问题讨论】:

  • 吞下异常?这不好。检查是否有异常被抛出。
  • 指的是catch{}?仅当重新加载列表不再具有项目 inde 时,才会使用 try 语句。这里不是这种情况,本例不需要
  • 我在您的代码中看不到 comboBox1.SelectedIndex = 0;。问题到底出在哪里?
  • 当您运行程序并选择索引 0(椅子)然后按下按钮。再次选择索引 0 时不会触发事件。
  • 我无法真正重现您的问题。您能具体说明导致此问题的具体步骤吗?

标签: c# .net combobox .net-4.0


【解决方案1】:

当您的第一个项目添加到项目集合时,索引会自动更改为 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
{
}

【讨论】:

  • 你是对的。再次设置列表时,所选索引设置为 0。奇怪的是,我的调试窗口在将其设置为 0 之前仍然显示为 -1。但是一个额外的消息框清楚地表明所选索引在尝试将其设置回 0 之前已经为 0。
  • 它给了我一些提示,但由于我的事件处理程序总是会跳过 -1,我可能会在 loaditems 方法中将所选项目强制为 -1。
  • @NickSick 这行不通。尝试一下。您需要使用取消订阅/订阅事件才能使其正常工作。就像我做的一样。如果您觉得此答案有帮助,可以将其标记为已接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多