Home > Enterprise >  How i create user panel using multiple panel
How i create user panel using multiple panel

Time:04-16

I want to create a panel listing everyone connected to a socket. So i create something like that for create the first panel and add another one with new pos (X: lastY 0 Y: lastY 88)

i used this example for do my code programmatically create panel and add picture boxes

        int numberOfConnected = 5;

        int panelSizeX = 776;
        int panelSizeY = 75;

        int lastPanelX = 0;
        int lastPanelY = 0;
        int firstPanelX = 0;
        int firstPanelY = 0;

        Panel panel = new Panel();
        //Retry create a panel and place it while i <= numberOfConnected
        for (int i = 0; i <= numberOfConnected; i  )
        {
            if(firstPanelX == 0 && firstPanelY == 0)
            {
                firstPanelX = 12;
                firstPanelY = 12;

                panel.Size = new System.Drawing.Size(panelSizeX, panelSizeY);
                panel.Location = new Point(firstPanelX, firstPanelY);
                panel.BackColor = new System.Drawing.Color(26, 26, 26); // Idk how to import a color so a error is here
            }
            else
            {
                panel.Size = new System.Drawing.Size(panelSizeX, panelSizeY);
                panel.Location = new Point(lastPanelX, lastPanelY   88); //Define new panel pos
            }
        }

CodePudding user response:

  • You never change lastPanelY in the loop.
  • for (int i = 0; i <= numberOfConnected; i ) looks like an error. Use < ?
  • You need to create a new panel each iteration.
  • You need to add the new panel to some parent control.
public partial class Form1 : Form
{
    Panel parent;
    public Form1()
    {
        InitializeComponent();

        parent = new Panel();
        parent.Dock = DockStyle.Fill;
        Controls.Add(parent);

        CreatePanels();
    }

    void CreatePanels()
    {
        int numberOfConnected = 5;

        var panelSize = new Size(776, 75);
        var panelPos = new Point(12, 12);
        int panelVertOffset = 88;

        for (int i = 0; i < numberOfConnected; i  )
        {
            Panel panel = new Panel();
            panel.Size = panelSize;
            panel.Location = panelPos;
            panel.BackColor = Color.FromArgb(26, 26, 26);
            parent.Controls.Add(panel);

            panelPos.Offset(0, panelVertOffset);
        }
    }
}

Depending on what you plan to do with these panels, you might want to use a custom control instead.

  •  Tags:  
  • c#
  • Related