Do I use the Paint event to control positions at runtime?

I am new to C # programming coming from VB6, so please be careful: P

I use panels to group controls (i.e. panels contain a text box, shortcuts, list, etc.), and then the panel is aligned at runtime, so that the controls are aligned in different resolutions. However, I am doing this from the Paint Paint (?) Procedure

i.e:.

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; } 

I want to ask: Is this the right way to do this?

The reason for this is that I'm not sure if this will affect performance as soon as the application runs on older systems (assuming that it will be used in XP with 2G RAM Pentium 4 HT or equivalent).

+4
source share
1 answer

No; this is definitely the wrong way to do this.
Paint can fire very often; you should do as little work as possible in it. (and you, of course, should not change the layout)

Instead, you should set the Anchor and Dock properties in the constructor so that all this happens automatically.

+6
source

All Articles