views:

1519

answers:

2

I have a page where I have to modify variables which are strings with pairs of values and labels. I was using a datagrid object but its not sufficient for whats required ( or eventually will not anyway ).

So I have a form which is a text label and textbox, and a flowpanel, and I'm trying to programmatically add instances of this form for each variable to the flowpanel, and Im getting nothing. Googling for the solution bring sup lots of video tutorials involving clicking on buttons in the UI designer and dropping them on flow panels, I want to do this programmatically however.

What is the 'correct' or 'standard' way of doing this.

A: 

The data (in pairs) sounds like it might fit better with a TableLayoutPanel, but the theory is the same; just call .Controls.Add(...) and it should work:

    FlowLayoutPanel panel = new FlowLayoutPanel();
    Form form = new Form();
    panel.Dock = DockStyle.Fill;
    form.Controls.Add(panel);

    for (int i = 0; i < 100; i++)
    {
        panel.Controls.Add(new TextBox());
    }

    Application.Run(form);

or with a TableLayoutPanel:

    TableLayoutPanel panel = new TableLayoutPanel();
    Form form = new Form();
    panel.Dock = DockStyle.Fill;
    panel.ColumnCount = 2;
    form.Controls.Add(panel);

    for (int i = 0; i < 100; i++)
    {
        panel.Controls.Add(new Label { Text = "label " + i });
        panel.Controls.Add(new TextBox { Text = "text " + i });
    }

Also - I wonder if a PropertyGrid would fit your needs better? This will handle all the "get value", "show value", "parse value", "store value" logic, and can be plugged with things like ICustomTypeDescriptor to allow dynamic properties.

Marc Gravell
A: 

To add instances of a form to a flowlayout panel, I do the following:

Form1 f1 = new Form1();
f1.TopLevel = false;
f1.Visible = true;
flowLayoutPanel1.Controls.add(f1);

Seems to work OK in my test code.

itsmatt