Ash
Ash

Reputation: 2601

Problem with layout of control when dynamically creating winform and controls for the Form

I get this response from a web service call. Something like this

<Response>
<Control1 type = "DropdownList" value= "USA,UK,Sweden,UAE"/>
<Control2 type = "Textbox" value= "Contries"/>
<Control3 type = "Button" value= "None">
</Response>

Based on this I de-serialize it into List<Controls>.

Now I need to be able to dynamically create a winform based on these controls. My problem is the layout. I want to be able to create them nicely separated (If possible vertically aligned) in batches of lets say 5.So If I need 15 controls I have 3 columns and 5 rows. What would be best way to achieve this? I know that I can use the inbuilt positioning properties like top, width etc., but maybe someone out there has done something similar in a better way.

Upvotes: 1

Views: 1148

Answers (2)

Ash
Ash

Reputation: 2601

So basically I am gonna do something like this(might be useful for someone else)

Form op = new Form();
            FlowLayoutPanel panel = new FlowLayoutPanel();
            op.Controls.Add(panel);
            for (int i = 0; i < 10; i++)
            {
                Button b = new Button();
                b.Text = "Button" + i.ToString();
                panel.Controls.Add(b);

            }

Upvotes: 0

Renatas M.
Renatas M.

Reputation: 11820

I think you should use TableLayoutPanel. Also you can read best practices to use this control.

One benefit of using TableLayoutPanel from above article:

Layouts that will be modified or generated dynamically at run time, such as data entry forms that have user-customizable fields added or subtracted based on preferences.

Upvotes: 1

Related Questions