如何prevent ListBox.SelectedIndexChanged事件? [英] How to prevent ListBox.SelectedIndexChanged event?

查看:175
本文介绍了如何prevent ListBox.SelectedIndexChanged事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是列表框在我的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());

这篇关于如何prevent ListBox.SelectedIndexChanged事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆