Search code examples
vb.netvisual-studiowinformssplitcontainer

How to center a modal Form on a specific Panel of a SplitContainer


I have GUI which is split in two pieces using a SplitContainer Control.
One part is a navigation Panel, the other a workspace Panel.

When I open the app, on start-up a new Form appears (using ShowDialog()), to welcomes Users. I would like to show it centered in the middle of the workspace Panel.

Is there anybody who knows how to solve this?

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

Solution

  • Assuming that Panel2 is your WorkSpace Panel, use its 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 its 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