Windows Forms APIs
This section provides comprehensive documentation for the Windows Forms APIs within the .NET Framework. Windows Forms is a rich UI framework for building desktop applications on Windows.
Introduction to Windows Forms
Windows Forms provides a managed code interface to the underlying Windows user interface (UI) elements. It allows you to build sophisticated Windows applications quickly and efficiently using the power of the .NET Framework.
Key features include:
- A vast library of pre-built controls.
- A powerful event-driven programming model.
- Design-time support for rapid application development.
- Extensibility for creating custom controls.
Core Controls
The foundation of any Windows Forms application is its set of controls. These are UI elements that users interact with.
Layout Management
Effective layout management is crucial for creating resizable and user-friendly forms. Windows Forms offers several mechanisms to achieve this.
- FlowLayoutPanel: Arranges controls in a horizontal or vertical flow.
- TableLayoutPanel: Arranges controls in a grid of rows and columns.
- Anchor and Dock Properties: Controls how child controls resize and reposition when their parent container is resized.
Example of docking:
public partial class MyForm : Form
{
private Button okButton;
public MyForm()
{
InitializeComponent();
okButton = new Button();
okButton.Text = "OK";
okButton.Dock = DockStyle.Bottom; // Dock the button to the bottom of the form
this.Controls.Add(okButton);
}
}
Data Binding
Windows Forms provides a robust data binding model that allows you to easily connect UI controls to data sources.
- BindingSource Component: A versatile component that acts as an intermediary between controls and data sources.
- Data Sources: Supports various data sources including arrays, lists, DataTables, and custom objects.
- Two-Way Data Binding: Changes in the UI automatically update the data source, and vice-versa.
Graphics and Drawing
The System.Drawing
namespace offers powerful capabilities for custom drawing and graphics manipulation.
- Graphics Object: Provides methods for drawing shapes, lines, text, and images.
- Pens and Brushes: Used to define the appearance of drawing operations (color, style, fill).
- Paint Event: The primary event for performing custom drawing on a control.
Basic drawing example:
private void MainForm_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
using (Pen redPen = new Pen(Color.Red, 2))
{
g.DrawRectangle(redPen, 50, 50, 100, 75); // Draw a red rectangle
}
}
Event Handling
Windows Forms is an event-driven framework. Controls generate events in response to user actions or system occurrences, and your application code handles these events.
Common events include:
- Click: Fired when a button or menu item is clicked.
- TextChanged: Fired when the text in a TextBox changes.
- SelectedIndexChanged: Fired when the selected item in a ListBox or ComboBox changes.
- Paint: Fired when a control needs to be redrawn.
Adding an event handler in code:
public partial class MyForm : Form
{
private Button submitButton;
public MyForm()
{
InitializeComponent();
submitButton = new Button();
submitButton.Text = "Submit";
submitButton.Click += SubmitButton_Click; // Attach event handler
this.Controls.Add(submitButton);
}
private void SubmitButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked!");
}
}
Dialogs
Windows Forms provides a set of standard dialog boxes for common tasks such as opening files, saving files, selecting colors, and displaying messages.
Common Dialogs
Customization and Extensibility
You can extend Windows Forms by creating your own custom controls or by customizing existing ones.
- User Controls: Combine existing controls to create reusable components.
- Custom Controls: Inherit from base control classes like
Control
orUserControl
to build entirely new controls with unique functionality and appearance. - Owner Drawing: Override painting methods to achieve highly customized visual elements.