Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag 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?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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


At what point should I load the comboboxes then?
Are the six "sub" UserControls created dynamically for each "parent" UserControl?...or are they placed statically at design-time thru the designer?
They are placed statically at design-time.
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()"?
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?
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great idea Mike...that worked!


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

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