Center Form of Main Form

codeorder 0 Tallied Votes 773 Views Share

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.
TechSupportGeek commented: Very helpful code snippet demonstrating the use of Functions. +2
codeorder 197 Nearly a Posting Virtuoso

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
commented: Nice post. I know someday this code will help me. +1
TechSupportGeek -3 Junior Poster in Training

Thank you, very useful post! :)

RenanLazarotto 0 Posting Whiz in Training

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

TechSupportGeek -3 Junior Poster in Training

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. :)

RenanLazarotto 0 Posting Whiz in Training

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

Oh yea, indeed.

codeorder 197 Nearly a Posting Virtuoso

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.