Accessing public events within nested UserControls?

BlakeMcKenna
BlakeMcKenna used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2014

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

Author

Commented:
At what point should I load the comboboxes then?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Are the six "sub" UserControls created dynamically for each "parent" UserControl?...or are they placed statically at design-time thru the designer?
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Author

Commented:
They are placed statically at design-time.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

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

Author

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, Digital Design, and Mathematics Teacher
Top Expert 2009

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

Author

Commented:
I see where your going and it makes sense. However, where does the "LoadCombo()" method reside?
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
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

Author

Commented:
Great idea Mike...that worked!

Thanks!

Author

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, Digital Design, and Mathematics Teacher
Top Expert 2009

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

Author

Commented:
Got it!!!

Thanks for the clarification.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial