Reputation: 483
I am trying to get a form to center the parent. I have done lots of 'googling' and none if it has worked.
The issue is. When I have a form set to "Center Parent Form" it will work, unless the parent form is moved from the original starting location.
Example: Parent form starts top left of screen. I move it to center screen. I then preform and action to have a popup window, that window gets Centered to the original location of the parent form in the top left. Not the current one. Visual: https://i.sstatic.net/byaSd.jpg
I have set the form properties to be 'Center Parent' as well as
Me.StartPosition = FormStartPosition.CenterParent
Upvotes: 0
Views: 1228
Reputation: 1489
In which class and method are you setting the Me.StartPosition
property?
Have you seen this Stackoverflow answer? https://stackoverflow.com/a/30199106/1337635
UPDATE You need to do two things to get this to work:-
As per @mark-hall, you need to show the form and pass in the parent:-
Dim child As frmChild
child = New frmChild()
child.Show(Me) 'Explicitly declare the parent
As per the above answer I referenced, in the Load
event of the child form, you need to call Me.CenterToParent()
Upvotes: 3
Reputation: 54532
I was able to duplicate the problem. Try adding the Shown
EventHandler to your popup form. If you then assign the owner to the Form when you show it you should be able to position your form in the Handler, something like this. Be aware if you move the Owning Form the popup Form will not change.
Make sure you Show the Form using Show(Me)
or else Owner
will not be populated.
Public Class popup
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
BackColor = Color.LightBlue 'So I can see it
End Sub
Private Sub popup_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
Left = (Owner.Width / 2 - Width / 2) + Owner.Left
Top = (Owner.Height / 2 - Height / 2) + Owner.Top
End Sub
End Class
Upvotes: 1