Desktop App Development - Textnotes

Desktop App Development


C# allows you to build desktop applications using WinForms and WPF, each with its own approach to UI, data binding, and architecture.

1. WinForms Basics

WinForms is a Windows Forms-based UI framework for building desktop apps quickly.

1.1 Controls & Events

Common controls: Button, TextBox, Label, ComboBox, DataGridView.


using System;
using System.Windows.Forms;

public class MyForm : Form
{
private Button btnClick;
private Label lblMessage;

public MyForm()
{
btnClick = new Button { Text = "Click Me", Left = 50, Top = 50 };
lblMessage = new Label { Text = "", Left = 50, Top = 100 };

btnClick.Click += BtnClick_Click;

Controls.Add(btnClick);
Controls.Add(lblMessage);
}

private void BtnClick_Click(object sender, EventArgs e)
{
lblMessage.Text = "Button clicked!";
}

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

1.2 Forms & Dialogs

  1. Forms are windows in WinForms.
  2. Dialogs: MessageBox, OpenFileDialog, SaveFileDialog.

private void ShowDialogExample()
{
DialogResult result = MessageBox.Show("Do you want to continue?", "Confirmation", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
MessageBox.Show("You clicked Yes");
}

1.3 Database Connectivity in WinForms

Using ADO.NET:


using System.Data.SqlClient;

private void LoadData()
{
string connectionString = @"Server=.;Database=TestDB;Trusted_Connection=True;";
string query = "SELECT * FROM Students";
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);

dataGridView1.DataSource = dt;
}
}

2. WPF Basics

WPF (Windows Presentation Foundation) is XAML-based and supports rich UI, animation, and data binding.

2.1 XAML Introduction

XAML = eXtensible Application Markup Language

Defines UI layout declaratively.


<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="MainWindow" Height="200" Width="300">
<StackPanel>
<TextBox Name="txtName" Width="200" Margin="10"/>
<Button Content="Click Me" Width="100" Click="Button_Click"/>
<TextBlock Name="lblMessage" Margin="10"/>
</StackPanel>
</Window>

private void Button_Click(object sender, RoutedEventArgs e)
{
lblMessage.Text = "Hello " + txtName.Text;
}

2.2 Data Binding

Bind UI controls to data sources:


<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Width="200"/>
<TextBlock Text="{Binding Name}"/>

public class Student
{
public string Name { get; set; }
}

DataContext = new Student { Name = "John" };

2.3 MVVM Pattern

  1. Model: Represents data (classes).
  2. View: XAML UI.
  3. ViewModel: Handles logic and exposes data for binding.

public class StudentViewModel : INotifyPropertyChanged
{
private string _name;
public string Name
{
get => _name;
set { _name = value; OnPropertyChanged(nameof(Name)); }
}

public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

Bind in XAML:


<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Text="{Binding Name}"/>

2.4 Commands & Events

  1. Commands replace event handlers for MVVM-friendly code.

public class RelayCommand : ICommand
{
private readonly Action _execute;
public RelayCommand(Action execute) => _execute = execute;

public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => true;
public void Execute(object parameter) => _execute();
}

Bind a button:


<Button Content="Click Me" Command="{Binding MyCommand}"/>

MyCommand = new RelayCommand(() => MessageBox.Show("Button Clicked"));

Summary of Chapter 15:

  1. WinForms: Quick UI, controls, events, dialogs, and database integration using ADO.NET.
  2. WPF: XAML-based UI, rich controls, data binding, MVVM architecture.
  3. MVVM: Separates UI, logic, and data for maintainable applications.
  4. Commands & Events: Support event handling in MVVM-friendly way.