Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 490
Gopinath Avatar asked Jan 16 '10 07:01

Gopinath


People also ask

What is the difference between form show () and form ShowDialog ()?

At its most basic level it keeps the process alive until the last form is closed. Show() method shows a windows form in a non-modal state. ShowDialog() method shows a window in a modal state and stops execution of the calling context until a result is returned from the windows form open by the method.

What is ShowDialog C#?

ShowDialog() Shows the form as a modal dialog box. ShowDialog(IWin32Window) Shows the form as a modal dialog box with the specified owner.


1 Answers

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();
like image 140
Fredrik Mörk Avatar answered Oct 08 '22 00:10

Fredrik Mörk