I am progmatically adding tabs to a TabControl
in my program. The contents of each tab are populated using a UserControl
:
private void process_addTab(string head, string type)
{
TabItem tab = new TabItem();
tab.Header = head;
switch (type)
{
case "trophy2": tab.Content = new Scoreboard2(); break;
case "trophy4": tab.Content = new Scoreboard4(); break;
case "text": tab.Content = new TextFields(); break;
case "talk": tab.Content = new LowerThirds(); break;
case "image": tab.Content = new ImageSelect(); break;
case "timer": tab.Content = new Timer(); break;
case "hitbox": tab.Content = new Hitbox(); break;
case "twitch": tab.Content = new Twitch(); break;
case "twitter": tab.Content = new Twitter(); break;
case "ustream": tab.Content = new Ustream(); break;
}
tabControl.Items.Add(tab);
}
This works great. However, the issue comes along when I remove tabs from the TabControl
. Each tab has a button in it to remove that specific tab from the control:
public static void RemoveClick(TabControl tabControl)
{
if (MessageBox.Show("Are you sure you wish to remove this tab?", "Remove Tab",
MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
{
TabItem item = (TabItem)tabControl.SelectedItem;
tabControl.Items.Remove(tabControl.SelectedItem);
}
}
This also seems to work well in that the tab is removed. However, it's a bit deceiving. In a few of the controls, I have timed functions running off a DispatcherTimer
. For instance, the Twitch control has a timer within the control that polls the Twitch API every 30 seconds to get channel information. If I remove the tab, the timer still continues to run; even though it shouldn't exist anymore.
Any idea how to fix this? Pretend I don't know much about C#; because I don't.