Windows Forms: Your First Desktop Form
This section starts the C# part of the lab. You will build a real desktop window in Visual Studio: drag controls onto a form, set their properties, and write the few lines of code that run when the user clicks a button or picks an item from a list. Every program in this section was built and run on Windows, and every screenshot below is from those runs.
Objectives
By the end of the section you should be able to:
- Explain what a Windows Forms application is, and what a form and a control are.
- Create a Windows Forms project and find the Form Designer, the Solution Explorer, the Toolbox and the Properties window.
- Build a "Hello World" form by placing a Label and changing its Text property.
- Add the common controls (GroupBox, Label, TextBox, ListBox, RadioButton, CheckBox, Button) and give each one a meaningful Name.
- Explain what an event and an event handler are, and create a handler by double-clicking a control.
- Write a ListBox handler that shows the selected item, and a Button handler that reads what the user typed.
- Fill a TreeView with nodes and show a picture in a PictureBox.
Part 1: what a Windows Forms application is
A Windows Forms application is a program that runs on the desktop computer. It shows one or more windows, called forms, and a form normally holds a collection of controls such as labels, text boxes, list boxes and buttons.
A simple login screen shows the three most common roles a control plays:

- Labels describe the controls next to them. Here they tell the user that one text box is for the user name and the other for the password.
- Text boxes hold what the user types: the user name and the password.
- The button has code attached to it. Clicking Login runs that code, for example to check the user name and password.
Everything on the form is a C# object
The designer lets you build a form without writing code, but nothing on the form is magic. Behind the design surface is ordinary C#:
| What you do in the designer | What it is in C# |
|---|---|
| Your form, Form1 | A class that inherits from the Form class in System.Windows.Forms. Form already knows how to draw the title bar, the minimize, maximize and close buttons, and how to resize. |
| Drag a control onto the form | A new private field of that control's class (for example Label), and a new object stored in it |
| Change a value in the Properties window | Set a property of that object, for example its Text |
| Double-click a control | A new method that runs when one of that object's events happens |
Keep this table in mind: every step in this section adds or changes a few lines of C#, and you will see those lines.
Part 2: Hello World
Step 1: create the project
- Launch Visual Studio and choose the menu option New, Project.
- Choose the project type Windows Forms Application: click the Windows option on the left-hand side, then Windows Forms Application. (Recent versions of Visual Studio call the template Windows Forms App; the Desktop filter shows it.)
- Give the project a name, here DemoApplication, and a location to store it.
- Click OK (or Create) to let Visual Studio create the project.
What Visual Studio opens
| Window | What it is for |
|---|---|
| Form Designer | The design surface where you build the form |
| Solution Explorer | The files of the project: Form1.cs and Program.cs |
| Toolbox | Every control you can add to a form: Label, TextBox, Button and many more |
| Properties window | The properties of the selected control (right-click the control and choose Properties) |
The two code files in the Solution Explorer are:
- Form1.cs, the form. It holds the code of the Windows Forms application, such as the event handlers you will write.
- Program.cs, the main program: the startup code for the application as a whole. Its
Mainmethod runs first and opens the first form:
namespace DemoApplication
{
internal static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
}Application.Run(new Form1()) creates a Form1 object and shows it. That first form is the main form: when the user closes it, the application ends.
If you expand Form1.cs in the Solution Explorer you will also find Form1.Designer.cs. Form1 is declared as a partial class, so its code can live in two files that the compiler joins into one class: Form1.cs for the code you write, and Form1.Designer.cs for the code the designer writes for you. You should almost never edit the designer file by hand.
Step 2: add a label
- From the Toolbox, choose the Label control and drag it onto the form.
- Right-click the label and choose Properties.
- In the Properties window, find Text and enter
Hello World. - Run the program with the Start button.
Each control has a set of properties that describe it, and every value you set in the Properties window becomes a line of C# in Form1.Designer.cs. This is the designer code of our Hello World program:
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(150, 110);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "Hello World";
//
// Form1
//
this.ClientSize = new System.Drawing.Size(400, 250);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Label label1;Read it with the table from Part 1 in mind: new System.Windows.Forms.Label() creates the label object, each this.label1.Something = ... line is one value from the Properties window, and this.Controls.Add(this.label1) puts the label on the form. The form's constructor in Form1.cs calls InitializeComponent(), so all of this runs when the form is created.
Visual Studio 2022 on .NET 8 writes the same code in a shorter form: label1 = new Label(); and label1.Location = new Point(150, 110);, with no this. in front and no namespaces, and it adds a label1.Size line that holds the measured size of the text. If your own Form1.Designer.cs looks like that, it means exactly the same thing.
Running the program shows the form with "Hello World" on it:

Part 3: building the User Details form
Now we build one form with more controls. It lets the user:
- Enter a name and an address.
- Choose the city they live in.
- Choose a gender.
- Choose the courses they want to learn: C# and ASP.Net.
This is the finished form, running on Windows:

GroupBox
A group box groups controls into a section. The name and the address are details of one person, so they belong together in their own section of the form.
- Drag a GroupBox from the Toolbox onto the form.
- In the Properties window, change its Text to
User Details.
Label
A label displays a text or a message to the user. It is normally used together with another control, to tell the user what to fill in: a label next to a text box says what the text box is for.
- Drag a Label onto the form two times, one for the name and one for the address.
- Set the Text of the first to
Nameand of the second toAddress.
TextBox and the Name property
A text box lets the user enter text.
- Drag two TextBox controls onto the form, one for the name and one for the address.
- In the Properties window, set the Name property of each one to a meaningful name:
txtNameandtxtAddress.
The Name is the name of the control's variable in your code. Choose names by a naming convention: a short prefix for the kind of control and a word for its job. It makes the controls much easier to use in code later. The names used in this section:
| Control | Prefix | Example |
|---|---|---|
| TextBox | txt | txtName, txtAddress |
| ListBox | lst | lstCity |
| RadioButton | rd | rdMale, rdFemale |
| CheckBox | chk | chkC, chkASP |
| Button | btn | btnSubmit |
ListBox
A list box shows a list of items on the form. Ours stores city names.
- Drag a ListBox onto the form.
- In the Properties window, change its Name to
lstCity. - Click the Items property (it shows "(Collection)"). The String Collection Editor pops up.
- Enter the city names, one per line:
Mumbai,BangaloreandHyderabad. - Click OK.
The editor turns your lines into one statement in the designer file:
this.lstCity.Items.AddRange(new object[] {
"Mumbai",
"Bangalore",
"Hyderabad"});RadioButton
A radio button shows a list of options out of which the user can choose one. We add two, for male and female.
- Drag two RadioButton controls onto the form.
- Set their Text to
MaleandFemale. - Set their Name to
rdMaleandrdFemale.
CheckBox
A check box lets the user choose several options at once. We add two, so the user can pick C#, ASP.Net, or both.
- Drag two CheckBox controls onto the form.
- Set their Text to
C#andASP.Net. - Set their Name to
chkCandchkASP.
The difference matters when you design a form: pick a RadioButton when the answers exclude each other (a gender), and a CheckBox when the user may choose more than one (courses).
Button
A button lets the user start the processing of the form. Ours submits everything the user entered.
- Drag a Button onto the form.
- Set its Text to
Submitand its Name tobtnSubmit.
You now have your first basic Windows form in place.
Interactive: the form designer
The widget below is a small form designer. Drag controls from the Toolbox onto the form, change Name and Text in the Properties panel, and watch the two code files change: every action you take in the designer is a few lines of C#. Double-click a control to add its event handler, then switch to Run to see which events fire. Play builds the login form from Part 1 one step at a time.
Part 4: events
An event is something that happens when an action is performed. The most common one is the click of a button. An event handler is a method that runs when its event happens: in Windows Forms you add code that performs certain actions, for example when a button is pressed.
Double-click creates a handler
Double-click the ListBox in the Form Designer. Visual Studio opens Form1.cs and adds an empty method for you:
private void lstCity_SelectedIndexChanged(object sender, EventArgs e)
{
}It also adds one line to Form1.Designer.cs, which connects the method to the ListBox's event:
this.lstCity.SelectedIndexChanged += new System.EventHandler(this.lstCity_SelectedIndexChanged);You do not need to worry about the method name or its parameters yet. What matters is that this method now runs whenever any item in the list box is selected. Double-clicking creates the handler for the control's default event, the event you most often need for that control. To see every other event of a control, click the lightning-bolt button in the Properties window.
We read the default events from Windows Forms itself with a short program run on Windows:
| Control | Default event |
|---|---|
| Button, Label | Click |
| TextBox | TextChanged |
| ListBox | SelectedIndexChanged |
| CheckBox, RadioButton | CheckedChanged |
| The form itself | Load |
The same run gave the size of a newly created control: a TextBox is 100 by 23 pixels, a Button 75 by 23, and a ListBox 120 by 96. One more measured detail: when the user picks Female while Male is selected, both radio buttons raise CheckedChanged, first rdMale (now unchecked), then rdFemale.
Worked example: show the selected city
Now add the code that shows the selected city in a message box:
private void lstCity_SelectedIndexChanged(object sender, EventArgs e)
{
string text = lstCity.GetItemText(lstCity.SelectedItem);
MessageBox.Show(text);
}- The method is the event handler Visual Studio created when you double-clicked the list box.
lstCity.SelectedItemis the item the user selected. Remember thatlstCityis the name of our ListBox.GetItemTextgets the actual text of that item, and we store it in the variabletext.MessageBox.Show(text)displays the value to the user.
Predict what appears when the user clicks Bangalore, then compare with the run:

Selecting an item raised SelectedIndexChanged, the handler read the selected item and showed it.
Worked example: the Submit button
The button follows the same idea. Double-click the button in the Form Designer to get its Click handler, then add:
private void btnSubmit_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string address = txtAddress.Text;
MessageBox.Show(name + address);
}- The method is the handler Visual Studio created when you double-clicked the button.
- The values the user typed are in the Text property of each text box. We copy them into two variables,
nameandaddress. MessageBox.Showdisplays the name and the address.
Predict: the user types Student as the name and Mansoura as the address, then clicks Submit. What exactly does the message box say?

The message is StudentMansoura, with no space: the + operator joins two strings exactly as they are. To separate them, join a space in between: MessageBox.Show(name + " " + address);. Practice 2 tests this.
The Name in the code must match the Name property
The Name property is the variable name. Suppose you named the name box txtUser in the Properties window, but the handler still reads txtName.Text. Predict what happens when you build.
The program does not build at all. This is the compiler's message from that build (file path shortened):
Form1.cs(21,27): error CS0103: The name 'txtName' does not exist in the current contextLine 21, column 27 of Form1.cs is txtName in string name = txtName.Text;. There is no field called txtName, because the designer created a field called txtUser. The fix is to use the same name in both places: rename the control back to txtName in the Properties window, or change the code to txtUser.Text.
Part 5: TreeView and PictureBox
TreeView
A tree control lists items in a tree-like fashion. The best known example is the folder structure in Windows Explorer.
- Drag a TreeView from the Toolbox onto the form.
- In the Properties window, click the Nodes property. The TreeNode Editor opens.
- Click Add Root, change the node's Text to
Root, and click OK. This adds the root node. - Select Root and click Add Child, then change the child's Text. Repeat to get three children:
Label,ButtonandCheckbox. - Click OK.
The designer writes the tree as TreeNode objects. The children are created first, then the root receives them as an array:
System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Label");
System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Button");
System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Checkbox");
System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("Root", new System.Windows.Forms.TreeNode[] {
treeNode1,
treeNode2,
treeNode3});and later adds the root to the tree with this.treeView1.Nodes.AddRange(...). When you run the program the tree shows Root with a plus sign; click it to expand the root node and see the three child nodes.
PictureBox
A picture box adds an image to the form.
- Drag a PictureBox from the Toolbox onto the form.
- Click its Image property. A new window pops up.
- Click Import and choose the image in the dialog box.
- Click OK.
The image is now displayed on the form. The Image property can also be set in code, like any other property. Our lab program loads its picture from a file next to the program, in the form's constructor:
pictureBox1.Image = Image.FromFile(Path.Combine(AppContext.BaseDirectory, "picture.png"));This is the finished form with the user's input filled in (Male and C# selected):

The complete program
Three files make the whole User Details program. Program.cs is the one shown in Part 2. Form1.cs holds the two event handlers:
namespace DemoApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// The designer's Import button attaches a picture to the Image property.
// Here the same property is set from the picture file next to the program.
pictureBox1.Image = Image.FromFile(Path.Combine(AppContext.BaseDirectory, "picture.png"));
}
private void lstCity_SelectedIndexChanged(object sender, EventArgs e)
{
string text = lstCity.GetItemText(lstCity.SelectedItem);
MessageBox.Show(text);
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string address = txtAddress.Text;
MessageBox.Show(name + address);
}
}
}Form1.Designer.cs holds everything we did in the designer. Read it section by section: one new line per control, one block of property lines per control, then the form itself.
namespace DemoApplication
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Label");
System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Button");
System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Checkbox");
System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("Root", new System.Windows.Forms.TreeNode[] {
treeNode1,
treeNode2,
treeNode3});
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtName = new System.Windows.Forms.TextBox();
this.txtAddress = new System.Windows.Forms.TextBox();
this.lstCity = new System.Windows.Forms.ListBox();
this.rdMale = new System.Windows.Forms.RadioButton();
this.rdFemale = new System.Windows.Forms.RadioButton();
this.chkC = new System.Windows.Forms.CheckBox();
this.chkASP = new System.Windows.Forms.CheckBox();
this.btnSubmit = new System.Windows.Forms.Button();
this.treeView1 = new System.Windows.Forms.TreeView();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.txtName);
this.groupBox1.Controls.Add(this.txtAddress);
this.groupBox1.Location = new System.Drawing.Point(20, 15);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(240, 100);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "User Details";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(15, 30);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "Name";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(15, 63);
this.label2.Name = "label2";
this.label2.TabIndex = 2;
this.label2.Text = "Address";
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(90, 27);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(130, 23);
this.txtName.TabIndex = 1;
//
// txtAddress
//
this.txtAddress.Location = new System.Drawing.Point(90, 60);
this.txtAddress.Name = "txtAddress";
this.txtAddress.Size = new System.Drawing.Size(130, 23);
this.txtAddress.TabIndex = 3;
//
// lstCity
//
this.lstCity.Items.AddRange(new object[] {
"Mumbai",
"Bangalore",
"Hyderabad"});
this.lstCity.Location = new System.Drawing.Point(20, 130);
this.lstCity.Name = "lstCity";
this.lstCity.Size = new System.Drawing.Size(120, 94);
this.lstCity.TabIndex = 1;
this.lstCity.SelectedIndexChanged += new System.EventHandler(this.lstCity_SelectedIndexChanged);
//
// rdMale
//
this.rdMale.AutoSize = true;
this.rdMale.Location = new System.Drawing.Point(290, 25);
this.rdMale.Name = "rdMale";
this.rdMale.TabIndex = 2;
this.rdMale.Text = "Male";
//
// rdFemale
//
this.rdFemale.AutoSize = true;
this.rdFemale.Location = new System.Drawing.Point(290, 55);
this.rdFemale.Name = "rdFemale";
this.rdFemale.TabIndex = 3;
this.rdFemale.Text = "Female";
//
// chkC
//
this.chkC.AutoSize = true;
this.chkC.Location = new System.Drawing.Point(290, 100);
this.chkC.Name = "chkC";
this.chkC.TabIndex = 4;
this.chkC.Text = "C#";
//
// chkASP
//
this.chkASP.AutoSize = true;
this.chkASP.Location = new System.Drawing.Point(290, 130);
this.chkASP.Name = "chkASP";
this.chkASP.TabIndex = 5;
this.chkASP.Text = "ASP.Net";
//
// btnSubmit
//
this.btnSubmit.Location = new System.Drawing.Point(290, 175);
this.btnSubmit.Name = "btnSubmit";
this.btnSubmit.Size = new System.Drawing.Size(90, 28);
this.btnSubmit.TabIndex = 6;
this.btnSubmit.Text = "Submit";
this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(430, 20);
this.treeView1.Name = "treeView1";
treeNode1.Name = "Node1";
treeNode1.Text = "Label";
treeNode2.Name = "Node2";
treeNode2.Text = "Button";
treeNode3.Name = "Node3";
treeNode3.Text = "Checkbox";
treeNode4.Name = "Node0";
treeNode4.Text = "Root";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
treeNode4});
this.treeView1.Size = new System.Drawing.Size(170, 110);
this.treeView1.TabIndex = 7;
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(430, 150);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(120, 60);
this.pictureBox1.TabIndex = 8;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(630, 245);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.lstCity);
this.Controls.Add(this.rdMale);
this.Controls.Add(this.rdFemale);
this.Controls.Add(this.chkC);
this.Controls.Add(this.chkASP);
this.Controls.Add(this.btnSubmit);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "User Details";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtAddress;
private System.Windows.Forms.ListBox lstCity;
private System.Windows.Forms.RadioButton rdMale;
private System.Windows.Forms.RadioButton rdFemale;
private System.Windows.Forms.CheckBox chkC;
private System.Windows.Forms.CheckBox chkASP;
private System.Windows.Forms.Button btnSubmit;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.PictureBox pictureBox1;
}
}Note the four controls inside the group box: they are added with this.groupBox1.Controls.Add(...), not to the form, which is how the designer records that they sit inside the group.
Quick check
- You need the user to pick exactly one faculty level out of four. RadioButton or CheckBox?
- Which property holds what the user typed in a TextBox?
- Which property is the name you use for a control in your code?
- What do you do in the designer to get a Click handler for a button?
- In the User Details form, which method runs when the user selects Hyderabad?
Practice
About 30 minutes. Build each form in Visual Studio (or plan it in the form designer widget first), then run it. The answers are at the end.
Practice 1: the login form (about 10 minutes)
Build the login form from Part 1:
| Control | Text | Name |
|---|---|---|
| Label | User Name | label1 |
| Label | Password | label2 |
| TextBox | (empty) | txtUser |
| TextBox | (empty) | txtPassword |
| Button | Login | btnLogin |
Set the form's Text to Login. When the user clicks Login, show a message box that says Welcome followed by the user name. What does the message box show when the user name is admin?
Practice 2: predict the message (about 4 minutes)
A form has two text boxes, txtName and txtAddress, and three buttons. Each handler first reads string name = txtName.Text; and string address = txtAddress.Text;, and then:
// button A
MessageBox.Show(name + " " + address);
// button B
MessageBox.Show("Name: " + name);
// button C
MessageBox.Show(address + name);The user types Student as the name and Cairo as the address. Write the exact message each button shows.
Practice 3: a list of courses (about 8 minutes)
Build a form titled Courses with a Label Choose a course, a ListBox named lstCourse with the items C#, ASP.Net and Operating Systems, and a Button named btnShow with the Text Show. When the user clicks Show, display You chose followed by the selected course. Use GetItemText and SelectedItem as in the lstCity handler.
- What does the message box show after the user selects Operating Systems and clicks Show?
- What does it show if the user clicks Show before selecting anything?
Practice 4: find the bug (about 4 minutes)
A student built the Practice 3 form, with the ListBox named lstCourse, and wrote this handler:
private void btnShow_Click(object sender, EventArgs e)
{
string course = lstCourses.GetItemText(lstCourses.SelectedItem);
MessageBox.Show("You chose " + course);
}Will it run? If not, what does the compiler report, and how do you fix it?
Practice 5: choose the controls (about 4 minutes)
Plan a student registration form. For each piece of information, choose the control and a Name that follows the naming convention: the student's full name; the faculty level (exactly one of four); the hobbies (any number of four); the city (one of a list of cities); a button that saves the form.
Answers
Quick check
- RadioButton: the user chooses exactly one option out of several. A CheckBox allows several choices.
- Text, for example
txtName.Text. - Name, for example
lstCity. - Double-click the button in the Form Designer. Visual Studio adds the handler method and wires it to the button's Click event.
lstCity_SelectedIndexChanged, the handler of the ListBox's SelectedIndexChanged event.
Practice 1
Form1.cs:
namespace DemoApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
string user = txtUser.Text;
MessageBox.Show("Welcome " + user);
}
}
}The designer part for the button (the labels and text boxes follow the same pattern as the User Details form):
this.btnLogin.Location = new System.Drawing.Point(110, 105);
this.btnLogin.Name = "btnLogin";
this.btnLogin.Size = new System.Drawing.Size(90, 28);
this.btnLogin.TabIndex = 4;
this.btnLogin.Text = "Login";
this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);With the user name admin the message box shows Welcome admin:

The space comes from the string "Welcome ", which ends with a space.
Practice 2
| Button | Code | Message |
|---|---|---|
| A | name + " " + address | Student Cairo |
| B | "Name: " + name | Name: Student |
| C | address + name | CairoStudent |
The three messages from the run:


+ joins strings exactly as they are and in the order written: nothing is added between them, so C gives CairoStudent.
Practice 3
private void btnShow_Click(object sender, EventArgs e)
{
string course = lstCourse.GetItemText(lstCourse.SelectedItem);
MessageBox.Show("You chose " + course);
}You chose Operating Systems:

- With nothing selected,
SelectedItemis empty andGetItemTextreturns an empty string, so the message box shows onlyYou chose:

Practice 4
It does not build. The compiler reports (file path shortened):
Form1.cs(12,29): error CS0103: The name 'lstCourses' does not exist in the current context
Form1.cs(12,52): error CS0103: The name 'lstCourses' does not exist in the current contextThere are two errors because the wrong name is used twice on line 12, at columns 29 and 52. The ListBox's Name is lstCourse, so the designer created a field called lstCourse; there is nothing called lstCourses. Use the same name as the Name property: lstCourse.GetItemText(lstCourse.SelectedItem). This is the same mistake as the txtUser and txtName example in Part 4.
Practice 5
| Information | Control | Name |
|---|---|---|
| Full name | TextBox | txtName |
| Faculty level, exactly one | RadioButton (four of them) | for example rdLevel1 to rdLevel4 |
| Hobbies, any number | CheckBox (four of them) | for example chkReading, chkSport |
| City from a list | ListBox | lstCity |
| Save | Button | btnSave |
A Label next to the text box and a GroupBox around each set of related controls help the user read the form. Other sensible names are fine, as long as each one has the right prefix and says what the control is for.