如何prevent ListBox.SelectedIndexChanged事件?事件、prevent、ListBox、SelectedIndexChanged

2023-09-03 00:21:28 作者:酷似一个小祖宗

我使用的是列表框在我的C#2.0 Windows窗体应用程序。填充列表框中的方法是

I am using a listbox in my C#2.0 windows forms application. The method to populate the list box is

    private void PopulateListBox(ListBox lb, ReportColumnList reportColumnList)
    {
        lb.DataSource = reportColumnList.ReportColumns;
        lb.DisplayMember = "ColumnName";
        lb.ValueMember = "ColumnName";
    }

然而,当它执行它也要求列表框的SelectedIndexChanged事件处理程序,即使我不是设定在方法选择的指数方法同上。我怎样才能prevent被要求对上述code SelectedIndexChanged事件处理程序?

However, when it executes the method it also calls the SelectedIndexChanged event handler of the listbox, even though I am not setting the selected index in the method above. How can I prevent the SelectedIndexChanged event handler from being called for the above code?

我希望用户做出选择的列表框,只有当事件被调用。或者是有这仅适用于使用鼠标点击?用户选择列表框中的项目

I want the event to be called only when the user makes the selections in the ListBox. Or is there any other event which is only for user selecting listbox items using mouse click?

在另一方面,即使用户点击一个空白区域,在列表框中SelectedIndexChanged事件被炒鱿鱼。所以,我想知道如何不同,它从鼠标点击事件?

On another note, even if the user clicks on an empty area in the list box the SelectedIndexChanged event gets fired. So I am wondering how different is it from the MouseClick event?

推荐答案

您可以填充,而不是设置数据源使用lb.Items.AddRange(列表框)。除了不能触发的SelectedIndexChanged,这也不会pre-选择第一项。

You can populate the listbox using lb.Items.AddRange() instead of setting the datasource. In addition to not triggering the SelectedIndexChanged, this also won't pre-select the first item.

 lb.Items.Clear();
 lb.Items.AddRange(reportColumnList.ReportColumns.ToArray());