Mr.Noob Mr.Noob - 23 days ago 6
C# Question

How to create windows forms dialog with dynamic number of buttons?

I need to create a sort of a dialog box or something like a popup screen. I have this array of items and then I need to create a buttons for each of them on the dialogbox so that i could navigate with a button click.

Whats the best way to do it in C#? can someone guide me on this please

Answer

If you are using , then place FlowLayoutPanel on your form. Then add all controls to it at runtime.

foreach(var item in items)
{
   Button button = new Button();
   // setup button properties
   // subscribe to events
   flowLayoutPanel.Controls.Add(button);
}

FlowLayoutPanel will arrange your controls automatically.