c# – 我是否在运行时使用Paint事件来定位控件?

我是来自VB6的C#编程的新手,所以请温柔:P

我一直在使用Panel来分组控件(即Panel包含Textbox,Labels,Listview等),然后让面板在运行时对齐,以便控件以不同的分辨率对齐.但是,我是通过Panel的Paint例程(?)来做到这一点的.

即:

private void pnlTop_Paint(object sender, PaintEventArgs e)
    {
        btnExit.Location                = new Point(this.Width - (this.Left + lblTitleMain.Left + btnExit.Width), 10);
        btnMinimize.Location            = new Point(this.Width - (this.Left + lblTitleMain.Left + (btnExit.Width * 2)), 10);
        btnSettings.Location            = new Point(this.Width - (this.Left + lblTitleMain.Left + (btnExit.Width * 2 + btnExit.Width)), 10);

        lblTitleMain.Left               = (((this.ClientSize.Width - lblTitleMain.Width) / 2) / 2) / 2;
        lblTitleMain.Top                = btnExit.Top + lblTitleMain.Height;

        int intMenuY                    = lblTitleMain.Bottom + 5;
        lnkMenuSystem.Location          = new Point(lblTitleMain.Left + 3, intMenuY);
        lnkMenuDeployment.Location      = new Point(lnkMenuSystem.Right + 50, intMenuY);
        lnkMenuTables.Location          = new Point(lnkMenuDeployment.Right + 50, intMenuY);
        lnkMenuTCP.Location             = new Point(lnkMenuTables.Right + 50, intMenuY);
        lnkMenuDCM.Location             = new Point(lnkMenuTCP.Right + 50, intMenuY);
        lnkMenuProcessData.Location     = new Point(lnkMenuDCM.Right + 50, intMenuY);
        lnkMenuGenerateReports.Location = new Point(lnkMenuProcessData.Right + 50, intMenuY);

        lineMenuButtom.StartPoint       = new Point((((this.ClientSize.Width - lblTitleMain.Width) / 2) / 2) / 2, lnkMenuSystem.Top + lnkMenuSystem.Height + 10);
        lineMenuButtom.EndPoint         = new Point(this.Width - (this.Left + lblTitleMain.Left), lnkMenuSystem.Top + lnkMenuSystem.Height + 10);

        lnkMenuErrorMessage.Location    = new Point(lnkMenuSystem.Left, lineMenuButtom.Y1+5);
        lnkMessageWelcome.Location      = new Point(lineMenuButtom.X2 - lnkMessageWelcome.Width, lineMenuButtom.Y2 + 5);
        GlobalVariables.intGeneralLeft  = lineMenuButtom.StartPoint.X;
        GlobalVariables.intGeneralWidth = lineMenuButtom.X2;
    }

我想问的是:这是正确的方法吗?

原因是因为我不确定一旦应用程序在旧系统上运行,这是否会影响性能(假设它将在XP中使用2g RAM Pentium 4 HT或同等系统).

解决方法:

没有;这当然是错误的做法.
油漆可以经常燃烧;你应该做尽可能少的工作. (你当然不应该修改布局)

相反,您应该在设计器中设置Anchor和Dock属性,以便它们全部自动发生.

上一篇:c# – 验证控制


下一篇:c# – 单击按钮时动态添加新文本框