1
votes

I have a button which when pressed adds a UserControl into a panel. Essentially what i want is the next time you press it, it would add another exact same userControl but underneath the last one.

So far i have tried this:

  int count = 0;
  private void B_AddQuestion_Click(object sender, EventArgs e)
    {
        QuestionAdder QA = new QuestionAdder();
        i++;
        for (int j = 0; j < count; j++)
        {
            Panel.Controls.Add(QA);
            GBX_Title.Text = i.ToString(); //This was to test to see if its counting
        }
    }

essentially this is working but what i think is happening is that it's adding user controls on top of each other. I tried changing the top and left but to no avail.

Maybe if I check if there is a user control, then add the next one below it?

EDIT: I did test flowLayoutPanels but I can't use this, since I would be having more userControls which must go NextTo the last userControl

EDIT: My Mistake, you can very well do this by using flowLayoutPanel, setting flowDirection to TopDown and disabeling wrap contents, thanks!

1
Set the location for the new QA object to be below any previous one; or use a flowlayout panel bur Read How to Ask and take the tour - Ňɏssa Pøngjǣrdenlarp
@Plutonix I did test flowLayoutPanels but I can't use this, since I would be having more userControls which must go NextTo the last userControl - Flame
Please read How to Ask and take the tour - Ňɏssa Pøngjǣrdenlarp
When you want only top-down flow, you can simply use Panel instead of FlowLayoutPanel. To do so, set AutoScroll property of the Panel to true, then add user control instances to the panel and set Dock property of them to Top. Take a look at this example. - Reza Aghaei
@Flame - I am not sure I understand what do you mean. Can you please explain more specifically why couldn't you use a FlowLayoutPanel? - Sipo

1 Answers

0
votes

Instead of a regular Panel, you could use a FlowLayoutPanel and set its FlowDirection property to TopDown.