Question

I have a tableLayoutPanel (size of 10x10 columns & rows added from the toolbox) and I have added single Panel into every cells so I could add two objects/components into a cell. Every cell contains a label and a button. The problem is I couldn't add the two components into a panel programatically. What should I do?

Here is my code:

private int[,] grid;
private Button[,] btn_grid;
private Label[,] lbl_grid;
private int timer = 0;
private Panel[,] pnl_grid;

        private bool createGrid()
        {
            Random rnd1 = new Random();
            grid = new int[width, height];
            pnl_grid = new Panel[width, height];
            btn_grid = new Button[width, height];
            lbl_grid = new Label[width, height];

            for (int x = 0; x <width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    btn_grid[x, y] = createButton(x, y);
                    lbl_grid[x, y] = createLables(x,y);
                    pnl_grid[x, y] = createPanels(x, y);

                    **//something's missing here to add the 2 components into a panel** 

                    tableLayoutPanel2.Controls.Add(pnl_grid[x,y]);


                }
            }}


private Button createButton(int gridX, int gridY)
{
    Button bttn = new Button();
    bttn.Text = "";
    bttn.Name = gridX.ToString() + " " + gridY.ToString();
    bttn.Size = new System.Drawing.Size(30, 30);
    Controls.AddRange(new System.Windows.Forms.Control[] { bttn, });
    bttn.Click += new System.EventHandler(bttnOnclick);
    //bttn.MouseClick += new System.Windows.Forms.MouseEventHandler(this.bttnOnRightClick);

    return bttn;
}

private Label createLables(int gridX, int gridY)
{
    Label lbl = new Label();
    lbl.Name = gridX.ToString() + " " + gridY.ToString();
    lbl.Text = "0";
    lbl.Size = new System.Drawing.Size(30, 30);
    //lbl.Font = new Font("Microsoft Sans Serif", 15.75f, lbl.Font.Style, lbl.Font.Unit);
    Controls.AddRange(new System.Windows.Forms.Control[] { lbl, });
    return lbl;
}

private Panel createPanels(int gridX, int gridY)
{
    Panel pnl = new Panel();
    pnl.Name = gridX.ToString() + " " + gridY.ToString();
    //pnl.Text = "0";
    pnl.Size = new System.Drawing.Size(30, 30);
    //lbl.Font = new Font("Microsoft Sans Serif", 15.75f, lbl.Font.Style, lbl.Font.Unit);
    Controls.AddRange(new System.Windows.Forms.Control[] { pnl, });
    return pnl;
}

Thank you for your appreciated attention and help!

Was it helpful?

Solution 2

You can remove the Controls.AddRange call from createLables, createButton createPanels method. Then you can modify your createpanels method as below

private Panel createPanels(int gridX, int gridY)
{
    Label lbl = lbl_grid[gridX,gridY];
    Button btn = btn_grid[gridX,gridY];
    Panel pnl = new Panel();
    pnl.Name = gridX.ToString() + " " + gridY.ToString();
    //pnl.Text = "0";
    pnl.Size = new System.Drawing.Size(30, 30);
    //lbl.Font = new Font("Microsoft Sans Serif", 15.75f, lbl.Font.Style, lbl.Font.Unit);
    pnl.Controls.AddRange(new System.Windows.Forms.Control[] { lbl,btn });
    lbl.Dock = DockStyle.Top;
    btn.Dock = DockStyle.Fill;
    return pnl;
}

Hope this helps

OTHER TIPS

Your code is fine until the last moment when you have to add the controls to the tableLayourPanel or to the panel. You just add the panel but not the button/label to the panel.

Thus, you have to options:

Adding button and label to the panel and the panel to the tableLayoutPanel (which seems the most logical one):

btn_grid[x, y] = createButton(x, y);
lbl_grid[x, y] = createLables(x,y);
pnl_grid[x, y] = createPanels(x, y);
pnl_grid[x, y].Controls.Add(btn_grid[x, y]);
pnl_grid[x, y].Controls.Add(lbl_grid[x, y]);
tableLayoutPanel2.Controls.Add(pnl_grid[x,y]);

or adding the three elements directly to the tableLayoutPanel:

btn_grid[x, y] = createButton(x, y);
lbl_grid[x, y] = createLables(x,y);
pnl_grid[x, y] = createPanels(x, y);
tableLayoutPanel2.Controls.Add(btn_grid[x,y]);
tableLayoutPanel2.Controls.Add(lbl_grid[x,y]);
tableLayoutPanel2.Controls.Add(pnl_grid[x,y]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top