Desktop Controls (.NET)
This section provides comprehensive documentation on the various controls available for building rich Windows desktop applications using the .NET Framework and .NET Core.
Introduction to Controls
Controls are the fundamental building blocks of a graphical user interface (GUI). They represent visual elements that users interact with, such as buttons, text boxes, labels, and lists. .NET provides a rich set of pre-built controls that simplify the development of user-friendly Windows applications.
Key Control Categories
The available controls can be broadly categorized based on their functionality:
- Basic Controls: For input and display of simple data (e.g.,
TextBox,Label,Button,CheckBox,RadioButton). - Selection Controls: For allowing users to choose from a list of options (e.g.,
ListBox,ComboBox,ListView,TreeView). - Navigation Controls: For organizing and navigating through application content (e.g.,
TabControl,MenuStrip,ToolStrip). - Information Controls: For displaying status or providing feedback (e.g.,
ProgressBar,ToolTip,StatusBar). - Container Controls: For grouping other controls and managing layout (e.g.,
Panel,GroupBox,Form). - Data-Bound Controls: Designed for seamless integration with data sources (e.g.,
DataGridView).
Commonly Used Controls
Button Control
The Button control is used to initiate an action when clicked by the user.
Class:
Button
Properties:
Text: The text displayed on the button.Enabled: Determines if the button can be interacted with.BackColor,ForeColor: Control button colors.
Events:
Click: Occurs when the button is clicked.
Example: Handling Button Click
using System;
using System.Windows.Forms;
public class MyForm : Form
{
private Button myButton;
public MyForm()
{
myButton = new Button();
myButton.Text = "Click Me";
myButton.Location = new System.Drawing.Point(50, 50);
myButton.Click += new EventHandler(MyButton_Click);
this.Controls.Add(myButton);
}
private void MyButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Button was clicked!");
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyForm());
}
}
TextBox Control
The TextBox control allows users to enter and display text. It supports single-line and multi-line input.
Class:
TextBox
Properties:
Text: The current text in the text box.Multiline: Set totruefor multi-line input.ReadOnly: Iftrue, the user cannot edit the text.PasswordChar: Character used to mask input for password fields.
Example: Getting Text from a TextBox
string userInput = myTextBox.Text;
MessageBox.Show($"You entered: {userInput}");
Label Control
The Label control is used to display static text, such as titles, descriptions, or instructions, to the user.
Class:
Label
Properties:
Text: The text to display.AutoSize: Iftrue, the label resizes to fit its text.Font: Specifies the font for the text.
Designing Your UI
You can add controls to your forms using the Visual Studio Designer or programmatically. The designer provides a drag-and-drop interface for arranging controls and setting their properties visually.
Tips for effective UI design:
- Use clear and concise labels.
- Group related controls logically using containers.
- Provide visual feedback for user actions.
- Ensure consistent layout and styling.
- Consider accessibility requirements.
Further Reading
Explore the links below for detailed information on specific controls and advanced UI design techniques: