How to center show.dialog form on parent panel

2019-08-13 00:34发布

I have GUI which is splitted two piece with SplitContainer element. One of is navigation panel one of is workspace panel. When i open the app on start-up there's a new form appears (Show.Dialog()) for Welcomes user but i would like to loads it center of the workspace panel.

Is there anybody experienced it how it can be solved ?

 Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        frmWelcome.ShowDialog()
 End Sub

2条回答
老娘就宠你
2楼-- · 2019-08-13 01:19

Assuming that Panel2 is your WorkSpace panel, use the Panel.PointToScreen method to calculate the screen coordinates of frmWelcome and position it in the middle.

Be sure to set your frmWelcome.StartPosition = Manual, in the designer or in the constructor.

Here I'm using the Shown event, to be sure that the pre-set positions in MainForm are already set.

Private Sub MainForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown

    Dim p As Point = New Point(((SplitContainer1.Panel2.ClientSize.Width) \ 2) - frmWelcome.Width \ 2,
                               ((SplitContainer1.Panel2.ClientSize.Height) \ 2) - frmWelcome.Height \ 2)

    frmWelcome.Location = SplitContainer1.Panel2.PointToScreen(p)
    frmWelcome.ShowDialog()

End Sub
查看更多
▲ chillily
3楼-- · 2019-08-13 01:20

You can use the properties on forms to do this.

set the frmWelcome form property StartPosition to CenterScreen.

If you want it center of the screen opening it, you will have to setup MDI but from there you can do frmWelcome.ShowDialog(Me) and set the property StartPosition to CenterParent.

Hope this helps!

查看更多
登录 后发表回答