C # in a series of controls to dynamically create and initialize

Sometimes necessary to dynamically create a series of controls in the project, and initialize them, here I show a method applied to the project:

/// <Summary>
        /// creates and initializes all the picture control and label control
        /// </ Summary>
        Private void InitialAllControls ()
        {
            int iRow = 0;
            int iColumn = 0;
            // Get setting from the configuration file the number of rows and columns
            IF (ConfigurationManager.AppSettings [ "RowNum"]! = null && ConfigurationManager.AppSettings [ "the column"]! = null)
            {
                iRow = the int.Parse (ConfigurationManager.AppSettings [ "RowNum"]. the ToString ( ));
                iColumn = the int.Parse (ConfigurationManager.AppSettings [ "the Column"] the ToString ());.
            }
            String strRC = "R & lt" iRow.ToString + () + "C" + iColumn.ToString ();
            // Get the control parameter information from the profile success
            IF (GetControlConfigInfo (strRC))
            {
                // Get the pitch size of the control images of the current picture (X direction)
                int = BigPicDx iBigImgDx + BigPicSizeX;
                // Get the pitch size of the control images of the current picture (Y direction)
                int = + BigPicSizeY BigPicDy iBigImgDy;
                String lbName, pbbkName;

                for (int i = 1; i <= iRow; i++)
                {
                    for (int j = 1; j <= iColumn; j++)
                    {
                        lbName = string.Format("lbW{0}{1}", i, Convert.ToChar(j + 64));
                        pbbkName = string.Format("pbBK{0}{1}", i, Convert.ToChar(j + 64));
                        PictureBox pbbk = new PictureBox();
                        pbbk.Name = pbbkName;
                        pbbk.Location = new Point(FirstPicTop + (j - 1) * iBigImgDx, FirstPicLeft + (i - 1) * iBigImgDy);
                        pbbk.Size = new Size(BigPicSizeX, BigPicSizeY);
                        pbbk.BackColor = Color.Transparent;
                        pbbk.Visible = true;

                        The Label new new LB = the Label ();
                        lb.Name = lbName;
                        lb.Location = new new Point (pbbk.Location.X + LabelRx, pbbk.Location.Y LabelRy +);
                        lb.Height = LabelSizeY; // configuration
                        lb.Width = LabelSizeX; // configuration
                        lb.TextAlign = ContentAlignment.MiddleCenter;
                        lb.Visible = to true;
                        // add to the picture control
                        this.Controls.Add (pbbk);
                        this.Controls.Add (LB);
                        // place the label control in the top picture
                        lb.BringToFront ();
                    }
                }
            }
        }

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/09/20/2182384.html

Guess you like

Origin blog.csdn.net/weixin_33908217/article/details/93053936