How do I manage events in dynamically loaded controls?

G

Guest

I have an application that I'm working on in VS 2005 Beta 2. The app has
several user controls (mostly, but not all, tab controls) that I intend to
dynamically load into a panel depending on the buttons pushed on the toolbar.
For example, if the button for my Employees module is pushed, I'll do
something like this:

With myForm.Panel1
.Controls.Clear()
.Controls.Add(New EmployeeTabControl)
.Controls(0).Dock = DockStyle.Fill
End With

This works great, but I'm running into problems trying to figure out how to
raise an event in my parent form from one of these dynamically loaded
controls.

For example, let's say that I have a user control called EmployeeMenu.
Clicking on "Add Employee" inside the EmployeeMenu raises an event, called
SectionChanged, in the main form that unloads the EmployeeMenu control and
loads AddEmployee. I'd envision the code looking something like this
(oversimplified a bit for clarity):

Public Sub MenuItemClicked() handles AddEmployee.Click
RaiseEvent DirectCast(Me.ParentForm, MyForm).SectionChanged(Me, New
EventArgs)
End Sub

Unfortunately, this doesn't work in the designer because the child control
doesn't have a parent form yet. Is there something simple here that I'm
missing? I'd like to be able to call events in the parent form from the
child controls, but have no idea what I'm doing wrong. Any help would be
appreciated.
 
R

recoil

Wouldn't exposing the ChangeTab method via a programmatic method that
anybody could call seem like a better solution. Then the Control would
need to trace its way up the control line to find a control of
myspecialtabcontrol. if it finds it get a reference to it and call this
public method,
 
R

Roger Bonine

Wouldn't exposing the ChangeTab method via a programmatic method that
anybody could call seem like a better solution. Then the Control would
need to trace its way up the control line to find a control of
myspecialtabcontrol. if it finds it get a reference to it and call this
public method,

Thanks - that did the trick! I guess I was making it more complicated than it
needed to be.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top