Windows Forms API Reference

This section provides comprehensive documentation for the Windows Forms API, a framework for building rich client applications for Windows.

Namespaces

Namespace: System.Windows.Forms

Contains classes and interfaces that represent the core Windows Forms functionality.

Key Classes

Namespace: System.Drawing

Provides classes for GDI+ graphics functionality used by Windows Forms.

Key Classes

Namespace: System.ComponentModel

Provides classes and interfaces that enable runtime and design-time attribute behavior for components.

Key Classes

Class: Control

Represents the base class for all Windows Forms controls.

Inheritance

System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control

Methods

Properties

Events

Method: Control.CreateGraphics()

Creates a Graphics object that you can use to draw on the control.

Signature

public System.Drawing.Graphics CreateGraphics();

Return Value

A System.Drawing.Graphics object for drawing on the control.

Remarks

You can use the Graphics object returned by this method to draw custom content on the control. Remember to dispose of the Graphics object when you are finished with it.

Example:

using (Graphics g = this.CreateGraphics())
{
    g.DrawString("Hello, Windows Forms!", this.Font, Brushes.Blue, 10, 10);
}

Method: Control.Dispose()

Releases the unmanaged resources used by the control and, optionally, releases the managed resources.

Signature

protected virtual void Dispose(bool disposing);

Remarks

It is important to call Dispose() on controls when they are no longer needed to free up system resources.

Method: Control.Refresh()

Forces the control to re-render its surface.

Signature

public void Refresh();

Remarks

This method forces a repaint of the control by invalidating the control and processing pending repaint messages. It is often used when the control's content needs to be updated immediately.

Method: Control.Invalidate()

Invalidates the entire control and causes a re-paint.

Signature

public void Invalidate();

Remarks

Calling Invalidate() marks the control's client area as needing to be redrawn. The system will then automatically generate a Paint event for the control.

Property: Control.Text

Gets or sets the text associated with this control.

Signature

public string Text { get; set; }

Remarks

This property is commonly used to set the label for a control or the text displayed in a form's title bar.

Property: Control.Name

Gets or sets the name of the control.

Signature

public string Name { get; set; }

Remarks

The Name property is used to identify controls, especially at design time and when accessing controls programmatically by name.

Property: Control.Size

Gets or sets the height and width of the control.

Signature

public System.Drawing.Size Size { get; set; }

Remarks

The size is represented by a System.Drawing.Size structure, which contains Width and Height properties.

Property: Control.Location

Gets or sets the coordinates of the upper-left corner of the control relative to the upper-left corner of its container.

Signature

public System.Drawing.Point Location { get; set; }

Remarks

The location is represented by a System.Drawing.Point structure, which contains X and Y properties.

Property: Control.Visible

Gets or sets a value indicating whether the control is visible.

Signature

public bool Visible { get; set; }

Remarks

Setting this property to false will hide the control. Setting it to true will show the control.

Property: Control.Enabled

Gets or sets a value indicating whether the control can respond to user interaction.

Signature

public bool Enabled { get; set; }

Remarks

When a control is disabled, it typically appears grayed out and does not respond to events like clicks.

Event: Control.Click

Occurs when the control is clicked.

Signature

public event EventHandler Click;

Remarks

This event is typically handled to perform an action when a button is clicked, a menu item is selected, or other clickable controls are interacted with.

Event: Control.Paint

Occurs when the control is redrawn.

Signature

public event PaintEventHandler Paint;

Remarks

The Paint event handler receives a PaintEventArgs object, which contains a Graphics object for drawing and a rectangle representing the area to be painted.

Example:

private void MyControl_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawRectangle(Pens.Red, 5, 5, 50, 50);
}

Event: Control.MouseMove

Occurs when the mouse pointer moves over the control.

Signature

public event MouseEventHandler MouseMove;

Remarks

The MouseMove event handler receives a MouseEventArgs object containing information about the mouse pointer's position and any modifier keys pressed.

Class: Form

Represents a window, which is a rectangular message-receiving surface on the screen.

Inheritance

System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ScrollableControl
System.Windows.Forms.ContainerControl
System.Windows.Forms.Form

Properties

Events

Event: Form.Load

Occurs when the form is first loaded.

Signature

public event EventHandler Load;

Remarks

This event is an excellent place to perform initialization tasks for your form, such as loading data or setting up controls.

Event: Form.FormClosing

Occurs when the form is closing.

Signature

public event FormClosingEventHandler FormClosing;

Remarks

The FormClosing event handler can be used to cancel the closing operation or to save unsaved data.

Class: Button

Represents a standard Windows button control.

Inheritance

System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ButtonBase
System.Windows.Forms.Button

Properties

Events

Property: Button.DialogResult

Gets or sets the return value of the form when the button is clicked.

Signature

public System.Windows.Forms.DialogResult DialogResult { get; set; }

Remarks

This property is particularly useful for buttons within dialog boxes (e.g., OK, Cancel, Yes, No) to programmatically close the form with a specific result.

Class: TextBox

Represents a Windows control that allows the user to edit and display text.

Inheritance

System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.TextBoxBase
System.Windows.Forms.TextBox

Properties

Events

Event: TextBox.TextChanged

Occurs when the text in the text box changes.

Signature

public event EventHandler TextChanged;

Remarks

This event is useful for validating user input or performing actions based on the current text in the TextBox.

Class: Label

Represents a Windows control that displays a text label.

Inheritance

System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.Label

Properties

Class: Panel

Represents a control that is a rectangular region of a form and serves as a container for other controls.

Inheritance

System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ScrollableControl
System.Windows.Forms.Panel

Properties

Class: DataGridView

Represents a Windows Forms control for displaying and editing tabular data.

Inheritance

System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ScrollableControl
System.Windows.Forms.DataGridView

Properties

Events

Struct: Point

Represents an ordered pair of x and y coordinates, which defines a point in two-dimensional space.

Fields

  • public int X
  • public int Y

Struct: Size

Represents the width and height of a rectangular region.

Fields

  • public int Width
  • public int Height

Struct: Rectangle

Represents a generic rectangle defined by a location and size.

Fields

  • public int X
  • public int Y
  • public int Width
  • public int Height

Struct: Color

Represents an RGBA color.

Static Properties

  • public static Color Empty
  • public static Color Black
  • public static Color White
  • public static Color Red
  • public static Color Blue
  • public static Color Green

Class: Font

Represents a draft of a font that can be used to display text.

Constructors

  • public Font(string familyName, float emSize);
  • public Font(string familyName, float emSize, FontStyle style);

Properties

  • public string Name
  • public float Size
  • public FontStyle Style

Class: Graphics

Provides methods for performing complex drawing and visual manipulation on a graphics display.

Methods

  • public void DrawLine(Pen pen, int x1, int y1, int x2, int y2);
  • public void DrawRectangle(Pen pen, int x, int y, int width, int height);
  • public void FillRectangle(Brush brush, int x, int y, int width, int height);
  • public void DrawString(string s, Font font, Brush brush, float x, float y);

Class: Attribute

Base class for attributes.

Remarks

Attributes provide declarative information about code elements.

Class: Component

Implements the IComponent interface and provides the foundation for creating Windows Forms components.

Properties

  • public ISite Site { get; set; }