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! (:
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
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