选择列表框Winform控件中的所有项目 [英] Selecting all items in a ListBox Winform Control
问题描述
我正在尝试选择ListBox中的所有项目,并为此制定了这种扩展方法:
I'm trying to select all items in ListBox and made this extension method for this purpose:
public static void SetSelectedAllItems(this ListBox ctl)
{
for (int i = 0; i < ctl.Items.Count; i++)
{
ctl.SetSelected(i, true);
}
}
问题是,如果列表框中有很多项目,则需要很长时间才能完成此任务,而且我可以看到列表框如何自动向下滚动并选择项目.
Problem is that if I have lots of items in the ListBox, it takes a long time to accomplish this task and I can watch how the ListBox is automatically scrolling down and selecting items.
是否有一种方法可以临时暂停控件的更新,以使任务更快地完成?我尝试使用:
Is there a way to temporary pause the update of a control, so that the task would finish faster? I tried using:
ctl.SuspendLayout();
for (int i = 0; i < ctl.Items.Count; i++)
...
ctl.ResumeLayout();
但这似乎无济于事.
推荐答案
调用 EndUpdate
方法,以防止在设置控件的属性时绘制/渲染该控件.
Call the BeginUpdate
and EndUpdate
methods to prevent the drawing/rendering of the control while properties on that control are being set.
这是修改后的代码:
public static void SetSelectedAllItems(this ListBox ctl)
{
ctl.BeginUpdate();
for (int i = 0; i < ctl.Items.Count; i++)
{
ctl.SetSelected(i, true);
}
ctl.EndUpdate();
}
您说过,您曾尝试致电 SuspendLayout
和
You said that you've tried calling SuspendLayout
and ResumeLayout
, but that only affects the control's layout events. This pair of methods is used when you want to change a control's position relative to other controls, like when you set the Size
, Location
, Anchor
, or Dock
properties.
这篇关于选择列表框Winform控件中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!