Accessing public events within nested UserControls?

Ok,

I hope I can convey what I'm trying to do....

I have a single UserControl (uc1) that is comprised of 6 other UserControls (ucSub1, ucSub2...).

UserControls ucSub1 - ucSub6 contain various controls, among them...a ComboBox. Each combobox needs to be populated via a DataTable utilizing the "DisplayMember" and "ValueMember" properties.

uc1 is created dynamically and can have up to 12 instances. Each instance is placed on a dynamically created TabPage of a TabControl.
12 uc's = 12 tabpages.

Each ucSub? has within it's code-behind the following statement:      Public Event LoadCombos()

At this point, uc1 has no code-behind code.

I'm pretty sure at some point I need to add an event using the "AddHandler" that will fire when a uc? control is created.

Here is my question:

What is the best way to code this so that when a uc? control is added, all the ComboBoxes of each ucSub? is populated?
BlakeMcKennaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
Can you not call the LoadCombos function after you create an instance of user control?
BlakeMcKennaAuthor Commented:
At what point should I load the comboboxes then?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Are the six "sub" UserControls created dynamically for each "parent" UserControl?...or are they placed statically at design-time thru the designer?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

BlakeMcKennaAuthor Commented:
They are placed statically at design-time.
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Then I think you're going about it backwards.  Instead of the subUCs raising an event that the mainUC subscribes to, why not just have the mainUC call a Public method in the subUCs telling them to load?  This way the mainUC could also pass any necessary information to the subUCs that they need to load the correct data...
BlakeMcKennaAuthor Commented:
Do you have an example of such a thing.

I only ask because I'm assuming that I would do something like this in the main UC:

Public Event LoadCombos()

But when I go to dynamically create the main UC, how do I reference the "LoadCombos()"?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I'm thinking NO events at all.

Maybe something more like:
Public Class MainUserControl

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        SubUserControl1.LoadCombo()
        SubUserControl2.LoadCombo()
        SubUserControl3.LoadCombo()
        SubUserControl4.LoadCombo()
        SubUserControl5.LoadCombo()
        SubUserControl6.LoadCombo()
    End Sub

End Class

Public Class SubUserControl

    Public Sub LoadCombo()
        ' ... load the ComboBox ...
    End Sub

End Class

Open in new window


Of course, that could be changed up to pass more information from the main usercontrol into the child usercontrol method.
BlakeMcKennaAuthor Commented:
I see where your going and it makes sense. However, where does the "LoadCombo()" method reside?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Inside the SubUserControl so it can access and load the ComboBox.

You may need to change up the MainUserControl so you can load the combos some time after the constructor:
Public Class MainUserControl

    Public Sub LoadCombos()
        SubUserControl1.LoadCombo()
        SubUserControl2.LoadCombo()
        SubUserControl3.LoadCombo()
        SubUserControl4.LoadCombo()
        SubUserControl5.LoadCombo()
        SubUserControl6.LoadCombo()
    End Sub

End Class

Open in new window


So then you could do:
Dim mainUC As New MainUserControl
' ... do some other stuff with "mainUC" ...
mainUC.LoadCombos()

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlakeMcKennaAuthor Commented:
Great idea Mike...that worked!

Thanks!
BlakeMcKennaAuthor Commented:
Mike,

One more thing. In the creation of my main UC, usually an custom event is created using the AddHandler. When doing so, is it required to declare a WithEvents object? For example...using my code below:

Public Class Form1
        Private WithEvents uc1_???     The question mark being whatever

                For Each itm In arrChannels
                    Dim tab As New TabPage
                    Dim uC1 As New userChannelSettings

                    uC1.Name = "ucChannel" & x + 1
                    uC1.mlChannelName.Text = itm
                    tab.Controls.Add(uC1)
                    uC1.ForeColor = tab.ForeColor

                    AddHandler uc1_????                       What kind of event should I add here

                    tab.Tag = itm
                    tab.Text = "Channel: " & itm.ToString
                    tab.Name = "tChannel" & x + 1
                    tabChannelList.Controls.Add(tab)
                Next

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
"WithEvents" should be used in conjuction with methods marked with the "Handles" keyword, which doesn't apply to your situation.  You don't need that declaration at all since you're declaring them locally in the method and wiring them up with AddHandler.
BlakeMcKennaAuthor Commented:
Got it!!!

Thanks for the clarification.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.