FormStartPosition.CenterParent does not work
Posted
by
kol
on Stack Overflow
See other posts from Stack Overflow
or by kol
Published on 2011-12-19T20:22:51Z
Indexed on
2012/11/19
23:02 UTC
Read the original article
Hit count: 208
In the following code, only the second method works for me (.NET 4.0). FormStartPosition.CenterParent
does not center the child form over its parent.
Why?
Source: this SO question
using System;
using System.Drawing;
using System.Windows.Forms;
class Program
{
private static Form f1;
public static void Main()
{
f1 = new Form() { Width = 640, Height = 480 };
f1.MouseClick += f1_MouseClick;
Application.Run(f1);
}
static void f1_MouseClick(object sender, MouseEventArgs e)
{
Form f2 = new Form() { Width = 400, Height = 300 };
switch (e.Button)
{
case MouseButtons.Left:
{
// 1st method
f2.StartPosition = FormStartPosition.CenterParent;
break;
}
case MouseButtons.Right:
{
// 2nd method
f2.StartPosition = FormStartPosition.Manual;
f2.Location = new Point(
f1.Location.X + (f1.Width - f2.Width) / 2,
f1.Location.Y + (f1.Height - f2.Height) / 2
);
break;
}
}
f2.Show(f1);
}
}
© Stack Overflow or respective owner