Form Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Represents a window or dialog box that makes up an application's user interface.
public ref class Form : System::Windows::Forms::ContainerControl
public class Form : System.Windows.Forms.ContainerControl
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
public class Form : System.Windows.Forms.ContainerControl
type Form = class
inherit ContainerControl
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Form = class
inherit ContainerControl
Public Class Form
Inherits ContainerControl
- Inheritance
- Derived
- Attributes
Examples
The following 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, MinimizeBox, MaximizeBox, 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.
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 MaximizeBox to false to remove the maximize box.
form1->MaximizeBox = false;
// Set the MinimizeBox to false to remove the minimize box.
form1->MinimizeBox = false;
// 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();
}
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 MaximizeBox to false to remove the maximize box.
form1.MaximizeBox = false;
// Set the MinimizeBox to false to remove the minimize box.
form1.MinimizeBox = false;
// 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();
}
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 MaximizeBox to false to remove the maximize box.
form1.MaximizeBox = False
' Set the MinimizeBox to false to remove the minimize box.
form1.MinimizeBox = False
' 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
Remarks
A Form is a representation of any window displayed in your application. The Form class can be used to create standard, tool, borderless, and floating windows. You can also use the Form class to create modal windows such as a dialog box. A special kind of form, the multiple-document interface (MDI) form, can contain other forms called MDI child forms. An MDI form is created by setting the IsMdiContainer property to true. MDI child forms are created by setting the MdiParent property to the MDI parent form that will contain the child form.
Using the properties available in the Form class, you can determine the appearance, size, color, and window management features of the window or dialog box you are creating. The Text property allows you to specify the caption of the window in the title bar. The Size and DesktopLocation properties allow you to define the size and position of the window when it is displayed. You can use the ForeColor color property to change the default foreground color of all controls placed on the form. The FormBorderStyle, MinimizeBox, and MaximizeBox properties allow you to control whether the form can be minimized, maximized, or resized at run time.
In addition to properties, you can use the methods of the class to manipulate a form. For example, you can use the ShowDialog method to show a form as a modal dialog box. You can use the SetDesktopLocation method to position the form on the desktop.
The events of the Form class allow you to respond to actions performed on the form. You can use the Activated event to perform operations such as updating the data displayed in the controls of the form when the form is activated.
You can use a form as the starting class in your application by placing a method called Main in the class. In the Main method add code to create and show the form. You will also need to add the STAThread attribute to the Main method in order for the form to run. When the starting form is closed, the application is also closed.
If you set the Enabled property to false before the Form is visible (for example, setting Enabled to false in the Microsoft Visual Studio designer), the minimize, maximize, close, and system buttons remain enabled. If you set Enabled to false after the Form is visible (for example, when the Load event occurs), the buttons are disabled.
Constructors
| Name | Description |
|---|---|
| Form() |
Initializes a new instance of the Form class. |
Fields
| Name | Description |
|---|---|
| ScrollStateAutoScrolling |
Determines the value of the AutoScroll property. (Inherited from ScrollableControl) |
| ScrollStateFullDrag |
Determines whether the user has enabled full window drag. (Inherited from ScrollableControl) |
| ScrollStateHScrollVisible |
Determines whether the value of the HScroll property is set to |
| ScrollStateUserHasScrolled |
Determines whether the user had scrolled through the ScrollableControl control. (Inherited from ScrollableControl) |
| ScrollStateVScrollVisible |
Determines whether the value of the VScroll property is set to |
Properties
| Name | Description |
|---|---|
| AcceptButton |
Gets or sets the button on the form that is clicked when the user presses the ENTER key. |
| AccessibilityObject |
Gets the AccessibleObject assigned to the control. (Inherited from Control) |
| AccessibleDefaultActionDescription |
Gets or sets the default action description of the control for use by accessibility client applications. (Inherited from Control) |
| AccessibleDescription |
Gets or sets the description of the control used by accessibility client applications. (Inherited from Control) |
| AccessibleName |
Gets or sets the name of the control used by accessibility client applications. (Inherited from Control) |
| AccessibleRole |
Gets or sets the accessible role of the control. (Inherited from Control) |
| ActiveControl |
Gets or sets the active control on the container control. (Inherited from ContainerControl) |
| ActiveForm |
Gets the currently active form for this application. |
| ActiveMdiChild |
Gets the currently active multiple-document interface (MDI) child window. |
| AllowDrop |
Gets or sets a value indicating whether the control can accept data that the user drags onto it. (Inherited from Control) |
| AllowTransparency |
Gets or sets a value indicating whether the opacity of the form can be adjusted. |
| Anchor |
Gets or sets the edges of the container to which a control is bound and determines how a control is resized with its parent. (Inherited from Control) |
| AutoScale |
Obsolete.
Obsolete.
Gets or sets a value indicating whether the form adjusts its size to fit the height of the font used on the form and scales its controls. |
| AutoScaleBaseSize |
Gets or sets the base size used for autoscaling of the form. |
| AutoScaleDimensions |
Gets or sets the dimensions that the control was designed to. (Inherited from ContainerControl) |
| AutoScaleFactor |
Gets the scaling factor between the current and design-time automatic scaling dimensions. (Inherited from ContainerControl) |
| AutoScaleMode |
Gets or sets the automatic scaling mode of the control. (Inherited from ContainerControl) |
| AutoScroll |
Gets or sets a value indicating whether the form enables autoscrolling. |
| AutoScrollMargin |
Gets or sets the size of the auto-scroll margin. (Inherited from ScrollableControl) |
| AutoScrollMinSize |
Gets or sets the minimum size of the auto-scroll. (Inherited from ScrollableControl) |
| AutoScrollOffset |
Gets or sets where this control is scrolled to in ScrollControlIntoView(Control). (Inherited from Control) |
| AutoScrollPosition |
Gets or sets the location of the auto-scroll position. (Inherited from ScrollableControl) |
| AutoSize |
Resize the form according to the setting of AutoSizeMode. |
| AutoSizeMode |
Gets or sets the mode by which the form automatically resizes itself. |
| AutoValidate |
Gets or sets a value that indicates whether controls in this container will be automatically validated when the focus changes. |
| BackColor |
Gets or sets the background color for the control. |
| BackgroundImage |
Gets or sets the background image displayed in the control. (Inherited from Control) |
| BackgroundImageLayout |
Gets or sets the background image layout as defined in the ImageLayout enumeration. (Inherited from Control) |
| BindingContext |
Gets or sets the BindingContext for the control. (Inherited from ContainerControl) |
| Bottom |
Gets the distance, in pixels, between the bottom edge of the control and the top edge of its container's client area. (Inherited from Control) |
| Bounds |
Gets or sets the size and location of the control including its nonclient elements, in pixels, relative to the parent control. (Inherited from Control) |
| CancelButton |
Gets or sets the button control that is clicked when the user presses the ESC key. |
| CanEnableIme |
Gets a value indicating whether the ImeMode property can be set to an active value, to enable IME support. (Inherited from ContainerControl) |
| CanFocus |
Gets a value indicating whether the control can receive focus. (Inherited from Control) |
| CanRaiseEvents |
Determines if events can be raised on the control. (Inherited from Control) |
| CanSelect |
Gets a value indicating whether the control can be selected. (Inherited from Control) |
| Capture |
Gets or sets a value indicating whether the control has captured the mouse. (Inherited from Control) |
| CausesValidation |
Gets or sets a value indicating whether the control causes validation to be performed on any controls that require validation when it receives focus. (Inherited from Control) |
| ClientRectangle |
Gets the rectangle that represents the client area of the control. (Inherited from Control) |
| ClientSize |
Gets or sets the size of the client area of the form. |
| CompanyName |
Gets the name of the company or creator of the application containing the control. (Inherited from Control) |
| Container |
Gets the IContainer that contains the Component. (Inherited from Component) |
| ContainsFocus |
Gets a value indicating whether the control, or one of its child controls, currently has the input focus. (Inherited from Control) |
| ContextMenu |
Obsolete.
Gets or sets the shortcut menu associated with the control. (Inherited from Control) |
| ContextMenuStrip |
Gets or sets the ContextMenuStrip associated with this control. (Inherited from Control) |
| ControlBox |
Gets or sets a value indicating whether a control box is displayed in the caption bar of the form. |
| Controls |
Gets the collection of controls contained within the control. (Inherited from Control) |
| Created |
Gets a value indicating whether the control has been created. (Inherited from Control) |
| CreateParams |
Gets the required creation parameters when the control handle is created. |
| CurrentAutoScaleDimensions |
Gets the current run-time dimensions of the screen. (Inherited from ContainerControl) |
| Cursor |
Gets or sets the cursor that is displayed when the mouse pointer is over the control. (Inherited from Control) |
| DataBindings |
Gets the data bindings for the control. (Inherited from Control) |
| DataContext |
Gets or sets the data context for the purpose of data binding. This is an ambient property. (Inherited from Control) |
| DefaultCursor |
Gets or sets the default cursor for the control. (Inherited from Control) |
| DefaultImeMode |
Gets the default Input Method Editor (IME) mode supported by the control. |
| DefaultMargin |
Gets the space, in pixels, that is specified by default between controls. (Inherited from Control) |
| DefaultMaximumSize |
Gets the length and height, in pixels, that is specified as the default maximum size of a control. (Inherited from Control) |
| DefaultMinimumSize |
Gets the length and height, in pixels, that is specified as the default minimum size of a control. (Inherited from Control) |
| DefaultPadding |
Gets the default internal spacing, in pixels, of the contents of a control. (Inherited from Control) |
| DefaultSize |
Gets the default size of the control. |
| DefaultVisualStylesMode | (Inherited from Control) |
| DesignMode |
Gets a value that indicates whether the Component is currently in design mode. (Inherited from Component) |
| DesktopBounds |
Gets or sets the size and location of the form on the Windows desktop. |
| DesktopLocation |
Gets or sets the location of the form on the Windows desktop. |
| DeviceDpi |
Gets the DPI value for the display device where the control is currently being displayed. (Inherited from Control) |
| DialogResult |
Gets or sets the dialog result for the form. |
| DisplayRectangle |
Gets the rectangle that represents the virtual display area of the control. (Inherited from ScrollableControl) |
| Disposing |
Gets a value indicating whether the base Control class is in the process of disposing. (Inherited from Control) |
| Dock |
Gets or sets which control borders are docked to its parent control and determines how a control is resized with its parent. (Inherited from Control) |
| DockPadding |
Gets the dock padding settings for all edges of the control. (Inherited from ScrollableControl) |
| DoubleBuffered |
Gets or sets a value indicating whether this control should redraw its surface using a secondary buffer to reduce or prevent flicker. (Inherited from Control) |
| EffectiveVisualStylesMode | (Inherited from Control) |
| Enabled |
Gets or sets a value indicating whether the control can respond to user interaction. (Inherited from Control) |
| Events |
Gets the list of event handlers that are attached to this Component. (Inherited from Component) |
| Focused |
Gets a value indicating whether the control has input focus. (Inherited from Control) |
| Font |
Gets or sets the font of the text displayed by the control. (Inherited from Control) |
| FontHeight |
Gets or sets the height of the font of the control. (Inherited from Control) |
| ForeColor |
Gets or sets the foreground color of the control. (Inherited from Control) |
| FormBorderColor |
Sets or gets the Form's border color. |
| FormBorderStyle |
Gets or sets the border style of the form. |
| FormCaptionBackColor |
Sets or gets the Form's title bar back color (caption back color). |
| FormCaptionTextColor |
Sets or gets the Form's title bar text color (windows caption text color). |
| FormCornerPreference |
Sets or gets the rounding style of the Form's corners using the FormCornerPreference enum. |
| FormRevealMode | |
| FormScreenCaptureMode | |
| Handle |
Gets the window handle that the control is bound to. (Inherited from Control) |
| HasChildren |
Gets a value indicating whether the control contains one or more child controls. (Inherited from Control) |
| Height |
Gets or sets the height of the control. (Inherited from Control) |
| HelpButton |
Gets or sets a value indicating whether a Help button should be displayed in the caption box of the form. |
| HorizontalScroll |
Gets the characteristics associated with the horizontal scroll bar. (Inherited from ScrollableControl) |
| HScroll |
Gets or sets a value indicating whether the horizontal scroll bar is visible. (Inherited from ScrollableControl) |
| Icon |
Gets or sets the icon for the form. |
| ImeMode |
Gets or sets the Input Method Editor (IME) mode of the control. (Inherited from Control) |
| ImeModeBase |
Gets or sets the IME mode of a control. (Inherited from Control) |
| InvokeRequired |
Gets a value indicating whether the caller must call an invoke method when making method calls to the control because the caller is on a different thread than the one the control was created on. (Inherited from Control) |
| IsAccessible |
Gets or sets a value indicating whether the control is visible to accessibility applications. (Inherited from Control) |
| IsAncestorSiteInDesignMode |
Indicates if one of the Ancestors of this control is sited and that site in DesignMode. This property is read-only. (Inherited from Control) |
| IsDisposed |
Gets a value indicating whether the control has been disposed of. (Inherited from Control) |
| IsHandleCreated |
Gets a value indicating whether the control has a handle associated with it. (Inherited from Control) |
| IsMdiChild |
Gets a value indicating whether the form is a multiple-document interface (MDI) child form. |
| IsMdiContainer |
Gets or sets a value indicating whether the form is a container for multiple-document interface (MDI) child forms. |
| IsMirrored |
Gets a value indicating whether the control is mirrored. (Inherited from Control) |
| IsRestrictedWindow |
Gets a value indicating whether the form can use all windows and user input events without restriction. |
| KeyPreview |
Gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus. |
| LayoutEngine |
Gets a cached instance of the control's layout engine. (Inherited from Control) |
| Left |
Gets or sets the distance, in pixels, between the left edge of the control and the left edge of its container's client area. (Inherited from Control) |
| Location |
Gets or sets the Point that represents the upper-left corner of the Form in screen coordinates. |
| Location |
Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container. (Inherited from Control) |
| MainMenuStrip |
Gets or sets the primary menu container for the form. |
| Margin |
Gets or sets the space between controls. |
| MaximizeBox |
Gets or sets a value indicating whether the Maximize button is displayed in the caption bar of the form. |
| MaximizedBounds |
Gets or sets the size of the form when it is maximized. |
| MaximumSize |
Gets the maximum size the form can be resized to. |
| MdiChildren |
Gets an array of forms that represent the multiple-document interface (MDI) child forms that are parented to this form. |
| MdiChildrenMinimizedAnchorBottom |
Gets or sets the anchoring for minimized MDI children. |
| MdiParent |
Gets or sets the current multiple-document interface (MDI) parent form of this form. |
| Menu |
Obsolete.
Gets or sets the MainMenu that is displayed in the form. |
| MergedMenu |
Obsolete.
Gets the merged menu for the form. |
| MinimizeBox |
Gets or sets a value indicating whether the Minimize button is displayed in the caption bar of the form. |
| MinimumSize |
Gets or sets the minimum size the form can be resized to. |
| Modal |
Gets a value indicating whether this form is displayed modally. |
| Name |
Gets or sets the name of the control. (Inherited from Control) |
| Opacity |
Gets or sets the opacity level of the form. |
| OwnedForms |
Gets an array of Form objects that represent all forms that are owned by this form. |
| Owner |
Gets or sets the form that owns this form. |
| Padding |
Gets or sets padding within the control. (Inherited from Control) |
| Parent |
Gets or sets the parent container of the control. (Inherited from Control) |
| ParentForm |
Gets the form that the container control is assigned to. (Inherited from ContainerControl) |
| PreferredSize |
Gets the size of a rectangular area into which the control can fit. (Inherited from Control) |
| ProductName |
Gets the product name of the assembly containing the control. (Inherited from Control) |
| ProductVersion |
Gets the version of the assembly containing the control. (Inherited from Control) |
| RecreatingHandle |
Gets a value indicating whether the control is currently re-creating its handle. (Inherited from Control) |
| Region |
Gets or sets the window region associated with the control. (Inherited from Control) |
| RenderRightToLeft |
Obsolete.
Obsolete.
This property is now obsolete. (Inherited from Control) |
| ResizeRedraw |
Gets or sets a value indicating whether the control redraws itself when resized. (Inherited from Control) |
| RestoreBounds |
Gets the location and size of the form in its normal window state. |
| Right |
Gets the distance, in pixels, between the right edge of the control and the left edge of its container's client area. (Inherited from Control) |
| RightToLeft |
Gets or sets a value indicating whether control's elements are aligned to support locales using right-to-left fonts. (Inherited from Control) |
| RightToLeftLayout |
Gets or sets a value indicating whether right-to-left mirror placement is turned on. |
| ScaleChildren |
Gets a value that determines the scaling of child controls. (Inherited from Control) |
| ShowFocusCues |
Gets a value indicating whether the control should display focus rectangles. (Inherited from Control) |
| ShowIcon |
Gets or sets a value indicating whether an icon is displayed in the caption bar of the form. |
| ShowInTaskbar |
Gets or sets a value indicating whether the form is displayed in the Windows taskbar. |
| ShowKeyboardCues |
Gets a value indicating whether the user interface is in the appropriate state to show or hide keyboard accelerators. (Inherited from Control) |
| ShowWithoutActivation |
Gets a value indicating whether the window will be activated when it is shown. |
| Site |
Gets or sets the site of the control. (Inherited from Control) |