8

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?

Gopinath
  • 1,858
  • 7
  • 31
  • 50
  • Try other properties See http://stackoverflow.com/questions/944897/show-a-child-form-in-the-centre-of-parent-form-in-c – Brij Jan 16 '10 at 08:10

6 Answers6

12

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
  • 155,851
  • 29
  • 291
  • 343
  • This is not working! I still see the form being randomly placed on the screen. – Gopinath Jan 16 '10 at 10:12
  • @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 Jan 16 '10 at 10:29
  • 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 Jan 19 '10 at 15:08
6

If Parent is not defined for the Dialog then use

login.StartPosition = FormStartPosition.CenterScreen;
login.ShowDialog(); 

where login is the Form Object

or you can also use if you are calling on top of an existing Parent Form

login.StartPosition = FormStartPosition.CenterParent;
login.ShowDialog();

This property can also be set in the Property dialog of the Form, if you think that the property is always the same for you. By default it should be set to CenterParent, which will not work in case you are invoking your Form before the Parent Form in some cases, like Login screen for the first time etc.

Jim Dagg
  • 2,044
  • 22
  • 29
6

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 this:

form1.StartPosition = FormStartPosition.CenterScreen;
Ghasem
  • 14,455
  • 21
  • 138
  • 171
hoodoos
  • 340
  • 1
  • 4
  • 12
2

I know this is old post, but I had the same problem and I solved with this way:

I create an Interface:

public interface FormExtensions
    {
        void CenterForm(Form forma);
    }

After I did implements the interface on my class:

    public partial class frmFirma : Form, FormExtensions
    {
        public frmFirma()
        {
            InitializeComponent();
        }
        public void CenterForm(Form forma)
        {
            forma.Location = new Point(
            Screen.PrimaryScreen.WorkingArea.Width / 2 - forma.Width / 2,
            Screen.PrimaryScreen.WorkingArea.Height / 2 - forma.Height / 2);
        }
    }

Then I can crate a instance of the: "frmFirma" an call the method "CenterForm":

private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Formas.frmFirma firma = new Formas.frmFirma();
            firma.CenterForm(firma);
            firma.ShowDialog();     
        }

I hope this works for someone.

1

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
  • 923
  • 3
  • 15
  • 34
1

this is the easiest way

Form f= new AmrDealForm();
f.CenterToScreen();
f.ShowDialog();
Amr Mausad
  • 41
  • 2