In this case, Form1 is the Main Form.

Pre-requisites: 2 Forms (Form1 and Form2), 1 Button (on Form1).

Public Class Form1

    Function centerForm(ByVal Form_to_Center As Form, ByVal Form_Location As Point) As Point
        Dim pLocation As New Point
        pLocation.X = (Me.Left + (Me.Width - Form_to_Center.Width) / 2) '// set the X coordinates.
        pLocation.Y = (Me.Top + (Me.Height - Form_to_Center.Height) / 2) '// set the Y coordinates.
        Return pLocation '// return the Location to the Form it was called from.
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.ShowDialog()
    End Sub
End Class
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Location = Form1.centerForm(Me, Me.Location) '// center Form of Main Form.
    End Sub
End Class

To center any other Form to the Main Form,
add the following code to the selected Form's .Load Event, as shown in the above code block for Form2.

Me.Location = Form1.centerForm(Me, Me.Location) '// center Form of Main Form.

Edited 5 Years Ago by codeorder: n/a

Comments
Very helpful code snippet demonstrating the use of Functions.

Late night programming and extra Parameters, could cause a bit of confusion.

Although the code posted in the original post of this thread will work,
..I did realize that an extra Parameter was added to the Function.
(whispering - "probably added by Santa Claus since I stole his goodie bag and he's still looking for it":D)

Here is the updated version.

Public Class Form1

    Function centerForm(ByVal Form_to_Center As Form) As Point
        Dim pLocation As New Point
        pLocation.X = (Me.Left + (Me.Width - Form_to_Center.Width) / 2) '// set the X coordinates.
        pLocation.Y = (Me.Top + (Me.Height - Form_to_Center.Height) / 2) '// set the Y coordinates.
        Return pLocation '// return the Location to the Form it was called from.
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Form2.ShowDialog()
    End Sub
End Class
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Location = Form1.centerForm(Me) '// center Form of Main Form.
    End Sub
End Class

Edited 5 Years Ago by codeorder: n/a

Comments
Nice post. I know someday this code will help me.

Hm... seems that if you set Start Position to "CenterParent" you have te same result! (:

That is the case, indeed, but I think what codeorder wanted to demonstrate here is the use of Functions. :)

That is the case, indeed, but I think what codeorder wanted to demonstrate here is the use of Functions. :)

Oh yea, indeed.

Using something like:

Form2.ShowDialog()

...and setting the StartPosition to "CenterParent" in Form2's Properties, will work.

However, using just:

Form2.Show()

...even if setting the Form2's Properties for StartPosition to "CenterParent", will NOT WORK.

I am sure there are ways around it other than the code I have posted, and if so, feel free to reply with a solution. After all, this is a "Code Snippet" thread.

The original code I have posted was created for a Window's Application, using the 2.0 .Net Framework.

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.