首页 > 解决方案 > 创建图片框的表格布局面板需要很长时间

问题描述

我正在创建这个图片框(66x66 像素)的表格布局面板,虽然在一台 PC 上完成的时间不到一秒,但在另一台 PC 上大约需要 8 秒(具有相同的功能......)。我必须在运行时创建图形,因为我只知道在运行时需要放置哪些图像。

这是功能:

 private void InitializeDrawerCells(int cabinetPartID, int drawerID)
 {
        PictureBox pb;


        try
        {              

            tblDrawerCells.RowStyles.Clear();
            tblDrawerCells.ColumnStyles.Clear();
            tblDrawerCells.Controls.Clear();

            tblDrawerCells.RowCount = 12;
            tblDrawerCells.ColumnCount = 16;
            for (int i = 0; i < tblDrawerCells.RowCount; i++)
            {
                RowStyle style = new RowStyle
                {
                    SizeType = SizeType.Absolute,
                    Height = tblDrawerCells.Height / tblDrawerCells.RowCount
                };
                tblDrawerCells.RowStyles.Add(style);
            }


            for (int i = 0; i < tblDrawerCells.ColumnCount; i++)
            {
                ColumnStyle style = new ColumnStyle
                {
                    SizeType = SizeType.Absolute,
                    Width = tblDrawerCells.Width / tblDrawerCells.ColumnCount
                };
                tblDrawerCells.ColumnStyles.Add(style);
            }

            for (int i = 0; i < tblDrawerCells.ColumnCount; i++)
            {
                for (int j = 0; j < tblDrawerCells.RowCount; j++)
                {
                    pb = new PictureBox();

                    if (_DrawerType == eDrawerType.Regular)
                    {
                        if (Scenraio == OpenCabinetScenario.Maintenance)
                        {
                            if (NarcoticsCabinetManager.CabinetParts[_CurrentCabPartID].Drawers[_DrawerNumber - 1].DrawerType == eDrawerType.Regular)
                                pb.BackgroundImage = ImageOn[tblDrawerCells.RowCount - j - 1][i];
                            else
                                pb.BackgroundImage = ImageReturnOff;

                        }
                        else
                            pb.BackgroundImage = ImageOff;
                    }
                    else
                        pb.BackgroundImage = global::NarcoticsStorageSystem2.Properties.Resources._11x6_Cells;
                    pb.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
                    // pb.Dock = System.Windows.Forms.DockStyle.Fill;
                    pb.Location = new System.Drawing.Point(0, 0);
                    pb.Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);
                    if (_DrawerType == eDrawerType.Regular)
                        pb.Size = new System.Drawing.Size(66, 66);
                    else if (_DrawerType == eDrawerType.Return)
                        pb.Size = new System.Drawing.Size(198, 66);
                    // if (_Scenraio == OpenCabinetScenario.FillItems)
                    // {
                    pb.Click += new System.EventHandler(this.pbCell_Click);

                    // }
                    pb.TabStop = false;

                    pb.Tag = new Tuple<int, int, int, int>(_DrawerNumber, tblDrawerCells.RowCount - j, i + 1, (int)ImageType.Off);

                    tblDrawerCells.Controls.Add(pb, i, j);
                }
            }

        }
        catch (Exception ex)
        {
           //add ex to log...
        }
        finally
        {


        }
    }

有任何想法吗?

标签: c#.net

解决方案


推荐阅读