How to react to Events from Dynamically created TabPages in a TabControl

I have a tab control. For each new Customer selected a tabpage gets added. So, essentially the controls are the same on each tab page.  I have generic event handlers for each of the controls. Since the tabpages are dynamically added..how do I respond to events raised by each tab. (since the event handlers are the same for a control in each of the tab pages, how do I figure out which tabpage/Customer invoked the event handler and access the other controls in the tabpage.)

Any help is appreciated. Thanks.


Answer this question

How to react to Events from Dynamically created TabPages in a TabControl

  • Guber

    Since the event is fired by a control on the tabpage, the sender is the control. I tried accessing the container of the control to then use that to cast to a tabpage and access all the other controls on the tabpage, but I got an null reference exception.

    I am currently trying to use the selectedtab property to tell me what tabpage it is and then accessing the controls on the page.

    Any suggestions/solutions are welcome.

  • Brian Fink

    The sender object of the event handler method should tell you which page fired the event. You can cast it to a TabPage and check its properties, maybe the name.
  • Pramod_SN

    Actually, sender would tell you which control fired the event. This is ok if you clicked on a tabpage, but if you clicked on a control in a tabpage you can get it's parent tabpage as follows:

      Dim ClickedControl as Control = DirectCast(sender,Control)
      Dim ControlsTabPage as TabPage = DirectCast(ClickedControl.Parent,TabPage)

    Since the TabPage must be selected in order for you to click on one of it's controls (unless you are mimicking a Click through code), you could just use:
      
      CustomerTabControl.SelectedTab

  • How to react to Events from Dynamically created TabPages in a TabControl