You are on page 1of 43

Programming and Interfacing

C#

Ng Quc Cng
Contents

Introduction
Classes, subjects, and methods
Graphical user interface

2
1.Introduction

In 2000, Microsoft announced the C# programming language.


C# has roots in the C, C++ and Java programming
languages.
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.

3
1. Introduction

C# has access to the powerful .NET Framework Class


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

4
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
others).
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
debug.
5
1.2. Introduction to C# apps

Example 1: Displaying a line of text

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

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

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

9
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
braces.
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.
10
1.2. Introduction to C# apps

Example 1: Displaying a line of text


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

11
1.3. Creating a simple app

Creating the console app

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

13
2. Classes, methods

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

14
2. Classes, methods

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

15
2. Classes, methods

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

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

17
3. 1. Windows forms

Windows Forms is one library that can be used to create


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

18
3. 1. Windows forms

19
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

20
3. 2. Event Handling

21
3. 2. Event Handling

Locating event information

22
3.3. Control properties and layout

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

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

25
3.4. Labels, Textboxes and Button

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

27
3.4. Labels, Textboxes and Button

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

28
3.4. Labels, Textboxes and Button

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

30
3.5. Groupboxes and panels

31
3.5. Groupboxes and panels

Creating a Panel with scrollbars.

32
3.5. Groupboxes and panels

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

33
3.5. Groupboxes and panels

34
3.5. Groupboxes and panels

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

36
3.6. Checkboxes and Radio button

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

37
3.6. Checkboxes and Radio button

38
3.6. Checkboxes and Radio button

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

39
3.6. Checkboxes and Radio button

40
3.6. Checkboxes and Radio button

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

42
3.6. Checkboxes and Radio button

Example
Using radio button

43