Windows Forms Controls
This section provides comprehensive documentation on the various controls available in the Windows Forms library. Windows Forms provides a rich set of user interface elements that allow you to build professional-looking desktop applications for Windows.
Overview
Windows Forms controls are objects that can be placed on a form to provide a user interface. They are essentially managed wrappers around underlying Windows controls, providing a rich, object-oriented programming model for creating Windows-based applications.
Common categories of controls include:
- Display Controls: Used for displaying text and images (e.g.,
Label,PictureBox,Panel). - Input Controls: Used for user input (e.g.,
TextBox,NumericUpDown,CheckBox,RadioButton). - Navigation Controls: Used for navigating between different parts of an application (e.g.,
TabControl,TreeView,ListView). - Action Controls: Used for initiating actions (e.g.,
Button,LinkButton,MenuItem). - Container Controls: Used for grouping other controls (e.g.,
GroupBox,Panel,SplitContainer).
Key Controls and Their Usage
Button Control
The Button control is one of the most fundamental controls. It is used to trigger an action when clicked by the user.
Example: Adding a Button
To add a button to your form and handle its click event:
// In your form's designer code-behind (e.g., MyForm.cs)
public partial class MyForm : Form
{
private Button myButton;
public MyForm()
{
InitializeComponent();
myButton = new Button();
myButton.Text = "Click Me";
myButton.Location = new Point(50, 50);
myButton.Click += MyButton_Click; // Attach event handler
this.Controls.Add(myButton);
}
private void MyButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Button was clicked!");
}
}
TextBox Control
The TextBox control allows users to enter and display text. It supports multiline input, scrolling, and password masking.
Multiline property to true for multiline text entry.
Label Control
The Label control is used to display static text, such as captions for other controls or descriptive information.
DataGridView Control
The DataGridView control provides a powerful and flexible way to display and edit tabular data. It supports features like sorting, filtering, and data binding.
Customizing Controls
Most controls offer a wide range of properties to customize their appearance and behavior. These include:
TextorCaption: The text displayed by the control.ForeColorandBackColor: The text and background colors.Font: The font used for text.SizeandLocation: The dimensions and position on the form.Enabled: Whether the control can respond to user interaction.Visible: Whether the control is displayed on the form.
Events
Controls expose various events that you can handle to respond to user actions or system changes. Common events include:
Click: Fired when a button is clicked.TextChanged: Fired when the text in aTextBoxchanges.SelectedIndexChanged: Fired when the selected item in a list-based control changes.Paint: Fired when the control needs to be redrawn.