Buttons, Labels, and TextBoxes in Windows Forms
Windows Forms makes it easy to build interactive desktop apps with drag-and-drop controls β but you can also create and place controls using code! This is a great way to learn how the UI works under the hood and gives you more dynamic control.
π§° What Are UI Controls?
UI controls are the building blocks of your applicationβs interface β things like buttons, labels, and text boxes. While Visual Studio lets you drag them onto a form, you can also create them directly in C# code using the System.Windows.Forms namespace.
π Getting Started
π Requirements:
- Visual Studio 2022 (or newer)
- .NET Desktop Development workload
β¨ Creating a WinForms Project:
- Open Visual Studio
- Click Create a new project
- Search for Windows Forms App (.NET)
- Select C#, name your project, and click Create
β Youβll see a blank form β letβs add controls using code!
π§± Drawing Controls in Code
You can create and place controls like this:
Label label1 = new Label();
label1.Text = "Name:";
label1.Location = new Point(10, 20);
label1.AutoSize = true;
this.Controls.Add(label1);
β Most common UI controls:
| Control | What it does |
|---|---|
Label | Shows static text |
TextBox | Takes text input from the user |
Button | Triggers actions when clicked |
βοΈ Example: Hello App Using Code
Letβs create a simple app that says hello when a button is clicked.
π§© Full Example:
using System;
using System.Drawing;
using System.Windows.Forms;
public class HelloForm : Form
{
public HelloForm()
{
this.Text = "Hello App";
Label lbl = new Label();
lbl.Text = "Enter your name:";
lbl.Location = new Point(10, 20);
lbl.AutoSize = true;
this.Controls.Add(lbl);
TextBox txt = new TextBox();
txt.Location = new Point(130, 18);
txt.Width = 150;
this.Controls.Add(txt);
Button btn = new Button();
btn.Text = "Say Hello";
btn.Location = new Point(10, 60);
this.Controls.Add(btn);
Label output = new Label();
output.Location = new Point(10, 100);
output.AutoSize = true;
this.Controls.Add(output);
btn.Click += (s, e) => {
output.Text = "Hello, " + txt.Text + "!";
};
}
[STAThread]
public static void Main()
{
Application.Run(new HelloForm());
}
}
π§ How It Works
- You create each control with
new - You set properties like
.Text,.Location, and.Size - You add them to the form with
this.Controls.Add() - You can handle events like
Clickto respond to user actions
π Project Structure Review
| File/Folder | What it does |
|---|---|
Program.cs | Starts the application |
Form1.cs or HelloForm.cs | Contains the control setup & logic |
bin/ | Compiled app |
obj/ | Temp build files |
β Best Practices
- β
Use clear, meaningful control names (
txtName,btnSubmit) - β Keep control setup separate from logic where possible
- β
Use layout features like
Anchor,Dockfor resizing - β
Group related controls with
PanelorGroupBox
π§ͺ Quick Challenge
π§© Create a small app that:
- Takes two text inputs: Name & City
- Shows a message like:
"Hi Mia from London!"
π‘ Hints:
- Use two
TextBoxcontrols - One
Button - One
Labelfor output - Use
+= (s, e) => {}to handle theClickevent
πΌ Visual Guide
-------------------------------
| Name: [__________] |
| City: [__________] |
| |
| [ Show Message ] |
| |
| Hi Mia from London! |
-------------------------------
π Summary
| Concept | Description |
|---|---|
| UI Controls | Elements like buttons, text boxes, labels |
| Events | Code that runs when users interact |
Controls.Add | Adds controls to the form dynamically |
Point | Sets position of controls (x, y) |
+= | Attaches code to events like Click |
π Want to Build More?
- β Make a calculator app
- β Build a simple login screen
- β
Add image support with
PictureBox - β Create multi-form apps
- β Combine with file save/load or SQLite database
π¬ Got questions? Want help building your first dynamic UI?
Weβre here to help you bring your ideas to life, one control at a time.