Desktop Controls in .NET

This document provides a comprehensive overview of the standard desktop controls available in the .NET Framework and .NET (Core/5+), focusing on their usage and common scenarios within Windows Forms and WPF applications.

Introduction to Desktop Controls

Desktop controls are the building blocks of graphical user interfaces (GUIs). They provide interactive elements that users can interact with, such as buttons, text boxes, menus, and lists. .NET offers a rich set of controls that allow developers to quickly and efficiently build robust desktop applications.

Windows Forms Controls

Windows Forms (WinForms) is a UI framework for building Windows desktop applications. It provides a drag-and-drop designer in Visual Studio and a large collection of pre-built controls.

Commonly Used WinForms Controls:

Example: Adding a Button and Label in WinForms

C# Code Snippet


using System.Windows.Forms;

public class MyForm : Form
{
    private Button myButton;
    private Label myLabel;

    public MyForm()
    {
        // Initialize controls
        myButton = new Button();
        myLabel = new Label();

        // Configure controls
        myButton.Text = "Click Me";
        myButton.Location = new System.Drawing.Point(50, 50);
        myButton.Click += MyButton_Click; // Event handler

        myLabel.Text = "Hello, World!";
        myLabel.Location = new System.Drawing.Point(50, 100);
        myLabel.AutoSize = true;

        // Add controls to the form
        this.Controls.Add(myButton);
        this.Controls.Add(myLabel);

        // Form properties
        this.Text = "WinForms Example";
        this.Size = new System.Drawing.Size(300, 200);
    }

    private void MyButton_Click(object sender, System.EventArgs e)
    {
        myLabel.Text = "Button Clicked!";
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}
            

WPF Controls

Windows Presentation Foundation (WPF) is a more modern UI framework for building Windows desktop applications. It uses XAML for declarative UI definition and offers greater flexibility in styling and data binding.

Commonly Used WPF Controls:

Example: XAML for WPF Controls

XAML Code Snippet


<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WPF Example" Height="300" Width="400">
    <Grid Margin="20">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel Orientation="Vertical" Grid.Row="0">
            <Label Content="Enter your name:"/>
            <TextBox x:Name="nameTextBox" Width="200" HorizontalAlignment="Left"/>
            <Button Content="Greet Me" Click="GreetButton_Click" Margin="0,10,0,0"/>
        </StackPanel>

        <TextBlock x:Name="greetingTextBlock" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="16"/>
    </Grid>
</Window>
            

C# Code-Behind


using System.Windows;

namespace WpfApp
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void GreetButton_Click(object sender, RoutedEventArgs e)
        {
            greetingTextBlock.Text = $"Hello, {nameTextBox.Text}!";
        }
    }
}
            

Key Concepts

Choosing Between WinForms and WPF

While both frameworks serve desktop development, consider these points: