tags:

views:

51

answers:

2

I'm looking for an efficient manner in which to handle (methods and events of controls) dynamically created tab pages with dynamically created controls in them (same interface per tab page).

I want to be able to identify what tab page, a button's Click event came from and such behavior.

Would I be set by using a generic collection list in which to store the tab pages in? Where and in what am I looking to hold the references for each individual control, along with their methods and events? Am I also looking to hold the references for each individual control in generic collections lists? I'm hoping to avoid the User Control TabPage inheritance method. Is that going to end up being the most efficient route I'm going to have to take?

+1  A: 

That's certainly possible, merely awkward. You can always get a reference to a control back from the Controls collection or the sender argument of an event. It's Parent property gives you the tab page. Etcetera.

Yes, a List<TabPage> would work. But also the TabControl.TabPages property.

Hans Passant
More on the TabControl.TabPages.Control property, well, one route I took was creating everything in a method (like the designer file), then adding the tab page to the tab control from within. I got a bit lost as to how I would identify from within the event handler which button sent the initial Click event. Can you elaborate more on how I could from within an event method identify the tab index from where it was initially fired off from?
Michael S.
The *sender* argument is the key. Cast it back to a button with (Button)sender.
Hans Passant
A: 

Perhaps I'm missing something, but assuming you know the hierarchy of the controls, on click you can cast the sender as a button (as Hans suggested), and get it's parent

if(MyTabPage == (TabPage)((Button)sender).Parent)

OR get the parent's parent

if(MyTabPage == (TabPage)((Button)sender).Parent.Parent)

or however far up the hierarchy you need to go to get to the TabPage to figure out which tab you're on.

DougJones