Form Class

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.

Syntax


public class Form : Control, IButtonControl, IContainerControl, ISite, IWin32Window, IDisposable, IOleObject, IPersistStream, ISerializable, ISupportInitialize
            

Members

The Form class inherits from Control and implements several interfaces, providing a rich set of properties, methods, and events for creating sophisticated user interfaces.

Properties

Methods

Events

Property: Text

Gets or sets the text displayed in the form's title bar.

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.

Property: Size

Gets or sets the width and height of the form.

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);
        

Method: Show()

Displays the form to the user.

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();
        

Event: Load

Occurs when the form is first loaded.

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!");
}