You are on page 1of 43

Programming and Interfacing


Ng Quc Cng

Classes, subjects, and methods
Graphical user interface


In 2000, Microsoft announced the C# programming language.

C# has roots in the C, C++ and Java programming
It has similar capabilities to Java and is appropriate for the
most demanding app-development tasks, especially for
building todays large-scale enterprise apps, and web-based,
mobile and cloud-based apps.
C# is object oriented.

1. Introduction

C# has access to the powerful .NET Framework Class

Librarya vast collection of prebuilt classes that enable you
to develop apps quickly

1.1. Microsofts .NET

In 2000, Microsoft announced its .NET initiative a broad

vision for using the Internet and the web in the development,
engineering, distribution and use of software.
.NET permits you to create apps in any .NET-compatible
language (such as C#, Visual Basic, Visual C++ and many
The.NET Framework:
executes apps and contains the .NET Framework Class
Library, which provides many capabilities that youll use to
build substantial C# apps quickly and easily.
The Common Language Runtime (CLR), another key part
of the .NET Framework, executes .NET programs and
provides functionality to make them easier to develop and
1.2. Introduction to C# apps

Example 1: Displaying a line of text

1.2. Introduction to C# apps

Example 1: Displaying a line of text

Line 3: using Directive

is a using directive that tells the compiler where to look for a

class thats used in this app.
A great strength of Visual C# is its rich set of predefined
classes that you can reuse.
These classes are organized under namespaces.

1.2. Introduction to C# apps

Example 1: Displaying a line of text

Line 5: Class declaration

begins a class declaration for the class Welcome1.

Every app consists of at least one class declaration thats
defined by the programmer.

1.2. Introduction to C# apps

Example 1: Displaying a line of text

Line 8:

The parentheses after the identifier Main indicate that its an

app building block called a method.
Class declarations normally contain one or more methods.

1.2. Introduction to C# apps

Example 1: Displaying a line of text

Body of a Method Declaration
The left brace in line 9 begins the body of the method
declaration. A corresponding right brace must end the
methods body (line 11).
Line 10 in the body of the method is indented between the
Displaying a Line of Text

Class Console provides standard input/output capabilities

that enable apps to read and display text in the console
window from which the app executes.
The Console.WriteLine method displays a line of text in the
console window.
1.2. Introduction to C# apps

Example 1: Displaying a line of text

The entire line 10, including Console.WriteLine, the
parentheses, the argument "Welcome to C#
Programming! in the parentheses and the semicolon(;), is
called a statement.

1.3. Creating a simple app

Creating the console app

2. Classes, methods

Suppose you want to drive a car and make it go faster by

pressing down on its accelerator pedal. What must happen
before you can do this? Well, before you can drive a car,
someone has to design it. A car typically begins as
engineering drawings, similar to the blueprints used to design
a house. These engineering drawings include the design for
an accelerator pedal to make the car go faster. The pedal
hides the complex mechanisms that actually make the car
go faster, just as the brake pedal hides the mechanisms
that slow the car and the steering wheel hides the
mechanisms that turn the car. This enables people with little
or no knowledge of how engines work to drive a car easily.

2. Classes, methods

The method describes the mechanisms that actually
perform its tasks.
The method hides from its user the complex tasks that it

2. Classes, methods

In C#, we begin by creating an app unit called a class to
house (among other things) a method.
In a class, you provide one or more methods that are
designed to perform the classs tasks.
a class that represents a bank account might contain one
method to deposit money in an account, another to
withdraw money from an account and a third to inquire
what the current account balance is.

2. Classes, methods

Just as you cannot drive an engineering drawing of a car,
you cannot drive a class.
Just as someone has to build a car from its engineering
drawings before you can actually drive it, you must build
an object of a class before you can make an app perform
the tasks the class describes.

3. Graphical user interface

A graphical user interface (GUI) allows a user to interact

visually with a program.
GUIs are built from GUI controls (which are sometimes called
components or widgetsshort for window gadgets).

3. 1. Windows forms

Windows Forms is one library that can be used to create

A Form is a graphical element that appears on your
computers desktop.

3. 1. Windows forms

3. 2. Event Handling

Normally, a user interacts with an apps GUI to indicate the

tasks that the app should perform.
When the user interacts with a GUI component, the
interactionknown as an event drives the program to
perform a task.
Common events:
Clicking a button
Typing in a textbox
Selecting items from a menu
Closing a window
Moving the cursor

3. 2. Event Handling

3. 2. Event Handling

Locating event information

3.3. Control properties and layout

3.4. Labels, Textboxes and Button

Labels provide text information (as well as optional images)

and are defined with class Label.
A Label displays text that the user cannot directly modify.
A Labels text can be changed programmatically by modifying
the Labels Text property.

3.4. Labels, Textboxes and Button

A textbox (class TextBox) is an area in which either text can

be displayed by a program or the user can type text via the
A password TextBox is aTextBox that hides the information
entered by the user.
set the property Use System Password Char to true, the
TextBox becomes a password TextBox.

3.4. Labels, Textboxes and Button

3.4. Labels, Textboxes and Button

A button is a control that the user clicks to trigger a specific

action or to select an option in a program.
All the button classes derive from class ButtonBase
(namespace System.Windows.Forms).

3.4. Labels, Textboxes and Button

The user enters text into a password box and clicks the
Button, causing the text input to be displayed in the Label.

3.4. Labels, Textboxes and Button

3.5. Groupboxes and panels

GroupBoxes and Panels are typically used to group several

controls of similar functionality or several controls that are
related in a GUI.
The primary difference between these two controls is that
GroupBoxes can display a caption (i.e., text) and do not
include scrollbars, whereas Panels can include scrollbars and
do not include a caption.

3.5. Groupboxes and panels

3.5. Groupboxes and panels

Creating a Panel with scrollbars.

3.5. Groupboxes and panels

uses a GroupBox and a Panel to arrange Buttons.
When these Buttons are clicked, their event handlers
change the text on a Label.

3.5. Groupboxes and panels

3.5. Groupboxes and panels

3.6. Checkboxes and Radio button

C# has two types of state buttons that can be in the on/off or

true/false statesCheckBoxes and RadioButtons.
Classes CheckBox and RadioButton are derived from class

3.6. Checkboxes and Radio button

A Check Box is a small square that either is blank or
contains a check mark.
You can also configure a CheckBox to toggle between
three states (checked, unchecked and indeterminate) by
setting its Three State property to true.

3.6. Checkboxes and Radio button

3.6. Checkboxes and Radio button

allows the user to select CheckBoxes to change a Labels
font style.

3.6. Checkboxes and Radio button

3.6. Checkboxes and Radio button

3.6. Checkboxes and Radio button

Radio buttons
Radio buttons (defined with class RadioButton) are similar
to CheckBoxes in that they also have two states
selected and not selected.
However, Radio Buttons normally appear as a group, in
which only one RadioButton can be selected at a time.

3.6. Checkboxes and Radio button

Using radio button