You are on page 1of 46

C# programming introduction 1

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

Introduction, Development environment


User Interface, controls
Properties and events
Dialogs and forms
C# language basics
C# language arrays and strings
C# language -program flow
Object Oriented programming Methods
Object Oriented programming Classes
File handling
Multimedia & Graphs

C# and .NET programming introduction 2


11.
12.
13.
14.
15.
16.
17.
18.
19.
20.

Debugging
Run-time placement
Threads
Internet communication
Databases Introduction and displaying
Databases Creating
Databases Accessing from code
Plotting
DLL and API
Hardware interfacing - USB interface
2

C# programming introduction 3
Software Visual C# 2008
Download from Microsoft
http://www.microsoft.com/express/download/default.aspx

Free but slightly limited version


Also install SQL 2008 used for databases
Register the product
3

C# programming introduction 4
Development environment
We will create Windows applications

C# programming introduction 5
Design environment:

Solution explorer

Menus

Form

Code editor

Toolbar
messages
5

Properties/events
window

C# programming introduction 6
The Form Most important - place controls the UI.
Display by clicking Form1.cs [Design] tab

Form
Textbox
Button
Label
Listbox

C# programming introduction 7
The Toolbox
Grouped by task
Contains controls
Common controls are:
Buttons,
Textboxes,
Labels,
Radio buttons
etc.

C# programming introduction 8
The Properties / Events window
Properties
Each control has properties e.g.
Name
Position (top and left)
Size (height and width)
Text
Description of property
8

C# programming introduction 9
The Properties / Events window
Events
Events happen to controls
e.g:
Button click
KeyPress
MouseMove
MouseDown
Others Form load

C# programming introduction 10
The Code Editor where you enter your code
Double-click object to enter code
Some added for you do not delete

10

C# programming introduction 11
Your First C# Program
Run C#, start a new Project, > Windows
Application and call it Hello world
Save the project. Select File>Save All.
Display the form (click form1.cs[Design] tab).
Add button (drag and drop) from Toolbox to form

11

C# programming introduction 12
Change the buttons text display (a property).
Display the properties window,
Scroll to the Text property, type in Hello world

12

C# programming introduction 13
Place TextBox and label to form
Change labels caption property to My First C# Program.
Form looks like:

13

C# programming introduction 14
Run program not much happens. Close it.
Double-click button to add code for button
click
Add code:

14

textBox1.Text="Hello world";

C# programming introduction 15
Run program, click button.
Hello World is displayed
Your first C# program !
Note use dot notation to access property
C# is case sensitive

15

C# programming introduction 16
Summary
Free software from Microsoft
Development environment
Form, Code editor, Toolbox,
properties/event window
Drag/drop controls (buttons) to form
Double-click to add code
First program

16

C# programming OOP2 - 17
Topics

Adding methods to class


Static classes available to all objects
Overriding default methods
Inheritance
Protected declaration

17

C# programming OOP2 - 18
Methods
Add method Move
Move Point one place in X and Y direction
Code:
public void Move( )
// declare public
{
_x++;
// move X by one
_y++;
// move Y by one
} // end move
Use:
myPoint.Move( );
18

C# programming OOP2 - 19
Method overloading
Add second Move method
pass distance to move
public void Move(int Xdistance, int Ydistance)
{
_x = _x + Xdistance;
_y = _y + Ydistance;
}
Use both:
myPoint.Move(12,34);
myPoint.Move( );
19

// pass X and Y
// one unit in X and Y

C# programming OOP2 - 20
IntelliSense knows about both:

20

C# programming OOP2 - 21
Static Classes
- dont have to be instantiated.
Distance from Origin example of this
available to all objects
Code:
class Calculate // pass x,y return distance
{
public static double DistanceToOrigin(int x, int y)
{
return Math.Sqrt(x * x + y * y);
}
}
Use:
distance = Calculate.DistanceToOrigin (myPoint.X, myPoint.Y);
21

C# programming OOP2 - 22
More useful ToString method ?
- Override default ToString method
Add code:
public override string ToString( )
{
return "My Point Object is at : " + _x + ," + _y;
}
Use:
MessageBox.Show(MyPoint.ToString( ));
Displays: My Point Object is at : 123,456
22

C# programming OOP2 - 23
Inheritance
Take a class and extend
Seen this when we create our Form:
public partial class Form1 : Form
Lets create Circle Class from our Point Class
Can add radius and area
Code: public Circle : Point

23

C# programming OOP2 - 24
Add new class Circle:
(Project > Add class)
Call it Circle, code:
class Circle : Point
{
}
Can now create a circle:
Circle smallcircle = new Circle( );
Because we are using existing code, its more
reliable
24

C# programming OOP2 - 25
Extend define radius
Constructors:
class Circle : Point
{
private double _radius;
// internal private
public Circle( )
{
}
public Circle(int xValue, int yValue, double radius)
{
_x = xValue;
// _x and _y now declared protected in Point class
// still private to outside world
_y = yValue;
_radius = radius;
}
25

C# programming OOP2 - 26
// add property - radius use get and set
public double radius
{
get
{
return _radius;
}
set
{
if (value >= 0)
_radius = value;
}
}
26

C# programming OOP2 - 27
Extend further
Add method Area:
// method Area
public double area( )
{
return Math.PI * _radius * _radius;
}

27

C# programming OOP2 - 28
Override ToString method:
public override string ToString()
{
return "Circle at x,"+_x+" y,"+_y+
"radius,"+_radius;
}

28

C# programming OOP2 - 29
Use:
Circle smlCircle = new Circle( );
Circle largeCircle = new Circle(12, 34, 56);
smlCircle.X = 98;
smlCircle.Y = 87;
smlCircle.Radius = 10;
MessageBox.Show(smlCircle.ToString( ));
MessageBox.Show(largeCircle.ToString( ));
MessageBox.Show
(smlCircle.area( ).ToString( ));
29

C# programming OOP2 - 30
Summary:

Adding methods
Static classes available to all objects
Overriding default methods
Inheritance extend class
Protected declaration

30

C# programming Hardware 31
Topics:

Serial port
Parallel port
API DLLs
USB

USB Module

31

C# programming Hardware 32
Serial Port control
Non-visual control.
Properties:
BaudRate: 9600,
DataBits: 8,
Parity: None,
PortName: COM1,
StopBits: One.
Main event: DataReceived
Occurs when data is received from the port
32

C# programming Hardware 33
Needs: using System.IO.Ports;
Set properties
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity =
(Parity)Enum.Parse(typeof(Parity), "None");
serialPort1.StopBits =
(StopBits)Enum.Parse(typeof(StopBits), "One");
Open device
serialPort1.Open();
33

C# programming Hardware 34
Send and receive data
serialPort1.WriteLine(textBox1.Text);
listBox1.Items.Add(serialPort1.ReadLine());

Or use DataReceived event

34

C# programming Hardware 35
e.g:
private void serialPort1_DataReceived
(object sender, SerialDataReceivedEventArgs e)

{
listBox1.Items.Add(serialPort1.ReadLine());
}

35

C# programming Hardware 36
Parallel interface.
One way of getting digital I/O.
Data register: Bits 0-7 data
Status Register: Bits: 0-2 not used, 3-Error, 4-Select, 5paper out, 6-acknowledge, 7 busy.
Control Register: Bits: 0 strobe, 1-Auto-feed, 2-initialise,
3-select, 4-IRQ enable, 5-7 not used
Base address (data register) is at 0x378
Status and control at 0x379 and 0x37A.
Eight outputs
Only status register bits are guaranteed inputs
36

C# programming Hardware 37
Accessing the parallel port
Use inpout32.dll - Lake View Research (www.lvr.com).
Provides direct read and write of the I/O
[DllImport("inpout32.dll", EntryPoint = "Out32")]
public static extern void Output(int adress, int value);
[DllImport("inpout32.dll", EntryPoint = "Inp32")]
public static extern int Input(int address);
Use:
Output(port, data);
temp = Input(port);
37

// writes data to port


// read port, puts data in temp

C# programming Hardware 38
USB interfacing
Most popular way of interfacing to the PC.
Complete design involves:

Hardware / USB interface

PC drivers

Understanding protocol and hardware


limitations
Difficult
38

C# programming Hardware 39
The USB interface - 1
USB 2.0 three modes of operation:

High speed (480 Mbits/s),

Full speed (12 Mbits/s) and

Low speed (1.5 Mbits/s).


Device indicates its speed by pulling
D+ or D- data line high.
Power can be taken from USB bus
but strict limitations
39

C# programming Hardware 40
The USB interface 2
The host controls the bus
- initiates & controls all messages
Up to 127 devices on the bus
- a device may not run at its full speed.
USB Connectors:
The A-type is exclusively for a host
B-types are for connection to slaves.
Smaller B-type for small devices such as mobile
phones and digital cameras.
40

C# programming Hardware 41
USB interfacing
Many manufacturers make USB / I/O modules
One is from DLP design: DLP-245PB-G

41

C# programming Hardware 42
The module features - 1:

USB 1.0 and 2.0 compatible


communication at up to 2Mbits/s
18 digital I/O lines (6 as A/D inputs)
Programmable Microchip 16F877A PIC
Pre-programmed code to interface to USB

42

C# programming Hardware 43
The module features - 2:

Code provides access to:


I/O (analogue and digital)
EEPROM and
external digital temperature sensors

Access to the PIC data bus for further


expansion.

No in-depth knowledge of USB hardware or


software is required

40-pin DIL pin-out: further expansion is easy.


43

C# programming Hardware 44
Using the module
Install drivers and DLL can then use from C#

Can read and write directly to I/O


Need to understand protocol
44

C# programming Hardware 45

45

C# programming Hardware 46
Summary

Serial port
Parallel port
API DLLs
USB

USB Module

46

You might also like