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 by codeorder: n/a