Usercontrol加载事件未触发 [英] Usercontrol load event not firing

查看:162
本文介绍了Usercontrol加载事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB.net,VS 2012



我有一个表格(frmParts),它有太多的控件。管理变得非常困难。我花了最近几天把它分成几个UserControls(每个都有20到30个文本框,标签,单选按钮等)。



而不是在设计时将每个UserControl的副本拖到窗体上,而是在加载父窗体时在代码中执行它。



UserControls是ucPartsHeader,ucPartsSH,ucPartsTCD,ucPartsCH和ucPartsECD。



添加ucPartsHeader时to frmPart它的Load事件触发。但是当添加其他4个时,它们的Load事件不会触发。我一遍又一遍地在代码中。我找不到什么是错的。我已经完成了对Load事件的覆盖,但这也不起作用。它永远不会到达那里。



所有的预加载事件代码似乎都会触发(比如在每个UserControl上创建条形管理器,表适配器等)。 br />


每个UserControl都有一个名为pubfncLoadData的公共函数。



我试过的:



这是我在frmParts的Load事件中运行的代码:



  Dim  sqlWhere 作为 字符串 =   WHERE PartID =&  .partID.ToString 
.pvtfncLoadData(sqlWhere)

' 这3行完美运行并按我的预期行事
Dim ucPH 作为 ucPartsHeader
Me .pnlHeader.Controls.Add(ucPH)
ucPH.pubfncLoadData(sqlWhere)

Dim ucSH 作为 ucPartsSH ' <<<此行正常运行
.pnlSH.Controls.Add(ucSH)' <<<这条线似乎运行正常,但实际上ucPartsSH的Load事件永远不会触发
ucSH.pubfncLoadData(sqlWhere)' <<<然后当它到达时,它崩溃,因为它需要首先运行Load事件

' 接下来的3个代码块也是如此(它们无法正常工作)
Dim ucTCD 作为 ucPartsTCD
.pnlTCD。 Controls.Add(ucTCD)
ucTCD.pubfncLoadData(sqlWhere)

Dim ucCH 正如 ucPartsCH
.pnlCH.Controls.Add(ucCH)
ucCH.pubfncLoadData(sqlWhere)

Dim ucECD As ucPartsECD
.pnlECD.Controls.Add(ucECD)
ucECD.pubfncLoadData( sqlWhere)





L每个5个UserControl的oad事件代码几乎相同。但它只是没有达到4个UserControls。



我在这一切中都有Try / Catch块,所以它将错误发布到日志文件中继续前进。创建表单并显示用户控件。但是UserControls中没有数据,因为它们的每个Load事件都没有运行。



我知道有人会想看到更多这里的代码能够提供帮助。我现在才刚开始,看看是否有人对我如何创建和将UserControls添加到主窗体有任何见解。由于它们几乎相同,我不明白为什么第一个有效,有些则无效。我评论了第一个,看看是否允许第二个工作。那没有做到。无论如何,第二,第三,第四和第五都不起作用,即使第一个被注释掉了。这是我第一次使用UserControl来更好地组织事情。也许我在这里遗漏了一些明显的东西。



谢谢,



A.

解决方案

只是猜测:您可能必须激活为其Load事件添加相应控件的面板。

UserControl.Load事件 [ ^ ]

示例:

  Dim  ucSH 正如  ucPartsSH 
.pnlSH.Controls.Add(ucSH)
.pnlSH。选择()
ucSH.pubfncLoadData(sqlWhere)

...等等其他面板。



希望这有帮助。

是的。我想你可能是对的。我想我刚刚得出了同样的结论。我花了一个小时从UserControl一次艰苦地删除一个控件,看看哪个是违规项目。即使在我删除了所有这些之后,我仍然遇到了同样的错误。第一个运行的,一个有效的,立即显示,因为它位于frmParts的顶部。其他4个位于8选项卡选项卡控件的第二个选项卡中,它们最初是隐藏的。


就是这样。谢谢。我知道有人会对此有所了解。对此,我真的非常感激。我从没想过他们不会通过我已经拥有的代码加载。我通过激活那些4个UC的标签然后激活标签控件中的第一个标签来修复它。很高兴在退出当天之前解决这个问题。再次感谢。


VB.net, VS 2012

I've got a form (frmParts) that had too many controls on it. It was getting very hard to manage. I spent the last couple days breaking it up into several UserControls (each with 20 to 30 text boxes, labels, radio buttons, etc).

Rather than dragging a copy of each UserControl onto the form at design-time, I'm doing it in code when the parent form is loaded.

The UserControls are ucPartsHeader, ucPartsSH, ucPartsTCD, ucPartsCH, and ucPartsECD.

When ucPartsHeader is added to frmParts its Load event fires. But when the other 4 are added, their Load events do not fire. I've been over and over and over the code. I can't find what's wrong. I've done an override of the Load events and that doesn't work either. It just never gets there.

All the pre-Load event code seems to fire (like creating bar managers, table adapters, etc that are on each UserControl).

Each UserControl has a public function called pubfncLoadData.

What I have tried:

Here's my code that runs in the Load event of frmParts:

Dim sqlWhere As String = " WHERE PartID = " & Me.partID.ToString
Me.pvtfncLoadData(sqlWhere)

'These 3 lines run perfectly and do what I expect them to do
Dim ucPH As New ucPartsHeader
Me.pnlHeader.Controls.Add(ucPH)
ucPH.pubfncLoadData(sqlWhere)

Dim ucSH As New ucPartsSH '<<< This line runs fine
Me.pnlSH.Controls.Add(ucSH) '<<< This line seems to run fine but actually the Load event of ucPartsSH never fires
ucSH.pubfncLoadData(sqlWhere) '<<< Then when it gets here, it crashes because it needs the Load event to run first

'The same is true for the next 3 blocks of code (they don't work right)
Dim ucTCD As New ucPartsTCD
Me.pnlTCD.Controls.Add(ucTCD)
ucTCD.pubfncLoadData(sqlWhere)

Dim ucCH As New ucPartsCH
Me.pnlCH.Controls.Add(ucCH)
ucCH.pubfncLoadData(sqlWhere)

Dim ucECD As New ucPartsECD
Me.pnlECD.Controls.Add(ucECD)
ucECD.pubfncLoadData(sqlWhere)



The Load event code for each of the 5 UserControls is nearly identical. But it just doesn't even get there for 4 of the UserControls.

I have Try/Catch blocks in all this so it posts the errors to a log file and keeps going. The form is created and the user controls show up. But the UserControls have no data in them because of the fact that each of their Load events don't run.

I know someone's going to want to see a lot more code here to be able to help. I'm just starting here for now to see if anyone has any insight on how I'm creating and adding the UserControls to the main form. Since they are all nearly the same, I don't understand why the first one works and the others do not. I commented out the first one to see if that would allow the 2nd one to work. That didn't do it. The 2nd, 3rd, 4th, and 5th don't work at all anyway, even with the first one commented out. This is my first time using UserControls to organize things a little better. Maybe I'm missing something obvious here.

Thanks,

A.

解决方案

Just a guess: You may have to activate the panel to which corresponding control is added for its Load event to be fired.
UserControl.Load Event[^]
Example:

Dim ucSH As New ucPartsSH
Me.pnlSH.Controls.Add(ucSH)
Me.pnlSH.Select()
ucSH.pubfncLoadData(sqlWhere)

... and so on for other panels.

Hope this helps.


Yep. I think you might be right. I think I just came to the same conclusion. I spent an hour painstakingly removing one control at a time from the UserControl to see which was the offending item. Even after I'd removed all of them I still got the same error. The first one that runs, the one that works, is shown immediately because it's at the top of frmParts. The other 4 are in the 2nd tab of an 8 tab tab control and they are hidden from view initially.


That was it. Thank you. I knew someone would have some insight into this. I really appreciate it. I never expected them to not "load" via the code I already had. I fixed it by activating the tab that those 4 UC's are on and then afterward activating the first tab in the tab control. Glad to get this solved before quitting for the day. Thanks again.


这篇关于Usercontrol加载事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆