maged rifaat maged rifaat - 1 year ago 124
C# Question

Winform width and height improper values

I am getting improper behavior in the size of the form,I set the values of the width and the height manually but the produced form is smaller than expected.

I made a simple form to illustrate my problem,The form is set to 400x400 in the IDE.

On the form there is three labels with text changing by the mouse move their code as follows:

private void Form1_MouseMove(object sender, MouseEventArgs e)
label1.Text = "this.Width = " + this.Width.ToString();
label2.Text = "this.Height = " + this.Height.ToString();
label3.Text = "Mouse X coordinate = " + e.X.ToString() + "\nMouse Y coordinate = " + e.Y.ToString();

And here is what i get:

The width

The width

The height

The height

Any ideas?

Answer Source

Windows form size is 400x400 but active area excluding Titlebar and borders is different, Mouse Pointer gets coordinates as per active area. You can get active area size by this.ClientSize.Width and this.ClientSize.Height.

Set Client area to 400x400 (Change form size according to it) and it will show you correct result.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download