locked
How do keep track of panels created dynamically and add controls to them? RRS feed

  • Question

  • Hi All,

    I need to know how to add multiple panels dynamically and keep track of each panel. When i click "next" button panel must be incremented with next panel shown and other panels must be hidden and when i click "back" button previous panel must be shown and others must be hidden. How can i do this. Im using the code given below to create mutltiple panels overlapping eachother liek a wizard.

    private void CreatePanel(String panelname)
          
        {
    
          Panel newpanels = new Panel();
          panel3.Controls.Add(newpanels);      
          newpanels.Name = panelname;  
          newpanels.BorderStyle = BorderStyle.Fixed3D;
          newpanels.Dock = DockStyle.Fill;
          newpanels.AutoScroll = true;
          
        }

    What else should be added? i need code for Next and Back buttons. Once the last panel is reached another button "Completed" has to be generated on the last panel. I need to create checkboxes dynamicaally on ech panel, so i need panel referance so that i can create checkboxes on each panel and once selection is made i must be able to move to next panel or able to move to previous panel.

    Urgent help needed.

    Thanks in Advance

    Sahana

     

    Tuesday, May 4, 2010 7:42 AM

Answers

  •  

    Hi !

    I've made this sample code. I used 1 Panel as main panel, which will contain other Panels and 2 buttons to move Back/Next.

    Its pretty much self-explanatory. Ask if you have any doubts.

     

      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
          panel1.ControlAdded += new ControlEventHandler(panel1_ControlAdded);
        }
    
        void panel1_ControlAdded(object sender, ControlEventArgs e)
        {
          //this method will be executed everytime New control is added to Panel1
          //where Panel1 is MAIN panel that would contain other panels
          //Access current panel here using currentPanel integer.
          switch (currentpanel)
          {
            case 0:
              CheckBox c = new CheckBox();
              c.Location = new Point(10, 50);
              c.Text = "checkbox" + currentpanel.ToString();
              //panels[currentpanel] would give you reference to the currently
              //shown panel
              panels[currentpanel].Controls.Add(c);
              break;
            case 1:
              Button b = new Button();
              b.Location = new Point(10, 50);
              b.Text = "button" + currentpanel.ToString();
              panels[currentpanel].Controls.Add(b);
              break;
            
           }
          
        }
    
        private List<Panel> panels = new List<Panel>();
        int currentpanel = 0;
    
        private void Form1_Load(object sender, EventArgs e)
        {
          for (int i = 0; i < 5; i++)
          {
            //my sample list of 5 panels,
            //each containing 1 label with different text
            Panel p = new Panel();
            Label l = new Label();
            l.Text = @"Panel" + i.ToString();
            l.Location=new Point(10, 10);
            p.Controls.Add(l);
            panels.Add(p);
          }
          panel1.Controls.Add(panels[currentpanel]); //set first panel
        }    
    
        private void button1_Click(object sender, EventArgs e)
        {
          //previous panel
          if(currentpanel > 0)
          {
            panel1.Controls.Clear();
            panel1.Controls.Add(panels[--currentpanel]);
          }
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
          //next panel
          if (currentpanel < panels.Count - 1)
          {
            panel1.Controls.Clear();
            panel1.Controls.Add(panels[++currentpanel]);
          }
          else
          {
            //Generate Completed Button here
          }
    
        }
    
    
      }


    Thanks

    "Feel the Force !"  | My blog
    • Proposed as answer by Vladimir.Ilic Wednesday, May 5, 2010 7:23 PM
    • Marked as answer by Helen Zhou Tuesday, May 11, 2010 2:25 AM
    Tuesday, May 4, 2010 9:17 AM

All replies

  •  

    Hi !

    I've made this sample code. I used 1 Panel as main panel, which will contain other Panels and 2 buttons to move Back/Next.

    Its pretty much self-explanatory. Ask if you have any doubts.

     

      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
          panel1.ControlAdded += new ControlEventHandler(panel1_ControlAdded);
        }
    
        void panel1_ControlAdded(object sender, ControlEventArgs e)
        {
          //this method will be executed everytime New control is added to Panel1
          //where Panel1 is MAIN panel that would contain other panels
          //Access current panel here using currentPanel integer.
          switch (currentpanel)
          {
            case 0:
              CheckBox c = new CheckBox();
              c.Location = new Point(10, 50);
              c.Text = "checkbox" + currentpanel.ToString();
              //panels[currentpanel] would give you reference to the currently
              //shown panel
              panels[currentpanel].Controls.Add(c);
              break;
            case 1:
              Button b = new Button();
              b.Location = new Point(10, 50);
              b.Text = "button" + currentpanel.ToString();
              panels[currentpanel].Controls.Add(b);
              break;
            
           }
          
        }
    
        private List<Panel> panels = new List<Panel>();
        int currentpanel = 0;
    
        private void Form1_Load(object sender, EventArgs e)
        {
          for (int i = 0; i < 5; i++)
          {
            //my sample list of 5 panels,
            //each containing 1 label with different text
            Panel p = new Panel();
            Label l = new Label();
            l.Text = @"Panel" + i.ToString();
            l.Location=new Point(10, 10);
            p.Controls.Add(l);
            panels.Add(p);
          }
          panel1.Controls.Add(panels[currentpanel]); //set first panel
        }    
    
        private void button1_Click(object sender, EventArgs e)
        {
          //previous panel
          if(currentpanel > 0)
          {
            panel1.Controls.Clear();
            panel1.Controls.Add(panels[--currentpanel]);
          }
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
          //next panel
          if (currentpanel < panels.Count - 1)
          {
            panel1.Controls.Clear();
            panel1.Controls.Add(panels[++currentpanel]);
          }
          else
          {
            //Generate Completed Button here
          }
    
        }
    
    
      }


    Thanks

    "Feel the Force !"  | My blog
    • Proposed as answer by Vladimir.Ilic Wednesday, May 5, 2010 7:23 PM
    • Marked as answer by Helen Zhou Tuesday, May 11, 2010 2:25 AM
    Tuesday, May 4, 2010 9:17 AM
  • Hi Omie,

    Say i have 3 panels right now that i have dragged from toolbox. I have panel 3 in which i need other dynamically generated panels to appear.So here Panel 3 acts as a parent panel or the main panel. I have next and back buttons in panel 2 which is docked bottom.I am generating these panels depending on the xml file. I am generating the checkboxes also depending on the xml file. So i cannot hardcode the text or label here wverything is dynamic. Now how do i add panels and keep referances so that i can add controls to them dynamically. I do not want any dependancies.Say i want all the panels to be generated first and then add checkboxes to the first panel and then 2nd panel ,3rd panel and so on. I need to store the user selections made in each panel also somewhere so that i can generate a file at the end when user clicks complete button.

    Thanks in advance

    Sahana

    Tuesday, May 4, 2010 10:01 AM
  • that button, checkbox thing was to illustrate where and when to create controls dynamically. You're supposed to replace it with your code that is reading xml file and creating your own controls.

    if you'd read comments above switch() block, I showed how you could get reference of current panel.

    I dont know how can I make it simpler.


    Thanks

    "Feel the Force !"  | My blog
    Tuesday, May 4, 2010 1:26 PM