Namespace: System.Windows.Forms
Represents a window, which is a separate screen that typically contains a title bar, border, and buttons for minimizing, maximizing, and closing. The Form class is the fundamental building block for Windows Forms applications, providing a surface for controls and user interaction.
public class Form : Control, IButtonControl, IContainerControl, ISite, IWin32Window, IDisposable, IOleObject, IPersistStream, ISerializable, ISupportInitialize
The Form
class inherits from Control
and implements several interfaces, providing a rich set of properties, methods, and events for creating sophisticated user interfaces.
Text
: Gets or sets the text displayed in the form's title bar.Size
: Gets or sets the width and height of the form.Location
: Gets or sets the coordinates of the upper-left corner of the form.StartPosition
: Gets or sets the value that specifies how the form is displayed when it is first shown.FormBorderStyle
: Gets or sets the style of the form's border.ControlBox
: Gets or sets a value indicating whether the form has a control box.MinimizeBox
: Gets or sets a value indicating whether the form has a minimize button.MaximizeBox
: Gets or sets a value indicating whether the form has a maximize button.Icon
: Gets or sets the icon displayed in the form's title bar and taskbar.AcceptButton
: Gets or sets the button control that is clicked when the user presses the ENTER key.CancelButton
: Gets or sets the button control that is clicked when the user presses the ESC key.Opacity
: Gets or sets the opacity of the form.Show()
: Displays the form to the user.ShowDialog()
: Shows the form as a modal dialog box.Close()
: Closes the form.Activate()
: Activates the form.BringToFront()
: Brings the form to the front of the z-order.SendToBack()
: Sends the form to the back of the z-order.Dispose()
: Releases the unmanaged resources used by the Control and its child controls.Load
: Occurs when the form is first loaded.Shown
: Occurs when the form is first displayed.Activated
: Occurs when the form is activated.Deactivate
: Occurs when the form is deactivated.Closing
: Occurs when the form is closing.FormClosed
: Occurs after the form has been closed.Paint
: Occurs when the control is redrawn.Resize
: Occurs when the form is resized.Click
: Occurs when the user clicks the form.
public string Text { get; set; }
This property is commonly used to set the title of the window, which often indicates the application's name or the current document being edited.
public Size Size { get; set; }
The Size
property takes a System.Drawing.Size
structure, which contains Width
and Height
properties.
// Example: Setting the form size
this.Size = new System.Drawing.Size(800, 600);
public void Show();
Calling this method makes the form visible. If the form is shown modally, this method will not return until the form is closed. For non-modal forms, it returns immediately.
// Example: Showing a non-modal form
MyForm newForm = new MyForm();
newForm.Show();
public event EventHandler Load;
The Load
event is typically used to perform initialization tasks for the form, such as loading data, setting initial control states, or configuring layout.
// Example: Handling the Load event
public MyForm()
{
InitializeComponent();
this.Load += new EventHandler(MyForm_Load);
}
private void MyForm_Load(object sender, EventArgs e)
{
// Perform initialization tasks here
MessageBox.Show("Form has loaded!");
}