Lecture 1 2025
Lecture 1 2025
LECTURE 1
2021
• Introduction to Programming
• How to use Visual Studio
• Your first C# programs
1
After this lecture you should …
1. Be able to use the Visual Studio Interactive Development
Environment to create a simple Windows Forms Based
program in C#
2. Know how to create, run and save a C# project
3. Understand what a Control, a Property, an Event and an
Event handler are
4. Know how to use the following C# controls e.g button,
form etc
2
WHAT IS PROGRAMMING?
• Programming is the skill to write code that a computer can follow, to execute
a plan you made, to solve a problem.
• Computer programming is a way of giving computers instructions about what
they should do next.
• These instructions are known as code, and computer programmers write
code to solve problems or perform a task.
3
WHAT IS PROGRAMMING?
For 1) you need computational thinking which includes algorithmic thinking
Computational Thinking
• The computational thinking process includes four key concepts:
– Decomposition: Break the problem down into smaller, more manageable parts.
– Pattern Recognition: Analyze data and identify similarities and connections among
its different parts.
– Abstraction: Identify the most relevant information needed to solve the problem
and eliminate the extraneous/irrelevant details.
– Algorithmic Thinking: Develop a step-by-step process to solve the problem so that
the work is replicable by humans or computers.
For 2) you need software and you need to learn the syntax/programming language.
Software
• Visual Studio (Integrated Development Environment)
Programming Language
• C#
4
Why C#?
• Mobile applications
• Web services
• Console-based applications
9
GETTING TO KNOW VISUAL STUDIO
10
GETTING TO KNOW VISUAL STUDIO
Main menu
Toolbox with
Controls
Default Windows
Form Solution Explorer
Properties window
11
Error list
GETTING TO KNOW VISUAL STUDIO
12
YOUR FIRST C# PROGRAM
• Our first program will consist of one Form with a single Button
control.
• The text on the button will be “Blue”.
• When we run the program and click on the “Blue” button, the Form’s
background colour will turn blue.
• It will appear as follows:
•In the next slides we write this program step-by-step and explain the
programming concepts as we go along.
13
YOUR FIRST C# PROGRAM
Step 1 – Place a Button control on the form.
14
YOUR FIRST C# PROGRAM
Step 2 - Changing the button’s Text to “Blue”
15
YOUR FIRST C# PROGRAM
Step 3 - Changing the button’s Name to “btnBlue”
16
YOUR FIRST C# PROGRAM
Step 4 – Adding program code to the button
18
YOUR FIRST C# PROGRAM
Step 4 – Adding program code to the button
Type the following program statement into the event handler exactly as
shown.
During the course of the year you will learn more about
what each of the parts in the header line means. For now
just remember not to change or delete any part of the
header statement.
19
YOUR FIRST C# PROGRAM
Step 4 – Adding program code to the button
1. Using program code (here the Form’s BackColor property is changed to Blue).
20
YOUR FIRST C# PROGRAM
Step 5 – Saving the project
21
YOUR FIRST C# PROGRAM
Step 6 – Executing your program
22
YOUR FIRST C# PROGRAM
Dealing with errors
There are two kinds or errors in programming – syntax errors and logical (or run
time) errors:
•Syntax errors are mistakes in the code syntax or typing errors such as leaving
out the semicolon at the end of a program statement or not spelling the name
of a control correctly. Most of these errors are picked up by the Visual Studio
editor and will be indicated with a red squiggly line in your code.
•In the example below there are two syntax errors. The text property must be
written with a capital T, and Blue has to be preceded by Color – i.e. Color.Blue;
•If you hover with your mouse pointer over the red line a message box will pop
up telling you what the error is.
•You have to fix all of these errors before you can run the program.
23
YOUR FIRST C# PROGRAM
Dealing with errors
• Run-time errors can be more difficult to identify. They occur when there are
no syntax errors but the program is not executing correctly when you run it
(i.e. it gives the wrong output).
• A very simple example would be if you click on the “Blue” button and the
Form’s colour changes to red instead of blue.
• In this case you have to inspect the code to find the error – the Visual Studio
editor will not able to pick it up. The programmer will have typed Color.Red
instead of Color.Blue but to the compiler this would seem acceptable.
• As we write more complex problems you will learn to “debug” your programs
because it is human to make errors while programming.
24
YOUR FIRST C# PROGRAM
Expanding the program
See if you can do the following on your own without looking at the solution
on the next slides:
•Add a second button to the Form
•Name it btnYellow
•Make it’s Text property “Yellow”
•Create a click event handler with code to change the background of the form
to yellow
•Also add a statements to each of the two click event handlers that will
change the Form’s Text property to the current background colour.
25
Lecture 1 Homework
26
Additional videos: WHAT IS AN ALGORITHM?
https://www.youtube.com/watch?v=ZnBF2GeAKbo
https://www.youtube.com/watch?v=oRkNaF0QvnI
https://www.youtube.com/watch?v=CvSOaYi89B4
27