Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Accessing Dynamically Added User Controls

Accessing Dynamically Added User Controls

Scheduled Pinned Locked Moved C#
winformshelptutorialquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mikan23
    wrote on last edited by
    #1

    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?

    P 1 Reply Last reply
    0
    • M Mikan23

      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?

      P Offline
      P Offline
      Polis Pilavas
      wrote on last edited by
      #2

      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?

      M 1 Reply Last reply
      0
      • P Polis Pilavas

        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?

        M Offline
        M Offline
        Mikan23
        wrote on last edited by
        #3

        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

        P 1 Reply Last reply
        0
        • M Mikan23

          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

          P Offline
          P Offline
          Polis Pilavas
          wrote on last edited by
          #4

          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?

          M 1 Reply Last reply
          0
          • P Polis Pilavas

            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?

            M Offline
            M Offline
            Mikan23
            wrote on last edited by
            #5

            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!

            P M 2 Replies Last reply
            0
            • M Mikan23

              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!

              P Offline
              P Offline
              Polis Pilavas
              wrote on last edited by
              #6

              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?

              M 1 Reply Last reply
              0
              • P Polis Pilavas

                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?

                M Offline
                M Offline
                Mikan23
                wrote on last edited by
                #7

                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?

                1 Reply Last reply
                0
                • M Mikan23

                  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!

                  M Offline
                  M Offline
                  Marc 0
                  wrote on last edited by
                  #8

                  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

                  M 1 Reply Last reply
                  0
                  • M Marc 0

                    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

                    M Offline
                    M Offline
                    Mikan23
                    wrote on last edited by
                    #9

                    That worked great! Thank you both! *sigh of relief*

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups