Accessing Dynamically Added User Controls
-
On my form (MainWindow) I have a TabControl called MonitorTabs. I programmatically add a tab to it for each user I want to monitor. Then I add a user control called Monitor that contains a group of controls including a timer (RefreshTimer) to that tab. From MainWindow, I call a function that needs to reset the interval on RefreshTimer on the currently selected tab. The number of RefreshTimers is dynamic, one for each User Control/Tab, and I don't know how to access any of them let alone the one that is currently selected. The timer is set to public, and still nothing shows up. Can anyone help?
-
On my form (MainWindow) I have a TabControl called MonitorTabs. I programmatically add a tab to it for each user I want to monitor. Then I add a user control called Monitor that contains a group of controls including a timer (RefreshTimer) to that tab. From MainWindow, I call a function that needs to reset the interval on RefreshTimer on the currently selected tab. The number of RefreshTimers is dynamic, one for each User Control/Tab, and I don't know how to access any of them let alone the one that is currently selected. The timer is set to public, and still nothing shows up. Can anyone help?
What you are asking is how to access the currently selected tab? If yes, the following line of code popups a MessageBox with the text of the currently selected tab:
MessageBox.Show(this.tabControl1.SelectedTab.Text);
If I understood otherwise, please try being a bit more specific with your question Regards, Polis Can you practice what you teach?
-
What you are asking is how to access the currently selected tab? If yes, the following line of code popups a MessageBox with the text of the currently selected tab:
MessageBox.Show(this.tabControl1.SelectedTab.Text);
If I understood otherwise, please try being a bit more specific with your question Regards, Polis Can you practice what you teach?
Sorry I wasn't clear enough. On each tab is a user control called Monitor. So if I have three tabs, then there are three Monitors. Monitor has various elements including a timer, a datagridview, a treeview, etc... since they are part of the same user control, they are all named the same. So for example, I want to set refreshTimer on Monitor on tab 2 (the currently selected tab). How would I access that refreshTimer. I have tried the following without success
MonitorTabs.SelectedTab.Monitor.refreshTimer MonitorTabs.SelectedTab.refreshTimer MonitorTabs.TabPages[MonitorTabs.SelectedIndex].Monitor.refreshTimer
Thanks -
Sorry I wasn't clear enough. On each tab is a user control called Monitor. So if I have three tabs, then there are three Monitors. Monitor has various elements including a timer, a datagridview, a treeview, etc... since they are part of the same user control, they are all named the same. So for example, I want to set refreshTimer on Monitor on tab 2 (the currently selected tab). How would I access that refreshTimer. I have tried the following without success
MonitorTabs.SelectedTab.Monitor.refreshTimer MonitorTabs.SelectedTab.refreshTimer MonitorTabs.TabPages[MonitorTabs.SelectedIndex].Monitor.refreshTimer
ThanksHmm... if I get it right, I would keep track of each Monitor's instance name on each tab (e.g. I would add "Monitor1" on tab1, "Monitor2" on tab2 and so on. Then, in order to access them, I would locate them by their names). Regards, Polis Can you practice what you teach?
-
Hmm... if I get it right, I would keep track of each Monitor's instance name on each tab (e.g. I would add "Monitor1" on tab1, "Monitor2" on tab2 and so on. Then, in order to access them, I would locate them by their names). Regards, Polis Can you practice what you teach?
That makes sense but I've never tried that before (somewhat new at this), could you demonstrate please? This is my current code.
TabPage newMonitorTab = new TabPage(TabTitle); MonitorTabs.TabPages.Add(newMonitorTab); MonitorTab monitor = new MonitorTab(); newMonitorTab.Controls.Add(monitor);
Thanks! -
That makes sense but I've never tried that before (somewhat new at this), could you demonstrate please? This is my current code.
TabPage newMonitorTab = new TabPage(TabTitle); MonitorTabs.TabPages.Add(newMonitorTab); MonitorTab monitor = new MonitorTab(); newMonitorTab.Controls.Add(monitor);
Thanks!You could write something like this:
// Add "monitor1" on tab1
TabPage tab1 = new TabPage("Tab 1");
MonitorTabs.TabPages.Add(tab1);MonitorTab monitor1 = new MonitorTab();
tab1.Controls.Add(monitor1);// Add "monitor2" on tab2
TabPage tab2 = new TabPage("Tab 2");
MonitorTabs.TabPages.Add(tab2);MonitorTab monitor2 = new MonitorTab();
tab2.Controls.Add(monitor2);// And so on...
This way, you know that tab1 has monitor1 on it, and tab2 has monitor2. So if the selectedTab is tab1 then you access monitor1 by writing something like
this.monitor1
Hope this makes sense. Regards, Polis Can you practice what you teach? -
You could write something like this:
// Add "monitor1" on tab1
TabPage tab1 = new TabPage("Tab 1");
MonitorTabs.TabPages.Add(tab1);MonitorTab monitor1 = new MonitorTab();
tab1.Controls.Add(monitor1);// Add "monitor2" on tab2
TabPage tab2 = new TabPage("Tab 2");
MonitorTabs.TabPages.Add(tab2);MonitorTab monitor2 = new MonitorTab();
tab2.Controls.Add(monitor2);// And so on...
This way, you know that tab1 has monitor1 on it, and tab2 has monitor2. So if the selectedTab is tab1 then you access monitor1 by writing something like
this.monitor1
Hope this makes sense. Regards, Polis Can you practice what you teach?Its getting closer, I have added them and can access them to a point using the following:
MonitorTab monitor = new MonitorTab(); monitor.Name = "Monitor" + MonitorTabs.TabPages.Count.ToString();
But I still can't access that control except using a foreach looping through the controls on the tab, which doesn't allow me to access the timer. Any suggestions? -
That makes sense but I've never tried that before (somewhat new at this), could you demonstrate please? This is my current code.
TabPage newMonitorTab = new TabPage(TabTitle); MonitorTabs.TabPages.Add(newMonitorTab); MonitorTab monitor = new MonitorTab(); newMonitorTab.Controls.Add(monitor);
Thanks!You can also make an hashtable, like [Tab] - [Monitor]:
private HashTable monitors = new HashTable();
MonitorTab GetMonitor(TabPage tab) {
return (MonitorTab) monitors(tab);
}void AddTab() {
TabPage newMonitorTab = new TabPage(TabTitle);
MonitorTabs.TabPages.Add(newMonitorTab);MonitorTab monitor = new MonitorTab();
newMonitorTab.Controls.Add(monitor);monitors.Add(newMonitorTab, monitor);
}// Then you can do:
MonitorTab selectedMonitor = GetMonitor(this.tabControl1.SelectedTab);Pompiedompiedom... ;)
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick
-
You can also make an hashtable, like [Tab] - [Monitor]:
private HashTable monitors = new HashTable();
MonitorTab GetMonitor(TabPage tab) {
return (MonitorTab) monitors(tab);
}void AddTab() {
TabPage newMonitorTab = new TabPage(TabTitle);
MonitorTabs.TabPages.Add(newMonitorTab);MonitorTab monitor = new MonitorTab();
newMonitorTab.Controls.Add(monitor);monitors.Add(newMonitorTab, monitor);
}// Then you can do:
MonitorTab selectedMonitor = GetMonitor(this.tabControl1.SelectedTab);Pompiedompiedom... ;)
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick