Question

I am using C# in VS 2010. I created a custom panel and would like to add this custom panel 9 times so I created a loop to add a copy of the panel 9 times at equal distance from each other. Each panel will have its own text and image. All I'm getting though is a single panel. Any insight would be appreciated

public partial class Form1 : Form
{
    int index = 0;
    List<CutePanel.CustomPanel> MenuItems = new List<CutePanel.CustomPanel>(); 
    public Form1()
    {
        InitializeComponent();

        for (int i = 0; i < 9; i++)
        {
            this.cpTest.BackColor = System.Drawing.SystemColors.ActiveCaption;
            this.cpTest.LabelText = "My super click text";
            this.cpTest.Location = new System.Drawing.Point(12, 12+(64*i));
            this.cpTest.Name = "cpTest";
            this.cpTest.Size = new System.Drawing.Size(344, 58);
            this.cpTest.SuperClick = null;
            this.cpTest.TabIndex = 6;
        }

        cpTest.MouseClick += new MouseEventHandler(cpTest_MouseClick);
        cpTest.SuperClick += new EventHandler(cpTest_SuperClick);
        cpTest.LabelText = "This is my text.";
        MenuItems.Add(cpTest);

    }

    void cpTest_SuperClick(object sender, EventArgs e)
    {
        tcTest.SelectedIndex = index++ % 2;
    }

    void cpTest_MouseClick(object sender, MouseEventArgs e)
    {
        tcTest.SelectedIndex = index++ % 2;
    }

    private void customPanel3_MouseClick(object sender, MouseEventArgs e)
    {
        tcTest.SelectedIndex = index++ % 2;
    }



} 

Thanks.

Was it helpful?

Solution

You have to make a distinction between your panel class and panel objects, also called instances of this class. Think of the class as a template that serves in creating objects. These objects are created with the new keyword:

for (int i = 0; i < 9; i++)
{
    var cp = new CutePanel.CustomPanel();
    cp.BackColor = System.Drawing.SystemColors.ActiveCaption;
    cp.LabelText = "My super click text";
    cp.Location = new System.Drawing.Point(12, 12+(64*i));
    cp.Name = "cpTest" + i;
    cp.Size = new System.Drawing.Size(344, 58);
    cp.SuperClick = null;
    cp.TabIndex = 6;

    cp.MouseClick += new MouseEventHandler(cpTest_MouseClick);
    cp.SuperClick += new EventHandler(cpTest_SuperClick);

    cp.LabelText = "This is my text.";
    MenuItems.Add(cp);
}

You can also assign it values from the existing panel:

cp.BackColor = cpTest.BackColor;
cp.Size = cpTest.Size;
...

An elegant way of making a duplicate is to include a Clone method in your panel class

public class CustomPanel
{
    ...

    public CustomPanel Clone()
    {
        var cp = (CustomPanel)this.MemberwiseClone();
        cp.Parent = null; // It has not yet been added to the form.
        return cp;
    }
}

Then

for (int i = 0; i < 9; i++)
{
    CustomPanel cp = cpTest.Clone();

    // Now only change the differing properties
    cp.Location = new System.Drawing.Point(12, 12+(64*i));
    cp.Name = "cpTest" + i;
    cp.TabIndex += i + 1;

    MenuItems.Add(cp);
}

but attention: If the cloned control is a container control containing other controls, you must clones those recursively as well. I.e., you must perform a deep clone! Creating new controls as shown in my first code snippet is safer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top