Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C# Windows Forms Development: A Hands‑On Tutorial

C# Windows Forms Development: A Hands‑On Tutorial

After mastering console applications, the next logical step is to create rich desktop experiences using Visual Studio’s Windows Forms. This guide walks you through every stage—from project setup to adding controls, wiring events, and even incorporating advanced components like TreeView and PictureBox.

Windows Forms Basics

A Windows Forms application runs natively on a desktop machine. It comprises a visual layout and a set of controls (labels, text boxes, list boxes, etc.) that the user interacts with.

Below is a simple login form created in C#:

C# Windows Forms Development: A Hands‑On Tutorial

Key elements in the example:

  1. Label controls that describe adjacent text boxes.
  2. Text boxes for username and password input.
  3. A button that triggers authentication logic.

C# Hello World

Let’s create a basic “Hello World” Windows Form to understand the Visual Studio workflow.

  1. Open Visual Studio and select File > New > Project.

    C# Windows Forms Development: A Hands‑On Tutorial

  2. Choose the Windows category and select Windows Forms App (.NET Framework). Name the project DemoApplication and click OK.

    C# Windows Forms Development: A Hands‑On Tutorial

  3. The Solution Explorer will show two files:
    • Form1.cs – the visual form.
    • Program.cs – application entry point.
  4. Add a Label from the Toolbox, drag it onto the form, and set its Text property to Hello World.

    C# Windows Forms Development: A Hands‑On Tutorial

  5. Run the application – the form displays “Hello World”.

    C# Windows Forms Development: A Hands‑On Tutorial

Adding Controls to a Form

Below is a step‑by‑step guide to assembling a user‑information form that captures name, address, city, gender, and course preferences.

GroupBox – Logical Sectioning

Drag a GroupBox onto the form and rename its Text property to User Details.

C# Windows Forms Development: A Hands‑On Tutorial

Label

Place two labels inside the GroupBox: “Name” and “Address”.

C# Windows Forms Development: A Hands‑On Tutorial

TextBox

Add two text boxes, naming them txtName and txtAddress.

C# Windows Forms Development: A Hands‑On Tutorial

ListBox – City Selection

Insert a ListBox named lstCity and populate it with “Mumbai”, “Bangalore”, and “Hyderabad”.

C# Windows Forms Development: A Hands‑On Tutorial

RadioButton – Gender

Place two RadioButton controls: rdMale (Text: Male) and rdFemale (Text: Female).

C# Windows Forms Development: A Hands‑On Tutorial

CheckBox – Course Preference

Add two CheckBox controls: chkC (Text: C#) and chkASP (Text: ASP.NET).

C# Windows Forms Development: A Hands‑On Tutorial

Button – Submit

Drop a Button named btnSubmit with Text set to “Submit”.

C# Windows Forms Development: A Hands‑On Tutorial

Event Handling for Controls

Events fire in response to user actions. Below are two common scenarios.

ListBox Selection Changed

Double‑click the lstCity control to generate an event handler. Add the following code:

private void lstCity_SelectedIndexChanged(object sender, EventArgs e)
{
    var selected = lstCity.SelectedItem?.ToString();
    if (!string.IsNullOrEmpty(selected))
        MessageBox.Show($"You selected: {selected}");
}

Running the app will show a message box whenever a city is chosen.

Button Click – Submit

Double‑click btnSubmit to create its handler and insert:

private void btnSubmit_Click(object sender, EventArgs e)
{
    var name = txtName.Text;
    var address = txtAddress.Text;
    MessageBox.Show($"Name: {name}\nAddress: {address}");
}

When you click “Submit”, the entered details are displayed.

TreeView and PictureBox Controls

TreeView – Hierarchical Data

Drag a TreeView onto the form. Use the Nodes property to add a root node labeled “Root”, then add child nodes “Label”, “Button”, and “CheckBox”.

C# Windows Forms Development: A Hands‑On Tutorial

PictureBox – Display Images

Insert a PictureBox, set its SizeMode to Zoom, and assign an image via the Image property.

C# Windows Forms Development: A Hands‑On Tutorial

Summary

C Language

  1. C++ For Loops Explained: Syntax, Workflow, and Practical Examples
  2. C++ Structs Explained with a Practical Example
  3. Mastering std::list in C++: Syntax, Functions & Practical Examples
  4. C# Enumerations (Enums) – Definition, Example, and Usage
  5. C# Abstract Classes: A Practical Tutorial with Code Examples
  6. Understanding C# Interfaces: Definition, Example, and Practical Use
  7. Mastering C# File Streams: StreamReader & StreamWriter – Step‑by‑Step Guide
  8. Master Java Reflection API: A Practical Guide with Code Examples
  9. Master Python Unit Testing with PyUnit: A Practical Guide & Example
  10. Python Calendar Module: Expert Guide with Code Examples