我有一个类似于mdi表单的主窗口表单。当用户从菜单中选择一个选项时,主窗口将加载要显示的UserControl。
Private Sub treNavTree_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles treNavTree.NodeMouseClick
Dim nextDisplay As UserControl = GetUserControl(e.Node.Text)
If nextDisplay Is Nothing Then Exit Sub
pnlMain.Controls.Remove(currentDisplay)
currentDisplay = nextDisplay
pnlMain.Controls.Add(currentDisplay, 1, 0)
currentDisplay.Dock = DockStyle.Fill
currentDisplay.Show()
End Sub
这些加载的每个UserControl都有一个ListView,其中包含从数据库中获取的许多项目。就目前而言,这些列表视图填充有UserControl.Load事件中的上述项目。这是一个问题,因为在等待这些大型列表视图填充时,UserControl.Paint明显闪烁/挂起。我的想法是将ListView的加载从UserControl的Load事件之外移到其自己的方法中,并在调用Show()之后进行调用。不幸的是,在开始加载列表视图之前,UserControl似乎并没有完全完成“首先显示”。至少,它仍然以完全相同的方式挂起。为什么会这样呢?在开始下一行之前还没有完全完成?我怀疑是这样,因为Show()和ShowDialog()之间存在差异。MSDN表示,与ShowDialog()相对,在调用Show()之后,代码将继续执行。如何在运行LoadListView方法之前确保UserControl完全完成自身的绘制?为了尽量减少闪烁,我尝试了互联网上的所有建议。什么都行不通。我需要这种情况发生。谢谢!
Private Sub treNavTree_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles treNavTree.NodeMouseClick
Dim nextDisplay As UserControl = GetUserControl(e.Node.Text)
If nextDisplay Is Nothing Then Exit Sub
pnlMain.Controls.Remove(currentDisplay)
currentDisplay = nextDisplay
pnlMain.Controls.Add(currentDisplay, 1, 0)
currentDisplay.Dock = DockStyle.Fill
currentDisplay.Show()
currentDisplay.LoadListView() 'Load the listview after done showing
End Sub
在尝试了几种不同的方法之后,.Show()
如果不立即启动ListView的加载,便无法找到通往下一个UserControl的方法。我现在很确定它必须异步运行.Show()并在加载表单时继续处理下几行,这不可避免。因此,我制作了自己的后台工作程序,并在该线程上加载了列表视图,同时在主线程上加载了UserControl,并在加载完成后将后台工作程序线程又回到了主线程上。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句