本文介绍了使用 ListBox 缓慢显示/绘制对话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
我的应用程序使用实体框架来提取一小部分结果......它需要大约 3 秒才能完成?为什么会这样?
My application uses entity framework to pull in a small tiny set of results... It takes about 3 seconds for it to do it? Why might this be?
Start.cs
...
private void projectToolStripMenuItem_Click(object sender, System.EventArgs e)
{
NewProject newProjectForm = new NewProject();
newProjectForm.ShowDialog(); // It seems to take about 3 or 4 seconds to actually get to this :S
}
...
NewProject.cs
public partial class NewProject : Form
{
private EFProjectTypeRepository projectTypeRepository;
public NewProject()
{
projectTypeRepository = new EFProjectTypeRepository();
InitializeComponent();
ListBoxProjectTypes();
}
public void ListBoxProjectTypes()
{
DateTime then = DateTime.Now;
// PreLoadedResources.projectTypes is a preloaded resource which takes split seconds to load.
ListBoxProjectType.Items.AddRange(PreLoadedResources.projectTypes.Select(item => (object)item.Title).ToArray()); // If I comment this line out, the dialogue is almost instant @ timeTaken {00:00:00.0010019}
DateTime now = DateTime.Now;
TimeSpan timeTaken = now.Subtract(then);
}
}
:
timeTaken {00:00:02.4261369} System.TimeSpan
当我第二次去展示对话时,它是即时的!
when I go and show the dialogue a second time, it's instant!
到目前为止,我的列表框显示了 1 个项目,哈哈.
注意事项:
public static class PreLoadedResources
{
public static IEnumerable<ProjectType> projectTypes;
}
推荐答案
ListBox 必须在每次添加项目时重绘.您可以使用 Dmitry 使用 AddRange() 的方法,也可以使用 BeginUpdate()/EndUpdate() 调用来包装循环.
The ListBox has to redraw every time you add an item. You can either use Dmitry's method of using AddRange(), or you can wrap your loop with BeginUpdate()/EndUpdate() calls.
ListBoxProjectType.BeginUpdate();
foreach( var projectType in projectTypes )
{
ListBoxProjectType.Items.Add(projectType.Title);
}
ListBoxProjectType.EndUpdate();
这篇关于使用 ListBox 缓慢显示/绘制对话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)