0

I want to center label depending of form width so I do:

var formWidth = StatisticsProjectForm.ActiveForm.Width;

   var lblProjectTimeWidth = lblProjectTime.Width;

    lblProjectTime.Width = formWidth / 2 - lblProjectTimeWidth / 2;

But when I run program it just don't center to middle of Form, what am I doing wrong?

Pepe
  • 125
  • 7

2 Answers2

3
var formWidth = StatisticsProjectForm.ActiveForm.Width;

   var lblProjectTimeWidth = lblProjectTime.Width;

    lblProjectTime.Left = formWidth / 2 - lblProjectTimeWidth / 2;
Yves Israel
  • 408
  • 3
  • 5
  • 1
    Also: Don't forget to clear all anchors so the control stays centered even when the form is resized! – TaW Aug 06 '18 at 18:01
1

use following code for setting label to middle and center of form I used Label1 and did center to middle of form.

public Form2()
    {
        InitializeComponent();
        this.Load += new EventHandler(centerLabel);
        this.Resize += new EventHandler(centerLabel);
    }
    protected void centerLabel(object sender,EventArgs e)
    {
        this.label1.Location = new Point((this.Width / 2)-label1.Width, (this.Height / 2)-label1.Height);
    }

you also add events for resize and load or whatever you want to do with centerLabel Method.

Arslan Ali
  • 450
  • 4
  • 12