在另一个线程中动态地将控件添加到 WinForms [英] Add Controls to WinForms dynamically in another Thread
问题描述
我有一个包含数据网格视图、绑定源、文件资源管理器控件等的 Winform...
I have a Winform that contains a datagridview, bindingsource, file explorer control, etc...
我需要动态地向面板添加几个控件(自定义用户控件)(如 Panel.Controls.Add(...)).这个过程可能很慢.
I need add several controls (Custom UserControls) to a Panel dynamically (like Panel.Controls.Add(...)). This process can be slow.
我想向用户显示一条消息(等待).
I want to show to the user a message (waiting).
最好的方法是什么?我使用 Backgroundworker,但我遇到了问题,我的应用程序没有响应,datagridview 没有显示垂直滚动条,还有一些奇怪的事情.
What is the best way? I use Backgroundworker but I have problems, my application not responds and datagridview not shows scrollbar vertical, and another strange things.
推荐答案
线程可能不是此类 gui 操作的最佳选择.所有控件都应该在同一个线程上创建.
A thread is probably not your best bet for gui operations like this. All controls should be created on the same thread.
而是在表单底部放置一个状态栏控件.在状态栏上,包括一个进度条和一个标签.添加控件时,通过在标签中包含一条消息并增加进度条,在状态栏上指明这一点.
Instead put a statusbar control at the bottom of your form. On the statusbar, include a progress bar and a label. When adding a control, indicate this on the statusbar by including a message in the label and incrementing the progressbar.
这篇关于在另一个线程中动态地将控件添加到 WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!