如何防止 TabControl 中 UserControl 的延迟加载?
我刚刚发现 TabControl 中的 UserControls 在选择父 TabPage 之前不会加载。有没有办法防止这种延迟加载?我需要在主窗体加载时初始化用户控件。
I have just discovered that UserControls in a TabControl will not load until the parent TabPage is selected. Is there a way to prevent this delay loading? I need to initialize the UserControls when the main form loads.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
TabControl
不会特殊对待它的控件,事实上,在任何情况下,UserControl
上的Load
事件紧接在UserControl
之前发生都是正常的。第一次显示控制。TabPage
负责显示控件,因此只有在首次选择时才会“加载”。为了克服这种(完全正常的)Windows 窗体行为,您可以将初始化代码移至单独的方法,并在
Form
加载时调用它,或者您可以将初始化代码放在UserControl 中
的构造函数。无论哪种方式,您都可以立即执行初始化。The
TabControl
does not treat its controls specially, in fact it is normal under all circumstances for theLoad
event on aUserControl
to occur immediately before the control is displayed for the first time. TheTabPage
is responsible for showing the control, therefore it will only be 'loaded' when first selected.To overcome this (perfectly normal) Windows Forms behaviour, you could move your initialisation code to a separate method and call it when the
Form
loads, or you could just place your initialisation code in theUserControl
's constructor instead. Either way, you can perform your initialisation immediately.您可以在表单的 Load 事件处理程序中为选项卡调用 Tabcontrol 的 SelectTab() 方法。
You can invoke Tabcontrol's SelectTab() method for the tabs in your Form's Load event handler.
我只是在寻找如何实现您描述的这种默认行为。我支持的应用程序不会延迟选项卡的加载。事实证明,选项卡是在加载事件而不是构造函数中初始化的。
因此,如果您在表单加载事件中将选项卡添加到选项卡控件,选项卡中的所有控件都会将其加载事件作为 TabPages.AddRange 调用的一部分触发
I was just searching how to achieve this default behaviour you describe. An application I support was not delaying the load of the tabs. Turns out the tabs were getting initialised in the load event instead of the constructor.
So if you add the tabs to the tabcontrol in the form load event all the controls in tabs will have their load events fired as a part of the TabPages.AddRange call