gpt4 book ai didi

c# - 选择 ListBox Winform 控件中的所有项目

转载 作者:行者123 更新时间:2023-11-30 14:13:04 33 4
gpt4 key购买 nike

我正在尝试选择 ListBox 中的所有项目并为此制作了此扩展方法:

    public static void SetSelectedAllItems(this ListBox ctl)
{
for (int i = 0; i < ctl.Items.Count; i++)
{
ctl.SetSelected(i, true);
}
}

问题是,如果我在 ListBox 中有很多项目,完成这项任务需要很长时间,我可以看到 ListBox 如何自动向下滚动并选择项目。

有没有办法暂时暂停控件的更新,以便更快地完成任务?我尝试使用:

ctl.SuspendLayout();
for (int i = 0; i < ctl.Items.Count; i++)
...
ctl.ResumeLayout();

但这似乎并没有做任何事情。

最佳答案

调用BeginUpdateEndUpdate在设置该控件的属性时防止绘制/呈现该控件的方法。

修改后的代码:

public static void SetSelectedAllItems(this ListBox ctl)
{
ctl.BeginUpdate();

for (int i = 0; i < ctl.Items.Count; i++)
{
ctl.SetSelected(i, true);
}

ctl.EndUpdate();
}

你说你试过调用SuspendLayoutResumeLayout ,但这只会影响控件的布局事件。当您想要更改控件相对于其他控件的位置时使用这对方法,例如当您设置 SizeLocationAnchor 时,或 Dock 属性。

关于c# - 选择 ListBox Winform 控件中的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15398091/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com