Form.WindowState Property

Gets or sets the form's window state.

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

public FormWindowState WindowState { get; set; }
/** @property */
public FormWindowState get_WindowState ()

/** @property */
public void set_WindowState (FormWindowState value)

public function get WindowState () : FormWindowState

public function set WindowState (value : FormWindowState)

Not applicable.

Property Value

A FormWindowState that represents the window state of the form. The default is FormWindowState.Normal.

Exception typeCondition

InvalidEnumArgumentException

The value specified is outside the range of valid values.

Before a form is displayed, the WindowState property is always set to FormWindowState.Normal, regardless of its initial setting. This is reflected in the Height, Left, Top, and Width property settings. If a form is hidden after it has been shown, these properties reflect the previous state until the form is shown again, regardless of any changes made to the WindowState property.

The following code example demonstrates how to create a top-most form. The example creates two forms, one that is maximized and one which will be displayed as a top-most form. The first form, named bottomForm, is displayed maximized, using the WindowState property, to better demonstrate the abilities of the top-most form. The second form, named topMostForm, sets the TopMost property to true to display the form as a top-most form. When this code is run, clicking on the maximized form will not cause the topmost form to be displayed below the maximized form. The example requires that the method defined in the example is called from another form.

private void CreateMyTopMostForm()
{
   // Create lower form to display.
   Form bottomForm = new Form();
   // Display the lower form Maximized to demonstrate effect of TopMost property.
   bottomForm.WindowState = FormWindowState.Maximized;
   // Display the bottom form.
   bottomForm.Show();
   // Create the top most form.
   Form topMostForm = new Form();
   // Set the size of the form larger than the default size.
   topMostForm.Size = new Size(300,300);
   // Set the position of the top most form to center of screen.
   topMostForm.StartPosition = FormStartPosition.CenterScreen;
   // Display the form as top most form.
   topMostForm.TopMost = true;
   topMostForm.Show();
}

private void CreateMyTopMostForm()
{
    // Create lower form to display.
    Form bottomForm = new Form();

    // Display the lower form Maximized to demonstrate effect
    // of TopMost property.
    bottomForm.set_WindowState(FormWindowState.Maximized);

    // Display the bottom form.
    bottomForm.Show();

    // Create the top most form.
    Form topMostForm = new Form();

    // Set the size of the form larger than the default size.
    topMostForm.set_Size(new Size(300, 300));

    // Set the position of the top most form to center of screen.
    topMostForm.set_StartPosition(FormStartPosition.CenterScreen);

    // Display the form as top most form.
    topMostForm.set_TopMost(true);
    topMostForm.Show();
} //CreateMyTopMostForm

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show: