Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
Form Class
Form Properties
 StartPosition Property

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
Form.StartPosition Property

Gets or sets the starting position of the form at run time.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Visual Basic (Declaration)
<LocalizableAttribute(True)> _
Public Property StartPosition As FormStartPosition
Visual Basic (Usage)
Dim instance As Form
Dim value As FormStartPosition

value = instance.StartPosition

instance.StartPosition = value
C#
[LocalizableAttribute(true)] 
public FormStartPosition StartPosition { get; set; }
C++
[LocalizableAttribute(true)] 
public:
property FormStartPosition StartPosition {
    FormStartPosition get ();
    void set (FormStartPosition value);
}
J#
/** @property */
public FormStartPosition get_StartPosition ()

/** @property */
public void set_StartPosition (FormStartPosition value)
JScript
public function get StartPosition () : FormStartPosition

public function set StartPosition (value : FormStartPosition)

Property Value

A FormStartPosition that represents the starting position of the form.
Exception typeCondition

InvalidEnumArgumentException

The value specified is outside the range of valid values.

This property enables you to set the starting position of the form when it is displayed at run time. The form can be displayed manually or in the default location specified by Windows. You can also position the form to display in the center of the screen or in the center of its parent form for forms such as multiple-document interface (MDI) child forms.

The following code example creates a new instance of a Form and calls the ShowDialog method to display the form as a dialog box. The example sets the FormBorderStyle, AcceptButton, CancelButton, and StartPosition properties to change the appearance and functionality of the form to a dialog box. The example also uses the Add method of the form's Controls collection to add two Button controls. The example uses the HelpButton property to display a help button in the caption bar of the dialog box.

Visual Basic
Public Sub CreateMyForm()
    ' Create a new instance of the form.
    Dim form1 As New Form()
    ' Create two buttons to use as the accept and cancel buttons.
    Dim button1 As New Button()
    Dim button2 As New Button()
    
    ' Set the text of button1 to "OK".
    button1.Text = "OK"
    ' Set the position of the button on the form.
    button1.Location = New Point(10, 10)
    ' Set the text of button2 to "Cancel".
    button2.Text = "Cancel"
    ' Set the position of the button based on the location of button1.
    button2.Location = New Point(button1.Left, button1.Height + button1.Top + 10)
    ' Set the caption bar text of the form.   
    form1.Text = "My Dialog Box"
    ' Display a help button on the form.
    form1.HelpButton = True
    
    ' Define the border style of the form to a dialog box.
    form1.FormBorderStyle = FormBorderStyle.FixedDialog
    ' Set the accept button of the form to button1.
    form1.AcceptButton = button1
    ' Set the cancel button of the form to button2.
    form1.CancelButton = button2
    ' Set the start position of the form to the center of the screen.
    form1.StartPosition = FormStartPosition.CenterScreen
    
    ' Add button1 to the form.
    form1.Controls.Add(button1)
    ' Add button2 to the form.
    form1.Controls.Add(button2)
    
    ' Display the form as a modal dialog box.
    form1.ShowDialog()
End Sub 'CreateMyForm
C#
public void CreateMyForm()
 {
    // Create a new instance of the form.
    Form form1 = new Form();
    // Create two buttons to use as the accept and cancel buttons.
    Button button1 = new Button ();
    Button button2 = new Button ();
   
    // Set the text of button1 to "OK".
    button1.Text = "OK";
    // Set the position of the button on the form.
    button1.Location = new Point (10, 10);
    // Set the text of button2 to "Cancel".
    button2.Text = "Cancel";
    // Set the position of the button based on the location of button1.
    button2.Location
       = new Point (button1.Left, button1.Height + button1.Top + 10);
    // Set the caption bar text of the form.   
    form1.Text = "My Dialog Box";
    // Display a help button on the form.
    form1.HelpButton = true;
 
    // Define the border style of the form to a dialog box.
    form1.FormBorderStyle = FormBorderStyle.FixedDialog;
    // Set the accept button of the form to button1.
    form1.AcceptButton = button1;
    // Set the cancel button of the form to button2.
    form1.CancelButton = button2;
    // Set the start position of the form to the center of the screen.
    form1.StartPosition = FormStartPosition.CenterScreen;
    
    // Add button1 to the form.
    form1.Controls.Add(button1);
    // Add button2 to the form.
    form1.Controls.Add(button2);
    
    // Display the form as a modal dialog box.
    form1.ShowDialog();
 }
 
C++
public:
   void CreateMyForm()
   {
      // Create a new instance of the form.
      Form^ form1 = gcnew Form;
      // Create two buttons to use as the accept and cancel buttons.
      Button^ button1 = gcnew Button;
      Button^ button2 = gcnew Button;
      
      // Set the text of button1 to "OK".
      button1->Text = "OK";
      // Set the position of the button on the form.
      button1->Location = Point(10,10);
      // Set the text of button2 to "Cancel".
      button2->Text = "Cancel";
      // Set the position of the button based on the location of button1.
      button2->Location
         = Point( button1->Left, button1->Height + button1->Top + 10 );
      // Set the caption bar text of the form.   
      form1->Text = "My Dialog Box";
      // Display a help button on the form.
      form1->HelpButton = true;
      
      // Define the border style of the form to a dialog box.
      form1->FormBorderStyle = ::FormBorderStyle::FixedDialog;
      // Set the accept button of the form to button1.
      form1->AcceptButton = button1;
      // Set the cancel button of the form to button2.
      form1->CancelButton = button2;
      // Set the start position of the form to the center of the screen.
      form1->StartPosition = FormStartPosition::CenterScreen;

      // Add button1 to the form.
      form1->Controls->Add( button1 );
      // Add button2 to the form.
      form1->Controls->Add( button2 );
      
      // Display the form as a modal dialog box.
      form1->ShowDialog();
   }
J#
public void CreateMyForm()
{
    // Create a new instance of the form.
    Form form1 = new Form();

    // Create two buttons to use as the accept and cancel buttons.
    Button button1 = new Button();
    Button button2 = new Button();

    // Set the text of button1 to "OK".
    button1.set_Text("OK");

    // Set the position of the button on the form.
    button1.set_Location(new Point(10, 10));

    // Set the text of button2 to "Cancel".
    button2.set_Text("Cancel");

    // Set the position of the button based on the location of button1.
    button2.set_Location(new Point(button1.get_Left(), 
        button1.get_Height() + button1.get_Top() + 10));

    // Set the caption bar text of the form.   
    form1.set_Text("My Dialog Box");

    // Display a help button on the form.
    form1.set_HelpButton(true);

    // Define the border style of the form to a dialog box.
    form1.set_FormBorderStyle(get_FormBorderStyle().FixedDialog);

    // Set the accept button of the form to button1.
    form1.set_AcceptButton(button1);

    // Set the cancel button of the form to button2.
    form1.set_CancelButton(button2);

    // Set the start position of the form to the center of the screen.
    form1.set_StartPosition(FormStartPosition.CenterScreen);

    // Add button1 to the form.
    form1.get_Controls().Add(button1);

    // Add button2 to the form.
    form1.get_Controls().Add(button2);

    // Display the form as a modal dialog box.
    form1.ShowDialog();
} //CreateMyForm

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Usage with Form.Show() with StartPosition.CenterParent      Frank Dzaebel   |   Edit   |   Show History

If using this property with the Show() method of the Form class you will not get a centered Position to the calling form although setting the StartPosition to CenterParent.

CenterParent is defined as: "The form is centered within the bounds of its parent form". But in this case the parent would be null so the Show method displays the form only at default position.

To achieve the centered behavior use code like this:

private void btnForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show(); CenterFormTo(f2, this);
}
 
/// <summary>The Form '<paramref name="form"/>' will be displayed centered in the '<paramref name="containerForm"/>'</summary>
protected void CenterFormTo(Form form, Form containerForm)
{
Point point = new Point(); Size formSize = form.Size;
Rectangle workingArea = Screen.GetWorkingArea(containerForm);
Rectangle rect = containerForm.Bounds;
point.X = ((rect.Left + rect.Right) - formSize.Width) / 2;
if (point.X < workingArea.X) point.X = workingArea.X;
else if ((point.X+formSize.Width) > (workingArea.X+workingArea.Width))
point.X = (workingArea.X + workingArea.Width) - formSize.Width;
point.Y = ((rect.Top + rect.Bottom) - formSize.Height) / 2;
if (point.Y < workingArea.Y) point.Y = workingArea.Y;
else if ((point.Y+formSize.Height) > (workingArea.Y+workingArea.Height))
point.Y = (workingArea.Y + workingArea.Height) - formSize.Height;
form.Location = point;
}

This is a reflected version from the protected Form method: CenterToParent().

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker