Avatar of BlakeMcKenna
Flag for United States of America asked on

Accessing public events within nested UserControls?


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?
Visual Basic.NET

Avatar of undefined
Last Comment

8/22/2022 - Mon
Nasir Razzaq

Can you not call the LoadCombos function after you create an instance of user control?

At what point should I load the comboboxes then?
Mike Tomlinson

Are the six "sub" UserControls created dynamically for each "parent" UserControl?...or are they placed statically at design-time thru the designer?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

They are placed statically at design-time.
Mike Tomlinson

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

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()"?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mike Tomlinson

I'm thinking NO events at all.

Maybe something more like:
Public Class MainUserControl

    Public Sub New()

        ' This call is required by the designer.

        ' Add any initialization after the InitializeComponent() call.
    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.

I see where your going and it makes sense. However, where does the "LoadCombo()" method reside?
Mike Tomlinson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Great idea Mike...that worked!

Your help has saved me hundreds of hours of internet surfing.


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
                    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

Open in new window

Mike Tomlinson

"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.

Got it!!!

Thanks for the clarification.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.