c Sharp Power Points Free
description
Transcript of c Sharp Power Points Free
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
1
C# and .NET programming – introduction 1
Introduction
A course in C# and .NET programming
Associated book:
C# and .NET Programming for Engineers, John
Allwork,
Publisher: Elektor, 2009,
ISBN: 978-0-905705-81-1
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
2
1. Introduction, Development environment2. User Interface, controls
– Properties and events
3. Dialogs and forms4. C# language – basics5. C# language – arrays and strings 6. C# language -program flow7. Object Oriented programming – Methods8. Object Oriented programming – Classes9. File handling10. Multimedia & Graphs
C# and .NET programming – introduction 2
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
3
C# and .NET programming – introduction 3
11. Debugging
12. Run-time placement
13. Threads
14. Internet communication
15. Databases – Introduction and displaying
16. Databases – Creating
17. Databases – Accessing from code
18. Plotting
19. DLL and API
20. Hardware interfacing - USB interface
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
4
C# and .NET programming – introduction 4
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
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
5
C# and .NET programming – introduction 5
Development environment
We will create Windows applications
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
6
C# and .NET programming – introduction 6
Design environment:
Toolbar
messagesProperties/events window
Code editorForm
Solution explorerMenus
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
7
C# and .NET programming – introduction 7
The Form – Most important - place controls – the UI.
Display by clicking Form1.cs [Design] tab
Form
Textbox
Button
Label
Listbox
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
8
C# and .NET programming – introduction 8
The Toolbox –
Grouped by task
Contains controls
Common controls are:
Buttons,
Textboxes,
Labels,
Radio buttons
etc.
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
9
C# and .NET programming – introduction 9
The Properties / Events window
Properties Each control has properties – e.g.
Name
Position (top and left)
Size (height and width)
Text
Description of property
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
10
C# and .NET programming – introduction 10
The Properties / Events window
Events
Events – happen to controls
e.g:
Button click
KeyPress
MouseMove
MouseDown
Others – Form load
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
11
C# and .NET programming – introduction 11
The Code Editor – where you enter your code
Double-click object to enter code
Some added for you – do not delete
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
12
C# and .NET programming – introduction 12
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
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
13
C# and .NET programming – introduction 13
Change the button’s text display (a property).
Display the properties window,
Scroll to the Text property, type in ‘Hello world’
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
14
C# and .NET programming – introduction 14
Place TextBox and label to form
Change label’s caption property to ‘My First C# Program’.
Form looks like:
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
15
C# and .NET programming – introduction 15
Run program – not much happens. Close it.
Double-click button to add code for button click
Add code: textBox1.Text="Hello world";
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
16
C# and .NET programming – introduction 16
Run program, click button.
‘Hello World’ is displayed –
Your first C# program !
Note use dot notation to access property
C# is case sensitive
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
17
C# and .NET programming – introduction 17
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
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
18
C# and .NET programming – OOP2 - 18
Topics
• Adding methods to class• Static classes – available to all objects• Overriding default methods• Inheritance• Protected declaration
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
19
C# and .NET programming – OOP2 - 19
MethodsAdd method MoveMove Point one place in X and Y directionCode:public void Move( ) // declare public{ _x++; // move X by one _y++; // move Y by one} // end move
Use:
myPoint.Move( );
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
20
C# and .NET programming – OOP2 - 20Method 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); // pass X and YmyPoint.Move( ); // one unit in X and Y
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
21
C# and .NET programming – OOP2 - 21
IntelliSense knows about both:
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
22
C# and .NET programming – OOP2 - 22Static Classes - don’t have to be instantiated.‘Distance from Origin’ example of this
– available to all objectsCode: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);
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
23
C# and .NET programming – OOP2 - 23More 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’
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
24
C# and .NET programming – OOP2 - 24
Inheritance
Take a class and extend
Seen this when we create our Form:
public partial class Form1 : Form
Let’s create Circle Class from our Point Class
Can add radius and area
Code: public Circle : Point
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
25
C# and .NET programming – OOP2 - 25
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, it’s more reliable
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
26
C# and .NET programming – OOP2 - 26
Extend – define radiusConstructors: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; }
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
27
C# and .NET programming – OOP2 - 27
// add property - radius – use get and set
public double radius { get { return _radius; } set { if (value >= 0) _radius = value; } }
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
28
C# and .NET programming – OOP2 - 28
Extend further
Add method – Area:
// method Area
public double area( )
{
return Math.PI * _radius * _radius;
}
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
29
C# and .NET programming – OOP2 - 29
Override ToString method:
public override string ToString()
{
return "Circle at x,"+_x+" y,"+_y+ "radius,"+_radius;
}
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
30
C# and .NET programming – OOP2 - 30
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( ));
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
31
C# and .NET programming – OOP2 - 31
Summary:
• Adding methods• Static classes – available to all objects• Overriding default methods• Inheritance – extend class• Protected declaration
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
32
C# and .NET programming – Hardware 32
Topics:
• Serial port• Parallel port• API DLLs• USB
• USB Module
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
33
C# and .NET programming – Hardware 33
Serial Port controlNon-visual control.
Properties:• BaudRate: 9600, • DataBits: 8, • Parity: None, • PortName: COM1, • StopBits: One.
Main event: DataReceived
Occurs when data is received from the port
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
34
C# and .NET programming – Hardware 34
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();
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
35
C# and .NET programming – Hardware 35
Send and receive data
serialPort1.WriteLine(textBox1.Text);
listBox1.Items.Add(serialPort1.ReadLine());
Or use DataReceived event
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
36
C# and .NET programming – Hardware 36
e.g:
private void serialPort1_DataReceived
(object sender, SerialDataReceivedEventArgs e)
{
listBox1.Items.Add(serialPort1.ReadLine());
}
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
37
C# and .NET programming – Hardware 37
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, 5-paper 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
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
38
C# and .NET programming – Hardware 38
Accessing the parallel portUse 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); // writes data to port
temp = Input(port); // read port, puts data in temp
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
39
C# and .NET programming – Hardware 39
USB interfacingMost popular way of interfacing to the PC.
Complete design involves:• Hardware / USB interface• PC drivers• Understanding protocol and hardware
limitations
Difficult
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
40
C# and .NET programming – Hardware 40
The USB interface - 1USB 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
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
41
C# and .NET programming – Hardware 41
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.
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
42
C# and .NET programming – Hardware 42
USB interfacingMany manufacturers make USB / I/O modules
One is from DLP design: DLP-245PB-G
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
43
C# and .NET programming – Hardware 43
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
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
44
C# and .NET programming – Hardware 44
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.
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
45
C# and .NET programming – Hardware 45
Using the module
Install drivers and DLL – can then use from C#
Can read and write directly to I/O
Need to understand protocol
© 2009 Elektor International Media C# Programming for Engineers, J Allwork
46
C# and .NET programming – Hardware 46