简体   繁体   中英

Adding windows form controls through code

I'm making a server manager and it needs to be able to add and get rid of servers. I want it to have a pretty interface so i've decided to the server displayed on a picturebox that contains 4 labels that print out the different stats of the server. My problem is that let's say the user of the program has 500 servers, for me to implement that, the only thing I know how to do is create, define, and populate the picture boxes and labels in code. EX: (this is my header, but this is how I would implement the server "cards")

private PictureBox headerArea = new PictureBox();
    private PictureBox dropShadow = new PictureBox();
    private Label headerText = new Label();

    public Suite()
    {
        InitializeComponent();
    }

    private void Suite_Load(object sender, EventArgs e)
    {
        guiInit();
    }

    private void guiInit()
    {
        //Header Text
        this.headerText.AutoSize = true;
        this.headerText.BackColor = System.Drawing.Color.DarkSlateBlue;
        this.headerText.Font = new System.Drawing.Font("Roboto Lt", 22F);
        this.headerText.ForeColor = System.Drawing.SystemColors.ButtonFace;
        this.headerText.Location = new System.Drawing.Point(12, 10);
        this.headerText.Name = "headerText";
        this.headerText.Size = new System.Drawing.Size(178, 29);
        this.headerText.TabIndex = 0;
        this.headerText.Text = "Server Manager";
        this.Controls.Add(headerText);

        //Background GUI
        this.headerArea.BackColor = Color.DarkSlateBlue;
        this.headerArea.Size = new System.Drawing.Size(1920, 60);
        this.headerArea.Location = new System.Drawing.Point(0, 0);
        this.Controls.Add(headerArea);

        //Drop Shadow
        this.dropShadow.Image = 
        global::ServerManager.Properties.Resources.dropshadow2;
        this.dropShadow.BackgroundImageLayout = 
        System.Windows.Forms.ImageLayout.Stretch;
        this.dropShadow.Location = new System.Drawing.Point(0, 47);
        this.dropShadow.Name = "dropShadow";
        this.dropShadow.Size = new System.Drawing.Size(1920, 65);
        this.dropShadow.TabIndex = 0;
        this.dropShadow.TabStop = false;
        this.Controls.Add(dropShadow);
    }

The problem with this is that I would have 2500 lines of code if I need to have 500 servers displayed. Is there a way I can make a method that generates these forms or a better way to do this?

If you're looking to reduce the number of lines of code you have, you should use a method that takes in the parameters you wish to change. Here's some sample code that will get you started.

private void DoThis(string a, string b, string c, string d)
{
    //Create your labels
    //Set the label texts equal to each string.
}

The variables in the constructor (the parentheses) will also have to be included in your call as well. You would call it using

DoThis("text1", "text2", "text3", "text4")

I dont really understand your words.I'm not good at English. Do you want to create 500 controls in only one from?

    //set the control's initial location
    const int HEADERTEXTLOCATIONX = 0;
    const int HEADERTEXTLOCATIONY = 0;
    //set the control's offset value
    const int OFFSETX = 30;
    const int OFFSETY = 20;

    List<Label> headerTextList = new List<Label>();
    private void creatSubControls(int controlsCount)
    {
        for (int item = 0; item < controlsCount; item++)
        {
            guiInit(item);
        }
    }

    private void guiInit(int iControl)
    {
        Label headerText = new Label();
        int offsetX = iControl * OFFSETX;
        int offsetY = iControl * OFFSETY;
        //Header Text
        headerText.AutoSize = true;
        headerText.BackColor = System.Drawing.Color.DarkSlateBlue;
        headerText.Font = new System.Drawing.Font("Roboto Lt", 22F);
        headerText.ForeColor = System.Drawing.SystemColors.ButtonFace;
        headerText.Location = new System.Drawing.Point(HEADERTEXTLOCATIONX + offsetX, HEADERTEXTLOCATIONY + offsetY);
        headerText.Name = "headerText";
        headerText.Size = new System.Drawing.Size(178, 29);
        headerText.TabIndex = 0;
        headerText.Text = "Server Manager" + (iControl + 1).ToString();
        Controls.Add(headerText);
    }

Thanks for all the help, didn't really know what to type because of my lack of knowledge with Winforms. This is more or less what I was looking for:

for (int i = 0; i < 10; i++)
        {
            Button button = new Button();
            button.Left = left;
            button.Top = top;
            this.Controls.Add(button);
            top += button.Height + 2;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM