954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Center Form of Main Form

By codeorder on Dec 27th, 2010 1:56 pm

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.

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
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

Thank you, very useful post! :)

TechSupportGeek
Junior Poster in Training
68 posts since Sep 2009
Reputation Points: 7
Solved Threads: 5
 

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

RenanLazarotto
Posting Whiz in Training
247 posts since Nov 2010
Reputation Points: 16
Solved Threads: 10
 
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. :)

TechSupportGeek
Junior Poster in Training
68 posts since Sep 2009
Reputation Points: 7
Solved Threads: 5
 
That is the case, indeed, but I think what codeorder wanted to demonstrate here is the use of Functions. :)

Oh yea, indeed.

RenanLazarotto
Posting Whiz in Training
247 posts since Nov 2010
Reputation Points: 16
Solved Threads: 10
 

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.

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: