0

I am trying to design a winform. The initial design has several rows, each with a label, a number entry box input, and an output text box. I have got the first row working.

Now I want to programmatically duplicate this row, several times. I have put the components into a panel (an container, with no frame). I was then hoping to clone this panel, and its content, set a now location, and label text for each.

I could probably workout how to do it all programmatically. However I want to layout the header and the first row, with the layout tool.

Am I going about this in a good way? What am I missing.

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52

2 Answers2

0

Yes, you can proceed with this idea by cloning the main control and all of its child controls.

Here is a sample code for that:

namespace CloneAControl
{
    public partial class Form1 : Form
    {
        private int yValue = 50;

        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Panel ctrl = panel1.Clone();
            //Control ctrl = ControlFactory.CloneCtrl(this.panel1);

            this.Controls.Add(ctrl);
            //ctrl.Text = "created by clone";
            ctrl.SetBounds(ctrl.Bounds.X, ctrl.Bounds.Y + yValue,
                           ctrl.Bounds.Width, ctrl.Bounds.Height);
            yValue = yValue + 50;
            ctrl.BackColor = Color.Red;
            ctrl.Show();
        }
    }

    public static class ControlExtensions
    {
        public static T Clone<T>(this T controlToClone)
            where T : Control
        {
            PropertyInfo[] controlProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

            T instance = Activator.CreateInstance<T>();

            foreach (PropertyInfo propInfo in controlProperties)
            {
                if (propInfo.CanWrite)
                {
                    if (propInfo.Name != "WindowTarget")
                        propInfo.SetValue(instance, propInfo.GetValue(controlToClone, null), null);
                }
            }

            return instance;
        }
    }
}
Mousa Alfhaily
  • 1,260
  • 3
  • 20
  • 38
A. Gopal Reddy
  • 370
  • 1
  • 3
  • 16
0

You have 2 approaches for this :

  1. The naive approach :

Which is to create the panels in the design view and hide them except the first one, and programmatically you can show and hide them as you want.

-Disatvantages :

  1. The number of panels you want to show is going to be fixed (limited).

  2. You are not using the power of OOP at all.



  1. The better approach :

Which is to create an array of the panels (rows) in your case, to store the panels and all of its child controls.

-Disatvantage :

  1. The number of panels you want to show is still going to be fixed (limited).

-To solve this problem :

Look for the terms Data structures, Generics.

List would be good choice in your case.

Hope that was useful.

Mousa Alfhaily
  • 1,260
  • 3
  • 20
  • 38