此代码如何优化减少工作量,C# C# C# C#

此代码如何优化减少工作量,C# C# C# C#

 public 主窗体()
        {
            InitializeComponent();
            this.Height = Screen.PrimaryScreen.Bounds.Height;
            this.Width = Screen.PrimaryScreen.Bounds.Width;
            pictureBox1.Width = this.Width;

            listBox1.Height = 100;
            listBox1.Width = 100;
            listBox2.Height = 100;
            listBox2.Width = 100;
            listBox3.Height = 100;
            listBox3.Width = 100;
            listBox4.Height = 100;
            listBox4.Width = 100;
            listBox5.Height = 100;
            listBox5.Width = 100;
            listBox6.Height = 100;
            listBox6.Width = 100;
            listBox7.Height = 100;
            listBox7.Width = 100;
            listBox8.Height = 100;
            listBox8.Width = 100;
            listBox9.Height = 100;
            listBox9.Width = 100;
            listBox10.Height = 100;
            listBox10.Width = 100;
            listBox11.Height = 100;
            listBox11.Width = 100;
            listBox12.Height = 100;
            listBox12.Width = 100;
            listBox13.Height = 100;
            listBox13.Width = 100;
            listBox14.Height = 100;
            listBox14.Width = 100;
            listBox15.Height = 100;
            listBox15.Width = 100;
            listBox16.Height = 100;
            listBox16.Width = 100;
            listBox17.Height = 100;
            listBox17.Width = 100;
            listBox18.Height = 100;
            listBox18.Width = 100;
            listBox19.Height = 100;
            listBox19.Width = 100;
            listBox20.Height = 100;
            listBox20.Width = 100;
            listBox21.Height = 100;
            listBox21.Width = 100;
            listBox22.Height = 100;
            listBox22.Width = 100;
            listBox23.Height = 100;
            listBox23.Width = 100;
            listBox24.Height = 100;
            listBox24.Width = 100;
            listBox25.Height = 100;
            listBox25.Width = 100;
            listBox26.Height = 100;
            listBox26.Width = 100;
            listBox27.Height = 100;
            listBox27.Width = 100;
            listBox28.Height = 100;
            listBox28.Width = 100;
            listBox29.Height = 100;
            listBox29.Width = 100;
            listBox30.Height = 100;
            listBox30.Width = 100;
            listBox31.Height = 100;
            listBox31.Width = 100;
            listBox32.Height = 100;
            listBox32.Width = 100;
            listBox33.Height = 100;
            listBox33.Width = 100;
            listBox34.Height = 100;
            listBox34.Width = 100;
            listBox35.Height = 100;
            listBox35.Width = 100;
            listBox36.Height = 100;
            listBox36.Width = 100;
            listBox37.Height = 100;
            listBox37.Width = 100;
            listBox38.Height = 100;
            listBox38.Width = 100;
            listBox39.Height = 100;
            listBox39.Width = 100;
            listBox40.Height = 100;
            listBox40.Width = 100;
            listBox41.Height = 100;
            listBox41.Width = 100;
            listBox42.Height = 100;
            listBox42.Width = 100;
            listBox43.Height = 100;
            listBox43.Width = 100;
            listBox44.Height = 100;
            listBox44.Width = 100;
            listBox45.Height = 100;
            listBox45.Width = 100;
            listBox46.Height = 100;
            listBox46.Width = 100;
            listBox47.Height = 100;
            listBox47.Width = 100;
            listBox48.Height = 100;
            listBox48.Width = 100;
            listBox49.Height = 100;
            listBox49.Width = 100;
            listBox50.Height = 100;
            listBox50.Width = 100;
            listBox51.Height = 100;
            listBox51.Width = 100;
            listBox52.Height = 100;
            listBox52.Width = 100;
            listBox53.Height = 100;
            listBox53.Width = 100;
            listBox54.Height = 100;
            listBox54.Width = 100;
            listBox55.Height = 100;
            listBox55.Width = 100;
            listBox56.Height = 100;
            listBox56.Width = 100;
            listBox57.Height = 100;
            listBox57.Width = 100;
            listBox58.Height = 100;
            listBox58.Width = 100;
            listBox59.Height = 100;
            listBox59.Width = 100;
            listBox60.Height = 100;
            listBox60.Width = 100;
            listBox61.Height = 100;
            listBox61.Width = 100;
            listBox62.Height = 100;
            listBox62.Width = 100;
            listBox63.Height = 100;
            listBox63.Width = 100;
            listBox64.Height = 100;
            listBox64.Width = 100;
            listBox65.Height = 100;
            listBox65.Width = 100;
            listBox66.Height = 100;
            listBox66.Width = 100;
            listBox67.Height = 100;
            listBox67.Width = 100;
            listBox68.Height = 100;
            listBox68.Width = 100;
            listBox69.Height = 100;
            listBox69.Width = 100;
            listBox70.Height = 100;
            listBox70.Width = 100;
            listBox71.Height = 100;
            listBox71.Width = 100;
            listBox72.Height = 100;
            listBox72.Width = 100;
            listBox73.Height = 100;
            listBox73.Width = 100;
            listBox74.Height = 100;
            listBox74.Width = 100;
            listBox75.Height = 100;
            listBox75.Width = 100;
            listBox76.Height = 100;
            listBox76.Width = 100;
            listBox77.Height = 100;
            listBox77.Width = 100;
            listBox78.Height = 100;
            listBox78.Width = 100;
            listBox79.Height = 100;
            listBox79.Width = 100;
            listBox80.Height = 100;
            listBox80.Width = 100;
            listBox81.Height = 100;
            listBox81.Width = 100;
            listBox82.Height = 100;
            listBox82.Width = 100;
            listBox83.Height = 100;
            listBox83.Width = 100;
            listBox84.Height = 100;
            listBox84.Width = 100;
            listBox85.Height = 100;
            listBox85.Width = 100;
            listBox86.Height = 100;
            listBox86.Width = 100;
            listBox87.Height = 100;
            listBox87.Width = 100;
            listBox88.Height = 100;
            listBox88.Width = 100;
            listBox89.Height = 100;
            listBox89.Width = 100;
            listBox90.Height = 100;
            listBox90.Width = 100;
            listBox91.Height = 100;
            listBox91.Width = 100;
            listBox92.Height = 100;
            listBox92.Width = 100;
            listBox93.Height = 100;
            listBox93.Width = 100;
            listBox94.Height = 100;
            listBox94.Width = 100;
            listBox95.Height = 100;
            listBox95.Width = 100;
            listBox96.Height = 100;
            listBox96.Width = 100;
            listBox97.Height = 100;
            listBox97.Width = 100;
            listBox98.Height = 100;
            listBox98.Width = 100;
            listBox99.Height = 100;
            listBox99.Width = 100;







        }

加一个引用
using System.Text.RegularExpressions;

            InitializeComponent();
            foreach (Control item in this.Controls)
            {
                if (item is ListBox)
                {
                    ListBox lb = item as ListBox;
                    if(Regex.IsMatch(lb.Name, @"^listBox\d{1,2}$"))
                    {
                        lb.Height = 100;
                        lb.Width = 100;
                    }

                }
            }

VS亲测通过。

for (int i = 1; i <= 99; i++)
{
(this.Controls["listBox" + i.ToString()] as ListBox).Width = 100;
(this.Controls["listBox" + i.ToString()] as ListBox).Height = 100;
}
如果ListBox都是嵌套在pictureBox里面,就是
(this.pictureBox1.Controls["listBox" + i.ToString()] as ListBox).Width = 100;
(this.pictureBox1.Controls["listBox" + i.ToString()] as ListBox).Height = 100;

我觉得这个问题是来搞笑的,这么多的控件在前台直接创建好打开界面不耗时吗?
最好放在窗口Loaded之后采用异步的方式进行创建,否则用户体验不好,用一个列表管理下这些ListBox即可,代码第一个即可以,都是遍历UI控件,找到ListBox,修改宽高

布局什么的按照自己要求搞定

List listBoxes = new List();

    private void MainWindow_Load(object sender, EventArgs e)
    {
        Task.Factory.StartNew(() =>
        {
            ListBox box = null;
            for (int i = 1; i <= 100; i++)
            {
                box = new ListBox()
                {
                    Name = "listBox" + i,
                    Width = 100,
                    Height = 100
                };
                listBoxes.Add(box);
            }
        }).ContinueWith(s =>
        {
            this.BeginInvoke(new Action(() =>
            {
                this.Controls.AddRange(listBoxes.ToArray());
            }));
        });
    }

///
/// 添加要设置的控件至集合
///
///
IEnumerable GetControls()
{
yield return listBox1;
yield return listBox2;
yield return listBox3;
//.....
yield return listBox100;
}

    private void SetControlSize()
    {
        foreach (ListBox control in GetControls())
        {
            control.Size = new Size(100, 100);
        }
    }