0% found this document useful (0 votes)
10 views

01. Exploring the Visual C# Toolbox(Part 1)

Uploaded by

PoEnya RaTna
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

01. Exploring the Visual C# Toolbox(Part 1)

Uploaded by

PoEnya RaTna
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 74

Business

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

\” Double quotation mark

\’ Single quotation mark

\\ Backslash

\0 Null

\a Alert (cause a beep)

\b Backspace (mundurkan kursor)

\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

bool 8 True or False

byte 8 0 - 255

sbyte 8 -128 - 127

char 16 1 character unicode

short 16 -32.768 - 32.767

ushort 16 0 - 65.535

int 32 -2.147.483.648 - 2.147.483.647

uint 32 0 - 4.294.967.295
38 38
float 32 -3,4 x 10 - +3,4 x 10

long 64 -9.223.372.036.854.775.808 sampai 9.223.372.036.854.775.807

ulong 64 0 sampai 18.446.744.073.709.551.615


28 28
decimal 128 -7,9x 10 sampai +7,9 x 10
-324 308
double 64 ±5,0 x 10 sampai ±1,7 x 10

string 0 sampai ~2.000.000.000 karakter unicode


Literal Values
Literals are notations that represent values in the source code of the program. The following
table shows the literal values that exist in C#.
Data Type Suffix Example

bool None true or false

int None 100

uint U or u 100U or 100u

long L or l 100L or 100l

ulong UL or ul 100UL or 100ul

float F or f 1.5F or 1.5f

double D or d 1.5D or 1.5d

decimal M or m 1.5M or 1.5m

char None ‘a’

string None “a…a”


Literal Values (Cont.)
Especially for string literals, it can be seen in the table below.
Escape Sequence Information

\’ Single quotation mark

\” Double quotation mark

\\ Backslash

\0 Null

\a Alert (causes a beep)

\b Backspace (Mundurkan kursor)

\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:

datatype variableName = initialization;


Variable (Cont.)
Variable names are used to identify variables in a program. When naming
variables, it's best to choose meaningful names that describe what they are
used for. Naming variables must follow the following naming conditions:
Must start with alphabet character, underscore "_" or symbol at "@"
After the first character may use the alphabet, number or underscore "_"
Variable names cannot contain spaces
1. We recommend that when naming variables use camelCase, for
example: square, costSalary
Variable (Cont.)
Examples of giving valid and invalid variable names:
aB123_45 (valid)
_567 (valid)
jlhRevenue (valid)
123ABC (invalid)
Xyz$wv (invalid)
Variable (Cont.)
Initializing means that a variable must be assigned a value before it can be
used.
Example:

string deskripsiProduk = "Baju";


Variable (Cont.)
Multiple variables that have the same data type can also be declared at
once by using a comma separator.
Example:

string namaDepan, namaTengah, namaBelakang;

string namaDepan = "Johan", namaTengah = "Putra", namaBelakang


= "Sinaga";

string namaDepan = "Johan", namaTengah = "Putra", namaBelakang


= "Sinaga";
Constant
A constant is almost the same as a variable that has a data type. If a
constant has already been declared, then its value cannot be changed at
the time of program execution.

The constant declaration syntax is as follows:

const datatype CONSTANTNAME = value;


Constant (Cont.)
The const keyword tells the compiler that the variable is read-only.
If any statement tries to change the value of the constant, an error will
occur. When declaring a constant, initialization of the initial value is
required.
Constant names don't have to be capitalized, but many programmers like
to capitalize constants to make them easy to distinguish from regular
variables.
Example:

const double SUKU_BUNGA = 0.129;


Value
Value is the quantity of the data type that has been defined (base type and
formation type).

A value can be a content stored by a variable name or constant name, a


value from the result of a calculation, or a value sent by a function. The
value entered must match the data type of the variable or constant.

◦ 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;

Filled in via input device


i=
Convert.ToInt32(Console.ReadLine())
;

i = int.Parse(Console.ReadLine());
Displayed using the command
Console.Write(i);

Console.WriteLine(i);
Implicit Conversion

• In C# there is an implicit conversion term which means that the


conversion can be done directly without having to use
functions or help commands because it has the same basic type
format.
• Can only be done for numeric data types.
Implicit Conversion(Cont.)
Data Type Data Type that can be converted

byte short, ushort, int, uint, long, ulong, float, double,


decimal
sbyte short, int, long, float, double, decimal

short int, long, float, double, decimal

ushort int, uint, long, ulong, float, double, decimal

int long, float, double, decimal

uint long, ulong, float, double, decimal

long float, double, decimal

ulong float, double, decimal

float double

char ushort, int, uint, long, ulong, float, double, decimal


Implicit Conversion (cont.)
• Example

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();

• Then a message will appear on the Error List:


Cannot implicitly convert type 'char' to 'short'. An
Explicit Conversion (Cont.)
• Explicit conversions are also known as Casting

• 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

Convert.ToByte(nilai) value converted to byte

Convert.ToChar(nilai) value converted to char

Convert.ToDecimal(nilai) value converted to decimal

Convert.ToDouble(nilai) value converted to double

Convert.ToInt16(nilai) value converted to short

Convert.ToInt32(nilai) value converted to int


Class Convert (Cont.)

Perintah Keterangan
Convert.ToInt64(nilai) nilai dikonversi menjadi tipe long

Convert.ToSByte(nilai) nilai dikonversi menjadi tipe sbyte

Convert.ToSingle(nilai) nilai dikonversi menjadi tipe float

Convert.ToString(nilai) nilai dikonversi menjadi tipe string

Convert.ToUInt16(nilai) nilai dikonversi menjadi tipe ushort

Convert.ToUInt32(nilai) nilai dikonversi menjadi tipe uint

Convert.ToUInt64(nilai) nilai dikonversi menjadi tipe ulong


Class Convert (Cont.)
• Example:
int bil1, bil2, a, b,
c; float d;
Console.Write("Masukkan Bilangan 1: ");
bil1 =
Convert.ToInt32(Console.ReadLine());
Console.Write("Masukkan Bilangan 2: ");
abil2
= bil1
= + bil2;
b = bil1 - bil2;
Convert.ToInt32(Console.ReadLine());
c = bil1 * bil2;
d = (float)bil1 /
bil2;

...
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

You might also like