I create a new form and call from the parent form as follows:
loginForm = new SubLogin();
loginForm.Show();
I need to display the chi
The setting of parent does not work for me unless I use form.ShowDialog();
.
When using form.Show();
or form.Show(this);
nothing worked until I used, this.CenterToParent();
.
I just put that in the Load method of the form. All is good.
Start position to the center of parent was set and does work when using the blocking showdialog.
Try:
loginForm.StartPosition = FormStartPosition.CenterParent;
loginForm.ShowDialog(this);
Of course the child for will now be a blocking form (dialog) of the parent window, if that isn't desired then just replace ShowDialog
with Show
..
loginForm.Show(this);
You will still need to specify the StartPosition though.
You need this:
Replace Me with this.parent, but you need to set parent before you show that form.
Private Sub ÜberToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ÜberToolStripMenuItem.Click
'About.StartPosition = FormStartPosition.Manual ' !!!!!
About.Location = New Point(Me.Location.X + Me.Width / 2 - About.Width / 2, Me.Location.Y + Me.Height / 2 - About.Height / 2)
About.Show()
End Sub
When launching a form inside an MDIForm
form you will need to use .CenterScreen
instead of .CenterParent
.
FrmLogin f = new FrmLogin();
f.MdiParent = this;
f.StartPosition = FormStartPosition.CenterScreen;
f.Show();
On the SubLogin Form I would expose a SetLocation method so that you can set it from your parent form:
public class SubLogin : Form
{
public void SetLocation(Point p)
{
this.Location = p;
}
}
Then, from your main form:
loginForm = new SubLogin();
Point p = //do math to get point
loginForm.SetLocation(p);
loginForm.Show();
If any windows form(child form) is been opened from a new thread of Main window(parent form) then its not possible to hold the sub window to the center of main window hence we need to fix the position of the sub window manually by means of X and Y co-ordinates.
In the properties of Subwindow change the "StartPosition" to be "Manual"
private void SomeFunction()
{
Thread m_Thread = new Thread(LoadingUIForm);
m_Thread.Start();
OtherParallelFunction();
m_Thread.Abort();
}
private void LoadingUIForm()
{
m_LoadingWindow = new LoadingForm(this);
m_LoadingWindow.ShowDialog();
}
public LoadingForm(Control m_Parent)
{
InitializeComponent();
this.Location = new Point( m_Parent.Location.X+(m_Parent.Size.Width/2)-(this.Size.Width/2),
m_Parent.Location.Y+(m_Parent.Size.Height/2)-(this.Size.Height/2)
);
}
Here the co-ordinates of center of parent is calculated as well as the subwindow is kept exactly at the center of the parent by calculating its own center by (this.height/2) and (this.width/2) this function can be further taken for parent relocated events also.