为什么在修改所选项目时,ListBox会触发SelectedIndexChanged事件?

时间:2017-01-03 18:48:45

标签: c# winforms listbox selectedindexchanged .net-4.5.2

我们获得了一个Windows Form应用程序,它是从Microsoft Visual Studio的模板(PasteBin上的设计器代码1 2 3 4)创建的,具有默认的ListBox exampleListBox和按钮exampleButton

我们使用1到10之间的数字填充ListBox。

for (int i = 0; i < 10; ++i)
{
    exampleListBox.Items.Add(i);
}

然后我们添加两个事件处理程序。

exampleListBox_SelectedIndexChanged只会向控制台写入当前选定的索引。

private void exampleListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    Console.WriteLine(exampleListBox.SelectedIndex);
}

exampleButton_Click会将当前所选索引的项目设置为自身。如此有效,这应该完全没有改变。

private void exampleButton_Click(object sender, EventArgs e)
{
    exampleListBox.Items[exampleListBox.SelectedIndex] = exampleListBox.Items[exampleListBox.SelectedIndex];
}

单击按钮时,我预计不会发生任何事情。然而,这种情况并非如此。即使exampleListBox_SelectedIndexChanged未更改,单击该按钮也会触发SelectedIndex事件。

例如,如果我点击exampleListBox中索引2处的项目,则exampleListBox.SelectedIndex将变为2.如果我按exampleButton,则exampleListBox.SelectedIndex仍为2。然而,exampleListBox_SelectedIndexChanged事件发生了。

为什么即使所选索引未被更改,事件也会触发?

此外,无论如何都要防止这种行为发生?

2 个答案:

答案 0 :(得分:11)

当您修改ListBox中的项目(或实际上是ListBox的关联ObjectCollection中的项目)时,底层代码实际上会删除并重新创建该项目。然后它选择这个新添加的项目。因此,已更改所选索引 ,并引发相应的事件。

我没有特别引人注目的解释为什么控件会以这种方式运行。它既可以用于编程方便,也可以只是WinForms原始版本中的一个错误,后续版本必须维护该行为是出于向后兼容的原因。此外,后续版本必须保持相同的行为,即使项目未被修改。这是您正在观察的反直觉行为。

令人遗憾的是,它是not documented - 除非你理解为什么会发生这种情况,然后你知道在你不知情的情况下,实际的SelectedIndex属性在幕后被改变了。

Quantic留下了一条指向the relevant portion of the code in the Reference Source的评论:

internal void SetItemInternal(int index, object value) {
    if (value == null) {
        throw new ArgumentNullException("value");
    }

    if (index < 0 || index >= InnerArray.GetCount(0)) {
        throw new ArgumentOutOfRangeException("index", SR.GetString(SR.InvalidArgument, "index", (index).ToString(CultureInfo.CurrentCulture)));
    }

    owner.UpdateMaxItemWidth(InnerArray.GetItem(index, 0), true);
    InnerArray.SetItem(index, value);

    // If the native control has been created, and the display text of the new list item object
    // is different to the current text in the native list item, recreate the native list item...
    if (owner.IsHandleCreated) {
        bool selected = (owner.SelectedIndex == index);
        if (String.Compare(this.owner.GetItemText(value), this.owner.NativeGetItemText(index), true, CultureInfo.CurrentCulture) != 0) {
            owner.NativeRemoveAt(index);
            owner.SelectedItems.SetSelected(index, false);
            owner.NativeInsert(index, value);
            owner.UpdateMaxItemWidth(value, false);
            if (selected) {
                owner.SelectedIndex = index;
            }
        }
        else {
            // NEW - FOR COMPATIBILITY REASONS
            // Minimum compatibility fix for VSWhidbey 377287
            if (selected) {
                owner.OnSelectedIndexChanged(EventArgs.Empty); //will fire selectedvaluechanged
            }
        }
    }
    owner.UpdateHorizontalExtent();
}

在这里,您可以看到,在初始运行时错误检查之后,它更新了ListBox的最大项目宽度,在内部数组中设置了指定的项目,然后检查是否已创建本机ListBox控件。实际上,所有WinForms控件都是本机Win32控件的包装器,ListBox也不例外。在您的示例中,肯定已创建本机控件,因为它在表单上可见,因此if (owner.IsHandleCreated)测试的计算结果为true。然后它会比较项目的文本,看它们是否相同:

  • 如果它们不同,则删除原始项目,删除选择,添加新项目,并在选择原始项目时选择它。这会导致SelectedIndexChanged事件被引发。

  • 如果它们相同并且当前选择了该项,则如评论所示,“出于兼容性原因”,手动引发SelectedIndexChanged事件。

我们刚刚分析的这个SetItemInternal方法是从ListBox中调用的,用于ListBox.ObjectCollection对象的默认属性:

public virtual object this[int index] {
    get {
        if (index < 0 || index >= InnerArray.GetCount(0)) {
            throw new ArgumentOutOfRangeException("index", SR.GetString(SR.InvalidArgument, "index", (index).ToString(CultureInfo.CurrentCulture)));
        }

        return InnerArray.GetItem(index, 0);
    }
    set {
        owner.CheckNoDataSource();
        SetItemInternal(index, value);
    }
}

这是exampleButton_Click事件处理程序中的代码调用的内容。

无法阻止此行为发生。您必须通过在SelectedIndexChanged事件处理程序方法中编写自己的代码来找到解决它的方法。您可以考虑从内置ListBox类派生自定义控件类,重写OnSelectedIndexChanged方法,并将解决方法放在此处。这个派生类将为您提供一个方便的位置来存储状态跟踪信息(作为成员变量),它将允许您使用修改后的ListBox控件作为整个项目的替代品,而无需修改SelectedIndexChanged事件处理程序无处不在。

但老实说,这不应该是一个大问题或任何你甚至需要解决的问题。您对SelectedIndexChanged事件的处理应该是微不足道的 - 只需更新表单上的某些状态,如依赖控件。如果没有发生外部可见的变化,它触发的变化基本上就是无操作。

答案 1 :(得分:0)

Cody Gray在最后一个答案中给出了解决方案。我的代码示例:

private bool lbMeas_InhibitEvent = false; // "some state on your form" 
private void lbMeas_SelectedIndexChanged(object sender, EventArgs e)
{
 // when inhibit is found, disarm it and return without action
 if (lbMeas_InhibitEvent) { lbMeas_InhibitEvent = false; return; }
 // ... find the new item
 string cNewItem = "ABCD";
 // set new item content, make sure Inhibit is armed
 lbMeas_InhibitEvent = true;
 // now replace the currently selected item
 lbMeas.Items[lbMeas.SelectedIndex] = cNewItem;
 // ... your code will proceed here
}