CJSoldier
CJSoldier

Reputation: 561

Form position VB.net

I have a two form app.. The main form loads to the bottom corner of the screen. But I am having trouble making the second form load in the center of that form all times. I used to do the parent/child thing but it screws up my design a bit. Is there any better way to do this?

Thanks!

'Set window to lower right of screen
    Me.Location = New Point(Screen.PrimaryScreen.WorkingArea.Width - Me.Width, Screen.PrimaryScreen.WorkingArea.Height - Me.Height)

Upvotes: 2

Views: 9218

Answers (2)

LarsTech
LarsTech

Reputation: 81675

If the child form is a dialog form, then all you have to do is set the StartPosition property:

Dim f As New Form
f.StartPosition = FormStartPosition.CenterParent
f.ShowDialog(Me)

If the child form is not a dialog form, then try positioning the form manually:

Dim f As New Form
f.StartPosition = FormStartPosition.Manual
AddHandler f.Load, Sub()
                     f.Location = New Point(Me.Left + Me.Width / 2 - f.Width / 2, _
                                            Me.Top + Me.Height / 2 - f.Height / 2)
                   End Sub
f.Show(Me)

Upvotes: 2

Arthur Rey
Arthur Rey

Reputation: 3056

If you want the second form to be centered in the first one, you should apply this formula :

Me.Location = New Point((firstForm.Width - Me.Width) / 2, (firstForm.Height - Me.Height) / 2)

Upvotes: 0

Related Questions