Kann ich diesem Code kürzer gestalten ?



  • Hallo,

    ich hab da einen Code geschrieben. Letztendlich geht es darum ein TableLayout zu befüllen. Dazu schreib ich in jede Zeile dasselbe Panel. In der for Schleife mach ich grundsätzlich mal immer dasselbe. Jetzt die Frage kann ich den Code irgendwie verkürzen ??

    const int NUMBER_ROWS = 1000;
                for (int i = 1; i < NUMBER_ROWS;i++)
                {
                    // 
                    // panel1
                    // 
                    Panel panelLamps = new System.Windows.Forms.Panel();
    
                    // 
                    // label111
                    // 
                    Label label111 = new System.Windows.Forms.Label();
                    label111.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label111.Location = new System.Drawing.Point(3, 0);
                    label111.Name = "label11";
                    label111.Size = new System.Drawing.Size(115, 34);
                    label111.Text = "Operation Mode:";
                    label111.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    
                    // 
                    // pictureBox77
                    // 
                    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
                    PictureBox pictureBox77 = new System.Windows.Forms.PictureBox();
                    pictureBox77.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox7.Image")));
                    pictureBox77.Location = new System.Drawing.Point(115, 0);
                    pictureBox77.Name = "OpMode";
                    pictureBox77.Size = new System.Drawing.Size(29, 34);
                    pictureBox77.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
    
                    // 
                    // pictureBox44
                    // 
                    PictureBox pictureBox44 = new System.Windows.Forms.PictureBox();
                    pictureBox44.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox4.Image")));
                    pictureBox44.Location = new System.Drawing.Point(680, 0);
                    pictureBox44.Name = "CommandOpenUnlock";
                    pictureBox44.Size = new System.Drawing.Size(29, 34);
                    pictureBox44.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
                    pictureBox44.Click += new System.EventHandler(this.pictureBox4_Click);
    
                    // 
                    // label44
                    // 
                    Label label44 = new System.Windows.Forms.Label();
                    label44.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label44.Location = new System.Drawing.Point(518, 0);
                    label44.Name = "label4";
                    label44.Size = new System.Drawing.Size(166, 34);
                    label44.Text = "Command Open Unlock:";
                    label44.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    
                    // 
                    // pictureBox55
                    // 
                    PictureBox pictureBox55 = new System.Windows.Forms.PictureBox();
                    pictureBox55.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox5.Image")));
                    pictureBox55.Location = new System.Drawing.Point(457, 0);
                    pictureBox55.Name = "CommandClose";
                    pictureBox55.Size = new System.Drawing.Size(29, 34);
                    pictureBox55.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
    
                    // 
                    // label99
                    // 
                    Label label99 = new System.Windows.Forms.Label();
                    label99.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label99.Location = new System.Drawing.Point(329, 0);
                    label99.Name = "label9";
                    label99.Size = new System.Drawing.Size(122, 34);
                    label99.Text = "Command Close:";
                    label99.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    
                    // 
                    // pictureBox66
                    // 
                    PictureBox pictureBox66 = new System.Windows.Forms.PictureBox();
                    pictureBox66.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox6.Image")));
                    pictureBox66.Location = new System.Drawing.Point(273, 0);
                    pictureBox66.Name = "AreaSafe";
                    pictureBox66.Size = new System.Drawing.Size(29, 34);
                    pictureBox66.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
    
                    // 
                    // label101
                    // 
                    Label label101 = new System.Windows.Forms.Label();
                    label101.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    label101.Location = new System.Drawing.Point(185, 0);
                    label101.Name = "label10";
                    label101.Size = new System.Drawing.Size(82, 34);
                    label101.TabIndex = 66;
                    label101.Text = "Area Safe:";
                    label101.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
    
                    //Füge Controls dem Panel hinzu
                    panelLamps.Name = "panel1";
                    panelLamps.Size = new System.Drawing.Size(1272, 34);
                    panelLamps.Controls.Add(pictureBox77);
                    panelLamps.Controls.Add(label111);
                    panelLamps.Controls.Add(pictureBox44);
                    panelLamps.Controls.Add(label44);
                    panelLamps.Controls.Add(pictureBox55);
                    panelLamps.Controls.Add(label99);
                    panelLamps.Controls.Add(pictureBox66);
                    panelLamps.Controls.Add(label101);
    
                    //Füge Panel einen TableLayoutPanel hinzu
                    panelTable.Controls.Add(panelLamps, 1, i);
                    panelTable.SetColumnSpan(panelLamps, 12);
    
                }
    


  • Das erste was ich dir empfehle ist ein UserControl zu erstellen, was die notwendigen Controls bereits besitzt.

    Das kürzt deinen Code extrem ein:

    const int NUMBER_ROWS = 1000;
    for (int i = 1; i < NUMBER_ROWS;i++)
    {
         MyUserControl panelLamps = new MyUserControl();
    
         panelTable.Controls.Add(panelLamps, 1, i);
         panelTable.SetColumnSpan(panelLamps, 12);
    }
    

    Was hältst du davon?



  • Ja das ist zumindest schon mal eine Möglichkeit 🙂



  • Du erstellst da z.B. mehrere Labels.
    Dabei ist relativ viel gleich.
    Also raus-factoren in eine CreateLabel Hilfsfunktion.
    Das selbe nochmal für PictureBox.

    UserControl wie inflames2k vorgeschlagen hat ist aber vermutlich besser. Den Code gibt's dann zwar immer noch in dieser Form, allerdings kümmert sich dann wieder der Forms Designer darum.


Log in to reply