Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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.
Posted

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:
VB.NET
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.
 
Share this answer
 
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.
 
Share this answer
 
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.
 
Share this answer
 
Comments
phil.o 21-Feb-18 19:05pm    
You are welcome :)
Please, do not answer to a solution with another solution. I have not been notified of your replies since you did not use the "Have a question or comment?" button.
And please mark your question as answered if appropriate.
Kindly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900