0

I have this Method that Generates a Panel on the form:

private void createPanels(int spacing)
        {
            pnl1.Location = new Point(0, spacing);
            pnl1.BorderStyle = BorderStyle.FixedSingle;
            pnl1.Size = new Size(100, 93);
            tabRotaBuild.Controls.Add( pnl1);

        }

I then call this method in form load:

   private void Form1_Load(object sender, EventArgs e)
    {
        createPanels(60);            
    }

What I want to do next is display this panel again (a new panel) under it with out clearing the existing one. I have a button that generates the new panel when clicked but when I click it the existing panel disappears when the new one is displayed.

This is the code behind the button:

int s = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            s += 100;
            createPanels(s);

        }
1
  • Instead of adding the new panel to a TabPage, try adding it to a FlowLayout control. Commented Mar 7, 2013 at 22:52

1 Answer 1

2

Check the method, you are just changing the location of the existing Panel and Control.Add method is setting the Parent property again, BorderStyle and Size have already been set to the same value:

private void createPanels(int spacing)
        {
            pnl1.Location = new Point(0, spacing); //notice here
            pnl1.BorderStyle = BorderStyle.FixedSingle;
            pnl1.Size = new Size(100, 93);
            tabRotaBuild.Controls.Add( pnl1);

        }

you need to create the new Panel add the pnl1 = new Panel(); within your method

private void createPanels(int spacing)
    {
        pnl1 = new Panel();
        pnl1.Location = new Point(0, spacing);
        pnl1.BorderStyle = BorderStyle.FixedSingle;
        pnl1.Size = new Size(100, 93);
        tabRotaBuild.Controls.Add( pnl1);

    }

or you could declare and instantiate the new panel within the method

Panel panel = new Panel();

Notice that you won't have the reference on the added panel. You could add each reference to the List<Panel> declared outside the method or something similar.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.