Hi! I m trying to open a filter form at the center of a form which is inside the panel 2 of a split container.. I ve selected Start Position of the filter form => Center Screen but the problem is that when I push the button which opens the filter form for the first time the form appears at the upper left corner of the panel 2.. next time I open the form it appears at the center.. could you pls help me?
thnx in advance!

Dani AI

Generated

A reliable fix is to set the dialog's owner so the runtime can center it against the top-level form, rather than relying on the panel/control that hosts the caller. was right to suggest CenterParent; the common problem here (the first show at 0,0 then correct on subsequent shows) happens when the dialog has no proper owner or when positioning is calculated before the parent layout is final. 's manual Location change is fine, but the owner approach is more robust.

Example (VB): set StartPosition to CenterParent and pass the top-level form as owner. From code that lives in a control or an "embedded" form inside the panel use FindForm():

Dim dlg As New FilterForm()
dlg.StartPosition = FormStartPosition.CenterParent

Dim ownerForm As Form = Me.FindForm()
If ownerForm IsNot Nothing Then
    dlg.ShowDialog(ownerForm)
Else
    dlg.ShowDialog()
End If

If a modal dialog is not acceptable or CenterParent still misbehaves (embedded forms, custom hosting, or timing issues), compute and set Location manually in screen coordinates before showing:

Dim dlg As New FilterForm()
dlg.StartPosition = FormStartPosition.Manual

Dim ownerForm As Form = Me.FindForm()
If ownerForm IsNot Nothing Then
    Dim ownerScreen = ownerForm.PointToScreen(New Point(0,0))
    Dim x = ownerScreen.X + (ownerForm.ClientSize.Width - dlg.Width) \ 2
    Dim y = ownerScreen.Y + (ownerForm.ClientSize.Height - dlg.Height) \ 2
    dlg.Location = New Point(x, y)
End If

dlg.Show(ownerForm)

Notes: CenterScreen centers on a monitor, not the parent form. If the caller is a Form hosted as a control (TopLevel = False), use FindForm() to get the real top-level form. If position still seems off on first show, set StartPosition = Manual and compute Location after sizes are known (for example in the caller, or in the dialog's Load event).

Recommended Answers

All 5 Replies

Have you tried setting the start position to centerParent?

I cant make the form (say it form1, the form which open the filter form) mdi parent because it is a form which appears in a panel of a split container and it said that i cant make a control, like the mdi parent, member of an the split container control.. I try to find a wayt to do so but i didn t achieve it yet

this is the interface

I solved by canging the Location Property and it works fine..

weldone!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.