Gopinath
Gopinath

Reputation: 1858

How to center a form using showdialog (.NET Compact Framework)

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?

Upvotes: 8

Views: 23611

Answers (6)

hoodoos
hoodoos

Reputation: 360

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;

Upvotes: 6

cristobalhdez
cristobalhdez

Reputation: 121

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.

Upvotes: 2

Amr Mausad
Amr Mausad

Reputation: 41

this is the easiest way

Form f= new AmrDealForm();
f.CenterToScreen();
f.ShowDialog();

Upvotes: 1

Saurav Bhowmick
Saurav Bhowmick

Reputation: 61

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.

Upvotes: 6

Vibin Jith
Vibin Jith

Reputation: 931

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

Upvotes: 1

Fredrik Mörk
Fredrik Mörk

Reputation: 158299

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();

Upvotes: 12

Related Questions