views:

673

answers:

3

I want to centre a popup form launched using Form.ShowDialog() in .NET Compact Framework. I dont see any property like StartPosition in .NET CF Form object.

Can someone please suggest me how to centre popups in .NET CF 3.5?

+1  A: 

You can make an extension method that does the work for you:

public static class FormExtensions
{
    public static void CenterForm(this Form theForm)
    {
        theForm.Location = new Point(
            Screen.PrimaryScreen.WorkingArea.Width / 2 - theForm.Width / 2,
            Screen.PrimaryScreen.WorkingArea.Height / 2 - theForm.Height / 2);
    }
}

You call it like this:

TheDialogForm f = new TheDialogForm();
f.CenterForm();            
f.ShowDialog();
Fredrik Mörk
This is not working! I still see the form being randomly placed on the screen.
Gopinath
@Gopinath: that's odd; I tried out the code before posting it. Is there any other code setting the `Location` or `Bounds` property of the form?
Fredrik Mörk
thanks fredrik. When tested on the device, the popup centered as expected. But when the application was ran on a PC, it did not center properly.Many thanks for your help.
Gopinath
A: 

Set the left and Top properties on the of the form in 'frmDialog_Activated event

Private Sub frmDialog_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Me.Left = (frmMain.Width - Me.Width) / 2 ' AS Your Wish
        Me.Top = (frmMain.Height - Me.Height) / 2 + 165 '' AS Your Wish
    End Sub
Vibin Jith
A: 

If you want your popup form appear in center of screen by default you can just set a starting position for it in form properties, it should sound like 'Center parent'.

Something like that: form1.StartPosition = FormStartPosition.CenterScreen;

hoodoos
As noted in the question, the `StartPosition` property is not available in the .NET Compact Framework.
Fredrik Mörk
damn me! i won't answer on stuff which i used that long ago.. really :)
hoodoos