C# 以编程方式触发 ListView.SelectedIndexChanged 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/808569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:03:26  来源:igfitidea点击:

Fire ListView.SelectedIndexChanged Event Programmatically?

c#winformslistviewselectedindexchanged

提问by James

How can one programmatically fire the SelectedIndexChanged event of a ListView?

如何以编程方式触发 ListView 的 SelectedIndexChanged 事件?

I've intended for the first item in my ListView to automatically become selected after the user completes a certain action. Code already exists within the SelectedIndexChanged event to highlight the selected item. Not only does the item fail to become highlighted, but a breakpoint set within SelectedIndexChanged is never hit. Moreover, a Debug.WriteLine fails to produce output, so I am rather certain that event has not fired.

我打算在用户完成某个操作后自动选中 ListView 中的第一项。SelectedIndexChanged 事件中已存在代码以突出显示所选项目。该项目不仅无法突出显示,而且永远不会命中 SelectedIndexChanged 中设置的断点。此外,Debug.WriteLine 无法产生输出,所以我很确定该事件没有被触发。

The following code fails to fire the event:

以下代码无法触发事件:

listView.Items[0].Selected = false;
listView.Items[0].Selected = true;
listView.Select();
Application.DoEvents();

The extra .Select() method call was included for good measure. ;) The deselection (.Selected = false) was included to deselect the ListViewItem in the .Items collection just in case it may have been selected by default and therefore setting it to 'true' would have no effect. The 'Application.DoEvents()' call is yet another last ditch method.

额外的 .Select() 方法调用被包括在内以进行很好的衡量。;) 包含取消选择 (.Selected = false) 以取消选择 .Items 集合中的 ListViewItem,以防万一它可能已被默认选中,因此将其设置为“true”将不起作用。'Application.DoEvents()' 调用是另一个最后的方法。

Shouldn't the above code cause the SelectedIndexChanged event to fire?

上面的代码不应该导致 SelectedIndexChanged 事件触发吗?

I should mention that the SelectedIndexChanged event fires properly on when an item is selcted via keyboard or mouse input.

我应该提到,当通过键盘或鼠标输入选择项目时,会正确触发 SelectedIndexChanged 事件。

采纳答案by Joshua Belden

Deselecting it by setting it to false won't fire the event but setting it to true will.

通过将其设置为 false 来取消选择它不会触发事件,但将其设置为 true 会。

    public Form1 ()
    {
        InitializeComponent();
        listView1.Items[0].Selected = false; // Doesn't fire
        listView1.Items[0].Selected = true; // Does fire.
    }

    private void listView1_SelectedIndexChanged (object sender, EventArgs e)
    {
        // code to run
    }

You might have something else going on. What event are you running your selection code?

你可能还有其他事情要做。您正在运行什么事件选择代码?

回答by Andrew Hare

Why can't you move the code that is currently inside your event handler's method into a method that can be called from the original spot and also from your code?

为什么不能将当前在事件处理程序方法中的代码移动到可以从原始位置和代码调用的方法中?

Something like this:

像这样的东西:

class Foo
{
    void bar(Object o, EventArgs e)
    {
        // imagine this is something important
        int x = 2;
    }

    void baz()
    {
        // you want to call bar() here ideally
    }
}

would be refactored to this:

将被重构为:

class Foo
{
    void bar(Object o, EventArgs e)
    {
        bop();
    }

    void baz()
    {
        bop();
    }

    void bop()
    {
        // imagine this is something important
        int x = 2;
    }
}

回答by Matt Brunell

If you create a class derived from ListView, you can call the protected method OnSelectedIndexChanged. This will fire the SelectedIndexChangedevent.

如果创建派生自的类ListView,则可以调用受保护的方法OnSelectedIndexChanged。这将触发SelectedIndexChanged事件。