01. Exploring the Visual C# Toolbox(Part 1)
01. Exploring the Visual C# Toolbox(Part 1)
Application
Programming
EXPLORING THE VISUAL C# TOOLBOX
Overview
You have learned to write simple C# programs that accept input from a user at the
console and produce output at the command line. The environment the user sees
is a program’s interface; unfortunately, the interfaces in the applications you have
written so far look dull.
Most modern applications use visually pleasing graphic objects to interact with
users. These graphical user interface (GUI) objects include the labels, buttons, and
text boxes you manipulate or control with a mouse, touch screen, or keyboard
when interacting with Windows-type programs.
Objectives
Upon completion of this Unit, students are expected to be able to:
Understand how to create visual C# applications
Understand how to create simple programs to run applications
•Understand the usage of each data type, variable, and constant
Contents
Object, Property, Method, and Event
Microsoft Visual Studio 2019 IDE
Getting to Know the Controls
Form
Buttons, Labels, and TextBoxes
GroupBox, Panel, CheckBox, and RadioButton
ListBox and ComboBox
a. MessageBox and DialogBox
Object, Property,
Method, and Event
o You learned that when you write a console
application, you can use a simple text editor
such as Notepad or you can use the Visual
Studio integrated development environment
(IDE). Technically, you have the same options
when you write a GUI program.
oThese graphical user interface (GUI) objects
include the labels, buttons, and text boxes
you manipulate or control with a mouse, touch
screen, or keyboard when interacting with
Windows-type programs.
Object, Property,
Method, and Event
oAll controls of object have a few
properties that are used to
manipulate the behavior of the
control.
oMethod is the term used to refer to functions
exposed by objects. These can be called in the
same way as any other function and can use
return values and parameters in the same way.
oEvents are like exceptions in that they are
raised (thrown) by objects, and you can supply
code that acts on them.
Microsoft Visual Studio 2019 IDE
oNow that you know enough about how WPF is constructed to
start getting your hands dirty, it’s time to look at the editor.
oStart by creating a new WPF project by selecting File ➪ New ➪
Project.
oFrom the Create a New Project dialog box, narrow the choices by
C#, Windows, and Desktop to the Windows Classic Desktop node
under Visual C# and select the project template WPF Application.
Microsoft Visual Studio 2019 IDE
Getting to Know the Controls
o Controls combine prepackaged code and a GUI that can be reused to create
more complex applications. They can define how they draw themselves by
default and a set of standard behaviors.
o The Toolbox contains the controls used to customize Forms.
o With visual app development, you can “drag and drop” controls onto the
Form and the IDE will write the code that creates the controls for you. This is
faster and simpler than writing this code yourself.
o Just as you do not need to know how to build an engine to drive a car, you
do not need to know how to build controls to use them.
o Reusing preexisting controls saves time and money when you develop apps.
Create a Simple
App in Visual
Studio
INTRODUCTION TO C# APPLICATION PROGRAMMING
Lesson Overview
Visual Studio is a software application used to develop an application, one of which is a console
application. The chapter discusses step-by-step developing a console application starting from
creating a project, renaming files, writing code and using IntelliSense, running applications,
studying errors that appear if any.
Mengubah Nama File Aplikasi
For new Apps, it's better to rename the app file (Program.cs) to a name that describes the app.
To rename a file, right-click on the Program.cs in Solution Explorer and select Rename to create
a new file name.
Windows automatically selects the name of the file (Program) and reads the name of that file.
For example, when renaming a file originally named Program.cs to Welcome.cs then press Enter
to rename the file. Windows will read the file name to Welcome and make sure to save the
filename extension.cs.
Rename a File(Cont.)
How to rename a file
Writing Code Using IntelliSense
Visual Studio has an IntelliSense feature where IntelliSense is an aid to code completion that
includes a number of other features such as parameter info, quick info, complete item list.
This feature will help you learn the program code that is being used, can keep a close eye on the
parameters you use and add properties and methods just by pressing a few keys and a list of
items appears starting from the letters you typed.
Writing Code Using IntellSense
(Cont.)
IntelliSense window
when typing the word "Cons"
Writing Code Using IntelliSense
(Cont.)
When you start typing, IntelliSense lists various items that start with a few letters that you type.
IntelliSense also displays brief information containing a description of the closest first item.
You can type the full item name such as Console, then double-click on the item name in the
IntelliSense window or can press the Tab key to complete the item name and once the full item
name is displayed, the IntelliSense window will close.
While the IntelliSense window is being displayed, you can press the Ctrl key to give a
transparent view of the IntelliSense window so that the program code can be seen behind the
IntelliSense window.
Writing Code Using IntellSense
(Cont.)
IntelliSense transparent window
Writing Code Using IntellSense
(Cont.)
When typing a period (.) after the Console, the IntelliSense window will reappear and will
display the members of the Console class used after the period is typed.
The list of options will adjust to what you've typed. The figure below shows the IntelliSense
window displaying a selection of items containing the word "Write".
You can also type "WL" to search for all items containing uppercase letters "W" and "L" such as
WriteLine and WindowLeft.
Writing Code Using IntellSense
(Cont.)
Writing Code Using IntellSense
(Cont.)
After you type the parenthetical character ("(") after Console.WriteLine, the Parameter Info
window is displayed. The Parameter Info window contains information about parameter
methods.
A method can have multiple versions. That is, a class can define multiple methods that have the
same name if these methods have different numbers or types of parameters. This method is also
called Overloaded Methods.
This method usually performs a similar task as the Parameter Info window shows how many
versions of the selected method are available and provides up and down arrows to see the
different versions. For example, there are many versions of the WriteLine method that allow you
to display different types of data.
Writing Code Using IntellSense
(Cont.)
The Parameter Info window is one of many features provided by Visual C# to facilitate
application development.
Writing Code Using IntellSense
(Cont.)
From the image above shows the Console.WriteLine program code will display the word (string),
so since you already know the version of WriteLine you want to use, you can close the
Parameter Info window by pressing the Esc key or the Parameter Info window will close
automatically by continuing the program code.
Compile and Run the Application
Depending on the type of project, the compiler can compile the code into a file with a .exe
extension (executable), an extension (a dynamically linked library) or one of several other
extensions. You find these files in the project subfolder. The file is a set of files and is the
packaging unit for compiled C# code. This set of files contains the Microsoft Intermediate
Language code for the application.
To compile the application, you select from the Build → Build Solution. If the application does
not have errors at compile time, then the application can compile and make the file executable.
Writing Code Using IntellSense
(Cont.)
How to build an app
Writing Code Using IntellSense
(Cont.)
Build Solution information
Writing Code Using IntellSense
(Cont.)
To run it, press the Ctrl + F5 keys which will call the Main method. If you have never tried to run
the application before doing the build, Visual C# will build the application first and then run it if
there are no errors in the application or program code.
Errors, Error Messages and
Error List Window
When program code is typed, Visual C# applications respond to the typed code through coloring
in syntax or by making mistakes.
When an error occurs, the Visual C# application will underline the location of the error with a
red squiggly line and provide a description in the Error List window. If the Error List window is
not visible, then the window can be displayed through the View Error List menu to display it.
Errors, Error Messages and Error
List Window (Cont.)
An example of an error in the image below is the absence of a semicolon (;) at the end of the
program code. The error message indicates that there is no semicolon. You can double-click the
error message in the Error List window to be directed to the error location in the program code.
Errors, Error Messages and
Error List Window
How to display Error List from View Menu
Modifying Your
Simple C#
Application
INTRODUCTION TO C# APPLICATION PROGRAMMING
Console Application (Cont.)
Basic Commandments in
Namespace System.Console
Lesson Overview
In Visual Studio, we can modify a C# application that has been developed with several methods
as needed.
In this chapter will discuss how to modify a C# application using Visual Studio such as how to
display one line of text using more than one statement, display more than one line of text using
one statement.
Display One Line of Text with
Multiple Statements
The program code is designed according to the needs of the application. Generally, there are two
ways to display a line of text: by using multiple statements to display a single line of text or using
a single statement to display more than one line of text.
The image below uses two statements to generate a single line of text.
Display a Single Line of Text with
Multiple Statements (Cont.)
The result of one-line program execution by using two statements
The first statement uses the Console Write Method to write and display characters (strings),
unlike the WriteLine method, the Write method does not position the cursor at the beginning of
the next line. The next character displayed will appear after the last character written with the
Write method.
Display Multiple Lines of Text
with One Statement
A single statement can display multiple lines by using newline characters that indicate the
Console Write and WriteLine methods when they need to position the cursor at the beginning of
the next line such as space characters, tab characters, newline characters.
Display Multiple Lines of Text
with One Statement (Cont.)
String Literals
Escape Sequence Keterangan
\n New line
\t Horizontal tab
\\ Backslash
\0 Null
\r Carriage return
Display Multiple Lines of Text
with One Statement (Cont.)
The image below shows four lines of text using newline characters where newline characters
indicate when to start a new line.
Display Multiple Lines of Text
with One Statement (Cont.)
The result of the run uses a single statement to display more than one line.
Comments
Generally, comments are used to insert notes on the program code that serves to explain how
the program code works to make it easier to understand. Comments are not intended for the
compiler, meaning comments on the program are not executed or executed.
Comments are intended for humans who read program code and attempt to understand the
code. Comments will be very helpful when making program modifications in the future,
especially on large and complex programs.
In C#, there are 3 types of comments, namely:
1. Line Comments
2. Block Comments
3. Documentation Comments
Comments (Cont.)
Line comments are used for 1-line comments in the program. The use of line comments begins
with two forward slash signs "//".
Everything written after the "//" sign to the end of the line will be ignored by the compiler. Line
comments don't always have to start at the beginning of a line, so comments can appear behind
the program line.
Comments (Cont.)
Line Comment
Comments (Cont.)
Block comments can be used for comments consisting of several lines.
Block comments begin with a forward slash followed by an asterisk "/*" and end with an
asterisk followed by a forward slash "*/".
Everything written between the "/*" and "*/" signs will be ignored by the compiler.
Comments (Cont.)
Block Comment
Comments (Cont.)
Documentation comments are used by professional programmers to include thorough
documentation in the source code of the program.
Visual Studio can quote information from documentation comments and generate external
documentation files.
Single line documentation comments begin with three forward slash signs "///". Meanwhile,
block documentation comments begin with a forward slash followed by two asterisk signs "/**"
and end with an asterisk followed by a forward slash "*/".
Data Types,
Variables,
Constants
Introduction to C# Application Programming
Lesson Overview
Data types group the data based on their content and properties.
A variable is a temporary storage area in memory that can change its value during program
execution.
Constants are similar to variables, but their values cannot be changed at the time of program
execution.
Data Type
Data type will group data based on its content and properties.
In addition, the data type also determines the format of the data to be stored in variables and
constants and how much memory is allocated to store the data.
The table below shows the data types that can be used in C#.
Data Type (Cont.)
Data Type Size (bit) Range of Values
byte 8 0 - 255
ushort 16 0 - 65.535
uint 32 0 - 4.294.967.295
38 38
float 32 -3,4 x 10 - +3,4 x 10
\\ Backslash
\0 Null
\n New line
\r Carriage return
\t Horizontal tab
Variable
A variable is a temporary storage in memory represented by a name,
whose value can change during program execution.
In C#, variables must be declared in the program before being used to
store data.
The variable declaration states 2 things about the variable, namely:
Data type
Variable name
Variable (Cont.)
The variable declaration syntax is as follows:
◦ Contoh:
◦ int a = "abc"; (ERROR!)
◦ int a = 123; (TRUE)
◦ string b = 123; (ERROR!)
◦ string b = "123“; (TRUE)
Value (Cont.)
Value can be manipulated by:
Filled in person
Int i = 5;
i = int.Parse(Console.ReadLine());
Displayed using the command
Console.Write(i);
Console.WriteLine(i);
Implicit Conversion
float double
int a = 1000;
double b = a; = {0}", b);
Console.WriteLine(
"b
Console.ReadKey();
char c =
'A'; int d
= c; = {0}", c);
Console.Writ
eLine("c
Console.WriteLine( = {0}", d); //Menghasilkan kode
"d ASCII
Explicit Conversion
• Explicit conversions are conversions that take place when the
conversion can't be done implicitly.
• Example :
char a =
'A'; short
b = a;
Console.Wri
teLine("a =
{0}", a);
Console.WriteLine("b = {0}",
b); Console.ReadKey();
• Sintaks:
• Variabel_Tujuan = (DataType) Variabel_Asal;
• Example:
char a =
'A'; short
b;
b =
(short)a;
Console.Wri
teLine("a =
{0}", a);
Class Convert
• It is a built-in class of C# that consists of a number of data
type conversion functions.
Command Information
Convert.ToBoolean(nilai) value converted to bool
Perintah Keterangan
Convert.ToInt64(nilai) nilai dikonversi menjadi tipe long
...
Class Convert (Cont.)
..
.
Console.WriteLine("{ + {1} = {2}", bil1, bil2, a);
0}
Console.WriteLine("{ - {1} = {2}", bil1, bil2, b);
0}
Console.WriteLine("{ x {1} = {2}", bil1, bil2, c);
0}
Console.WriteLine("{ / {1} = {2}", bil1, bil2, d);
0}
Console.ReadKey();
Example:
Calculating the Area of a Rectangle
static void Main(string[] args){
int panjang;
int lebar;
int luas;
Console.Writ
e("Masukkan
Panjang :
");
panjang =
Convert.ToIn
t32(Console.
ReadLine());
Console.Writ
e("Masukkan
Lebar : ");
lebar =
Convert.ToIn
t32(Console.
Exercise
Exercise 1
Create a simple program to display your own data inputted through the
keyboard!
Summary
Now students are able to:
Understand creating visual C# applications
Understand how to programmatically simple and run visual C# applications
Understand the use of data types, variables, and constants
Question &
Answers