tags:

views:

791

answers:

1

I want to create a kind of 4 x 3 matrix with textboxes and checkboxes. Whether the element is checkbox or textbox depends upon the values in database.I want it to be dynamic. What is the best way to start?

// something like this but I need to fill in each elements of the matrix...
  private void CreateSpecificControl(string requestedType)
        {
                if (requestedType == "CheckBox")
                {

                    CheckBox control1 = new CheckBox();

                    control1.Click += new EventHandler(chk_CheckedChanged);

                   //TableLayout panel
                    layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));
                    layout.Controls.Add(control1);

                }
                else
                {
                    Label control1 = new Label();

                    control1.Text = "Not a checkbox";
                    layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));
                    layout.Controls.Add(control1);

                }

        }
+1  A: 

Use a usercontrol. See this tutorial.

At run time you can change the contents of the User control. There's a Controls collection in each user control that you can add or remove elements from. For example if you want to add check boxes just do somethign like this:

myUserControl.Controls.Add(new CheckBox());

Similarly elements can be removed from this collection, thus achieving a dynamic behaviour.

Frederick
What does a user control achieve?
benPearce