Tài liệu Lập trình trên windows với microsoft® .net
                
              
                                            
                                
            
 
            
                 165 trang
165 trang | 
Chia sẻ: Khủng Long | Lượt xem: 1095 | Lượt tải: 0 
              
            Bạn đang xem trước 20 trang mẫu tài liệu Lập trình trên windows với microsoft® .net, để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên
C# co ban 1-HHK.ppt
C# co ban 2-HHK.ppt
C# co ban 3-HHK.ppt
C# co ban 4-HHK.ppt
C# co ban 5-HHK.ppt
C# co ban 6-HHK.ppt
C# co ban 7- HHK.ppt
C# co ban 8- HHK.ppt
C# co ban 9- HHK.ppt
C# co ban 10-HHK.ppt
Lập trình trên Windows 
với Microsoft® .NET
Giảng viên : Hồ Hoàn Kiếm
 Giới thiệu Microsoft.Net .
 Giới thiệu ngôn ngữ C# và các khái niệm cơ bản.
 Lập trình hướng đối tượng với C#.
 Lập trình Winforms với C#.
 Lập trình cơ sở dữ liệu với ADO.Net .
Nội dung môn học 
 Khảo sát kiến trúc .Net . 
 Xây dựng các ứng dụng với .Net .
 Các thành phần trong .Net Framework.
 Vai trò của CLR và IL .
Giới thiệu Microsoft.NET.
Microsoft .NET Framework Architecture
Windows LINUX
Common Language Runtime
Framework Class Library
Common Language Specification
Microsoft 
Visual Basic®
.NET
C++ C#
Microsoft 
JScript® 
M
ic
ro
s
o
ft V
is
u
a
l S
tu
d
io
®
.N
E
T
XML Web 
Services
Devices
Servers
User 
Experiences
Developer 
Tools
.NET Platform
.NET Framework
Common Language 
Runtime
Class Library
Windows
Web Services
ADO.NET
Data Types
Visual Basic 
Applications
Visual C# 
Applications
Visual C++ 
Applications
Programming Services
.NET 
Platform 
Code
Source code compiles 
as MSIL
JIT compiler produces
machine language
One-stop Application 
Development
Visual Studio .NET
Design Develop Debug Deploy
Data Access
XML Web
Services Tools
Windows
Forms Tools
Web Forms 
Tools
Error 
Handling
Multiple
Languages
Các đặc điểm của Visual Studio .NET
Hổ trợ lập trình đa ngôn ngữ.
Độc lập với hệ điều hành (Platform ).
Xây dựng ứng dụng nhanh chóng và dễ dàng.
Hổ trợ xây dựng ứng dụng cho nhiều thiết bị .
Môi trường thiết kế trực quan .
Hướng đến các ứng dụng trên Internet (Webservice, WAP)
Microsoft Intermediate Language (MSIL)
Common Language Specification
VB C++ C# JScript J#
Common Language Runtime (CLR)
Just In-Time Compiler (JIT)
Operating System
MSIL, JIT và CLR
Thực thi một chương trình .Net
C#
Code
C#
Compiler
Visual Basic
Code
VisualBasic
Compiler
COBOL
Code
COBOL
Compiler
IL
JIT
Compiler
Native
Code
.NET 
source 
code
L
a
n
g
u
a
g
e
C
o
m
p
il
e
r
MSIL
+
Metadata
Machine
code
Code 
executedC
L
R
1st Compilation
2nd Compilation
Thực thi các chương trình .NET
Microsoft Intermediate Language
.NET 
source 
code
L
a
n
g
u
a
g
e
C
o
m
p
il
e
r
MSIL
+
Metadata
Machine
code
Code 
executedC
L
RSIL
Helps Language 
interoperability
IL is not bytecode, but 
is very close to it. 
Therefore, when the 
application is executed, 
the IL to machine code 
conversion is quick!
MSIL converted to 
CPU-specific code by 
CLR
CPU-independent set of 
instructions
.NET 
source 
code
L
a
n
g
u
a
g
e
C
o
m
p
il
e
r
MSIL
+
Metadat
a
Machine
code
Code 
executedC
L
RCLR
Manages memory
Makes it easy to design 
components & 
applications whose 
objects interact across 
languages
Cross-language 
integration 
(especially cross-
language 
inheritance)
Compile once, and run on any 
CPU & OS that supports the 
runtime!
Visual C#.Net và các khái niệm cơ bản
 Được phát triển bởi Microsoft do nhóm Anders Hejlsberg và 
Scott Wiltamuth sáng tác.
 Ngôn ngữ lập trình trực quan, hướng sự kiện, hướng đối 
tượng.
 Dựa theo ý tưởng các ngôn ngữ khác : C,C++, Java và 
Visual Basic.
 Hổ trợ đầy đủ bởi .Net Platform.
 Compiler hiệu quả nhất trong .Net family.
 Khả năng thay thế cho C++.
 Hạn chế sử dụng con trỏ.
Chương trình C#
using System;
class SampleCSharp
{
static void Main(string[] args)
{
Console.WriteLine("Hello world");
}
}
Các kiểu dữ liệu trong C#
 Các kiểu dữ liệu đơn giản : int, float, string , char, bool..
 Các kiểu dữ liệu tham chiếu : đối tượng, lớp.
 Các bổ từ khi sử dụng với biến : private, public, protected
 Các kiểu dữ liệu khác : Array, Struct, Enum...
 Giá trị mặc định cho các kiểu dữ liệu :
Nhập và xuất trong C#
 Sử dụng các phương thức của lớp Console trong 
namspace System.
 Console.WriteLine()
 Console.Write() 
 Console.ReadLine() 
 Console.Read ()
Ví dụ :
using System;
class SampleCSharp
{
static void Main(string[] args)
{
//Nhập vào 1 chuổi 
string s = Console.ReadLine();
//Nhập vào số nguyên 
int n = int.Parse(Console.ReadLine());
//In ra mà hình 
Console.WriteLine("s = {0} va n = {1}",s,i);
}
}
Cấu trúc If
 Cú pháp : 
if (biểu thức )
{
//Các câu lệnh 
}
else 
{
//Cá câu lệnh  
}
Lập trình trên Windows 
với Microsoft® .NET
Giảng viên : Hồ Hoàn Kiếm
Lập trình hướng đối trượng trong C#
 Namespace, Lớp và Đối tượng.
 Các thành phần của Lớp , Đối tượng.
 Constructors và Destructors .
 Nạp chồng phương thức (Overloading).
 Các phương thức chồng toán tử ( Operator 
Overloading ).
 Viết lại các thành phần của lớp (Overriding)
 Kế thừa (Inheritance).
Namespace .
 Tránh sự trùng lắp khi đặt tên lớp.
 Quản lý mã được dễ dàng.
 Giảm bớt sự phức tạp khi chạy với các ứng dụng 
khác 
namespace Tên_Namespace 
{
//Khai báo các lớp
} 
 Có thể khai báo các namespace, class,bên 
trong namespace khác.
Namespace
 Ví dụ 1 :
namespace Sample
{
public class A
{
}
public class B
{
}
}
Namespace
 Ví dụ 2 :
namespace Sample_2
{
public class A
{
}
namspace Sample_3
{
//.
}
}
Lớp và Đối tượng 
 Khai báo :
class Tên_lớp
{
//khai báo các thành phần 
}
 Ví dụ :
class KhachHang
{
private int mMaKhachHang;
private string mTenKhachHang; 
}
Lớp và Đối tượng
 class KhachHang
{ 
//Các thành phần 
//Các phương thức 
public void In()
{
//Các câu lệnh
} 
}
Các thành phần trong Lớp
 Thành phần của lớp 
- khai báo với từ khóa static. 
class KhachHang
{
private static int mMaKH;
public static string mTenKH;
public static void In() { 
// Các câu lệnh
} 
}
 Sử dụng : TênLớp.TênThànhPhần
 ví dụ : KhachHang.mTenKH = 1; 
KhachHang.In() ;
Các thành phần trong Lớp
 Thành phần của đối tượng : 
class KhachHang
{
private int mMaKH;
public string mTenKH;
public void In() { 
// Các câu lệnh
} 
}
 Sử dụng : TênĐốiTượng.TênThànhPhần
 ví dụ : KhachHang objKH = new KhachHang()
objKH .In() ; objKH.mTenKH = “ABC”;
Constructors trong C#
 Phương thức đặc biệt trong lớp.
 Được gọi khi đối tượng được tạo.
 Dùng để khởi dựng đối tượng.
 Cùng tên với tên lớp .
 Không có giá trị trả về.
Constructors trong C#
 class KhachHang
{
private int mMaKH;
private string mTenKH; 
public KhachHang()
{ 
mKH = 0;
mTenKH = “ABC”;
}
} 
 Constructor có thể có tham số .
Constructors trong C#
 class KhachHang
{
private int mMaKH;
private string mTenKH; 
public KhachHang() {
mKH = 0; mTenKH = “ABC”;
}
public KhachHang(int MaKH, string TenKH) 
{ 
mKH = MaKH;
mTenKH = TenKH;
}
} 
Static Constructor 
 Gọi một lần duy nhất trước khi đối tượng được tạo .
 Không có tham số .
 class KhachHang
{
public KhachHang() {
mKH = 0; mTenKH = “ABC”;
}
static KhachHang() 
{ 
// Các câu lệnh
}
} 
Private Constructor 
 Sử dụng khi các thành phần trong lớp là static .
 Không cần thiết tạo đối tượng cho lớp.
 class KhachHang
{
private static int mMaKH;
public static string mTenKH;
public static void In() { 
// Các câu lệnh
} 
private KhachHang() {
}
} 
Destructors trong C#
 Được gọi bởi Garbage Collector .
 Được gọi tự động khi đối tượng được hủy.
class KhachHang
{
public KhachHang() {
mKH = 0; mTenKH = “ABC”;
}
~KhachHang()
{ 
// Các câu lệnh
}
} 
Overloading Methods .
 Các phương thức có cùng tên , khác danh sách 
tham số hoặc kiểu tham số .
public void In() 
{ 
// Các câu lệnh
}
public void In(string s) 
{ 
// Các câu lệnh
}
public void In(int s) 
{ 
// Các câu lệnh
}
Lập trình trên Windows 
với Microsoft® .NET
Giảng viên : Hồ Hoàn Kiếm
Lập trình hướng đối trượng trong C#
 Namespace, Lớp và Đối tượng.
 Các thành phần của Lớp , Đối tượng.
 Constructors và Destructors .
 Nạp chồng phương thức (Overloading).
 Các phương thức chồng toán tử ( Operator 
Overloading ).
 Viết lại các thành phần của lớp (Overriding)
 Kế thừa (Inheritance).
Namespace .
 Tránh sự trùng lắp khi đặt tên lớp.
 Quản lý mã được dễ dàng.
 Giảm bớt sự phức tạp khi chạy với các ứng dụng 
khác 
namespace Tên_Namespace 
{
//Khai báo các lớp
} 
 Có thể khai báo các namespace, class,bên 
trong namespace khác.
Namespace
 Ví dụ 1 :
namespace Sample
{
public class A
{
}
public class B
{
}
}
Namespace
 Ví dụ 2 :
namespace Sample_2
{
public class A
{
}
namspace Sample_3
{
//.
}
}
Lớp và Đối tượng 
 Khai báo :
class Tên_lớp
{
//khai báo các thành phần 
}
 Ví dụ :
class KhachHang
{
private int mMaKhachHang;
private string mTenKhachHang; 
}
Lớp và Đối tượng
 class KhachHang
{ 
//Các thành phần 
//Các phương thức 
public void In()
{
//Các câu lệnh
} 
}
Các thành phần trong Lớp
 Thành phần của lớp 
- khai báo với từ khóa static. 
class KhachHang
{
private static int mMaKH;
public static string mTenKH;
public static void In() { 
// Các câu lệnh
} 
}
 Sử dụng : TênLớp.TênThànhPhần
 ví dụ : KhachHang.mTenKH = 1; 
KhachHang.In() ;
Các thành phần trong Lớp
 Thành phần của đối tượng : 
class KhachHang
{
private int mMaKH;
public string mTenKH;
public void In() { 
// Các câu lệnh
} 
}
 Sử dụng : TênĐốiTượng.TênThànhPhần
 ví dụ : KhachHang objKH = new KhachHang()
objKH .In() ; objKH.mTenKH = “ABC”;
Constructors trong C#
 Phương thức đặc biệt trong lớp.
 Được gọi khi đối tượng được tạo.
 Dùng để khởi dựng đối tượng.
 Cùng tên với tên lớp .
 Không có giá trị trả về.
Constructors trong C#
 class KhachHang
{
private int mMaKH;
private string mTenKH; 
public KhachHang()
{ 
mKH = 0;
mTenKH = “ABC”;
}
} 
 Constructor có thể có tham số .
Constructors trong C#
 class KhachHang
{
private int mMaKH;
private string mTenKH; 
public KhachHang() {
mKH = 0; mTenKH = “ABC”;
}
public KhachHang(int MaKH, string TenKH) 
{ 
mKH = MaKH;
mTenKH = TenKH;
}
} 
Static Constructor 
 Gọi một lần duy nhất trước khi đối tượng được tạo .
 Không có tham số .
 class KhachHang
{
public KhachHang() {
mKH = 0; mTenKH = “ABC”;
}
static KhachHang() 
{ 
// Các câu lệnh
}
} 
Private Constructor 
 Sử dụng khi các thành phần trong lớp là static .
 Không cần thiết tạo đối tượng cho lớp.
 class KhachHang
{
private static int mMaKH;
public static string mTenKH;
public static void In() { 
// Các câu lệnh
} 
private KhachHang() {
}
} 
Destructors trong C#
 Được gọi bởi Garbage Collector .
 Được gọi tự động khi đối tượng được hủy.
class KhachHang
{
public KhachHang() {
mKH = 0; mTenKH = “ABC”;
}
~KhachHang()
{ 
// Các câu lệnh
}
} 
Overloading Methods .
 Các phương thức có cùng tên , khác danh sách 
tham số hoặc kiểu tham số .
public void In() 
{ 
// Các câu lệnh
}
public void In(string s) 
{ 
// Các câu lệnh
}
public void In(int s) 
{ 
// Các câu lệnh
}
Lập trình trên Windows 
với Microsoft® .NET
Giảng viên : Hồ Hoàn Kiếm
Overloading Methods
Áp dụng cho các toán tử :
Overloading Methods
public class Point
{
public int m_x;
public int m_y;
public Point (){ }
public Point(int xx,int yy)
{
m_x = xx ;
m_y = yy; 
}
public static Point operator + (Point p1,Point p2) {
Point result = new Point();
result.m_x = p1.m_x + p2.m_y;
result.m_y = p1.m_x + p2.m_y;
return result; 
}
}
Overloading Methods
static void Main(string[] args)
{
Point objP1 = new Point(1,1);
Point objP2 = new Point(2,2);
Point objResult = new Point();
objResult = objP1 + objP2;
Console.WriteLine("The result is m_x = {0} and m_y = {1}", 
objResult.m_x , objResult.m_y);
}
Kiểu Structs
 Kiểu dữ liệu do người dùng định nghĩa.
 Có thể định nghĩa các phương thức.
 Có thể định nghĩa các Constructors
 Structs là kiểu tham trị .
 Structs không được kế thừa .
Kiểu Structs
public struct Point 
{
public int x, y;
public Point(int p1, int p2) 
{
x = p1;
y = p2; 
}
public int Add()
{
return x+y;
}
}
Kiểu Structs
static void Main(string[] args)
{ 
Point p1 = new Point() ;
Point p2 = new Point(10,10);
Console.Write(" Point 1: ");
Console.WriteLine("x = {0}, y = {1}", p1.x, p1.y);
Console.Write(" Point 2: ");
Console.WriteLine("x = {0}, y = {1}", p2.x, p2.y);
Console.WriteLine("Call Add method: {0}",p2.Add());
Console.ReadLine();
}
Kiểu Enumrator
 Tập hợp các hằng số đã được đặt tên. 
enum Days
{
//mac dinh Phan tu dau tien bat tu 0
Sat,
Sun,
Mon, 
Tue,
Wed,
Thu,
Fri
};
Kiểu Enumrator
public static void Main() 
{
int x = (int) Days.Sun;
int y = (int) Days.Fri;
Console.WriteLine("Sun = {0} : Value : {1}", x,Days.Sun);
Console.WriteLine("Fri = {0} : Value : {1}", y,Days.Fri );
}
 Kết quả : 
Sun = 2 : Value : Sun
Fri = 7 : Value : Fri
Properties
 Sử dụng để truy cập các thành phần Private.
 Kiềm tra dữ liệu cho các thành phần trong lớp .
public class Square
{
public double mSide; 
}
public class Test {
static void Main() {
Square obj = new Square ();
obj. mSide = 3;
}
}
Properties 
public class Square
{
//Khi báo các thành phần
private double mSide;
// Khai báo property
public double Side
{
get { };
set { };
}
// Khai báo các phương thức
}
Properties 
 Property giống như field, nhưng việc truy cập đươc 
thực hiện qua thao tác get và set .
public int Side 
{
get
{
return mSide;
}
set
{
if (mSide < 0) return ; 
mSide = value;
}
} 
Properties 
namespace SampleProperty
{
public class Rectangle 
{
private int m_Length ;
private int m_Width ;
private int m_Area ;
public Rectangle()
{
m_Length = 3; // Length = 3;
m_Width = 2; 
}
Properties 
public int Length 
{
get
{
return m_Length; 
}
set
{
if(m_Length < 0 ) return;
m_length = value; 
}
}
Properties 
public int Width 
{
get
{
return m_Width; 
}
set
{
// Đọan mã kiểm tra giá trị ..
m_Width = value; 
}
} 
Properties 
public int Area 
{
get
{
return m_Area; 
}
} 
public void CalArea()
{
m_Area = m_Length*m_Width;
}
Properties 
public class Test 
{
public static void Main() 
{
Rectangle objRectangle = new Rectangle();
objRectangle. Length = 3;
objRectangle. Width = 4;
objRectangle.CalArea();
Console.WriteLine("{0}", objRectangle. Area ); 
}
}
Indexer
 Khi thành phần của lớp là các kiểu tập hợp.
 Sử dụng với : new, virtual, sealed, override, abstract, 
extern.
 Indexer giống như Property, đuợc truy cập thông qua 
get và set
 Khai báo sử dụng từ khóa this.
 Khi sử dụng Indexer ta sử dụng dấu [] cho get và 
set .
Indexer
class IndexerClass 
{
private int [] myArray = new int[100]; 
public int this [int index] 
{ 
get {
If (index = 100) return 0;
else
return myArray[index];
}
set {
if (!(index = 100))
myArray[index] = value;
}
}
}
Indexer
public class Test
{
public static void Main() 
{
IndexerClass b = new IndexerClass();
b[3] = 256;
b[5] = 1024;
for (int i=0; i<=10; i++) 
{
Console.WriteLine("Element # {0} = {1}", i, b[i]);
}
}
}
Lập trình trên Windows 
với Microsoft® .NET
Giảng viên : Hồ Hoàn Kiếm
Kế thừa trong C#
 Cho phép khai báo 1 lớp mới được dẫn xuất từ 1 
lớp đã có.
 Sử dụng lại các đọan mã đã viết .
 Hổ trợ đơn thừa kế.
 Không cho phép đa thừa kế.
 Cho phép thực thi nhiều interface
Kế thừa trong C#
class Software
{
private int m_z;
public int m_v;
protected int m_x;
public Software()
{
m_x = 100;
}
public Software(int y)
{
m_x = y;
}
}
Kế thừa trong C#
class MicrosoftSoftware : Software
{
public MicrosoftSoftware()
{
Console.WriteLine(m_x);
}
}
Kế thừa trong C#
class IBMSoftware : Software
{
public IBMSoftware(int y) : base(y) {
Console.WriteLine(m_x);
}
public IBMSoftware(string s, int f) : this(f) {
Console.WriteLine(s);
}
}
Kế thừa trong C#
static void Main(string[] args)
{ 
MicrosoftSoftware objMS = new MicrosoftSoftware();
IBMSoftware objIBM1 = new IBMSoftware(50);
IBMSoftware objIBM2 = new IBMSoftware("test",75);
Console.ReadLine();
}
Kế thừa trong C# 
 Từ khóa sealed : Lớp không cho phép kế thừa 
public sealed class A
{
}
public class B : A 
{
}
 Lớp B không được phép kế thừa lớp A .
Overriding Method 
class Animal
{
public Animal()
{
Console.WriteLine("Animal constructor");
}
public void Talk()
{
Console.WriteLine("Animal talk");
}
}
Overriding Method 
class Dog : Animal
{
public Dog() {
Console.WriteLine("Dog constructor");
}
public new void Talk()
{
Console.WriteLine("Dog talk");
}
}
Overriding Method 
class Test
{
static void Main(string[] args)
{
Animal a1 = new Animal();
a1.Talk();
Dog d1 = new Dog();
d1.Talk();
}
}
Tính đa hình - Polymorphism 
class Animal
{
public Animal()
{
Console.WriteLine("Animal constructor");
}
public virtual void Talk()
{
Console.WriteLine("Animal talk");
}
}
Tính đa hình - Polymorphism
class Dog : Animal
{
public Dog() {
Console.WriteLine("Dog constructor");
}
public override void Talk()
{
Console.WriteLine("Dog talk");
}
}
Tính đa hình - Polymorphism
class Test
{
static void Main(string[] args)
{
Animal objA = new Animal();
Dog objD = new Dog();
objA = objD ; 
objA.Talk();
}
}
Lớp trừu tượng – Abstract Class
 Không được tạo đối tượng.
 Có thể định nghĩa các phương thức.
 Có thể mở rộng từ lớp dẫn xuất.
 Dùng để làm lớp cơ sở.
 Có thể thực thi interface
Lớp trừu tượng – Abstract Class
abstract class Shape 
{
// Khai cac field
protected float m_Height = 5;
protected float m_Width = 10;
//Khai bao cac method
public abstract void CalculateArea();
public abstract void CalculateCircumference();
public void PrintHeight(){
Console.WriteLine("Height = {0}",m_Height); 
}
public void PrintWidth() {
Console.WriteLine("Width = {0}",m_Width); 
}
}
Lớp trừu tượng – Abstract Class
class Rectangle:Shape
{
public Rectangle( {
m_Height = 20;
m_Width = 30;
}
public override void CalculateArea() {
Console.WriteLine("Area : {0}",m_Height * m_Width ); 
}
public override void CalculateCircumference() {
Console.WriteLine("Circumference = 
{0}",(m_Height+m_Width)*2); 
}
}
Lớp trừu tượng – Abstract Class
class Test
{
static void Main(string[] args)
{
Rectangle objRec = new Rectangle();
objRec.CalculateArea(); 
objRec.CalculateCircumference(); 
}
}
Giao diện – Interface
 Không được tạo đối tượng.
 Không thể định nghĩa các phương thức.
 Lớp thực thi interface phải thực thi tất cả các 
phương thức của interface.
 Interface có thể được kế thừa các interface
khác.
Giao diện – Interface
interface ITest
{
void Print();
}
class Base:ITest
{
public void Print()
{
Console.WriteLine("Print method called");
}
}
Giao diện – Interface
static void Main(string[] args)
{
Base obj = new Base();
obj.Print(); 
//Gọi phương thức Print() bằng interface ITest
ITest ib = (ITest)obj ;
ib.Print(); 
//Gọi phuong thức Print() bằng cách ép kiểu Interface 
ITest về lớp Base
Base ojB = (Base)ib;
ojB.Print();
}
Lập trình trên Windows 
với Microsoft® .NET
Giảng viên : Hồ Hoàn Kiếm
Lập trình với Winform
User Interface
Program
Lập trình với Winform
 Sử dụng System.Windows.Forms
 Dùng chung giao diện với các ứng dụng khác .
 Dựa trên nền tảng lập trình hướng đối tượng.
 Giao diện winform được kế thừa từ lớp Form
 Form gồm nhiều đối tượng Visual Inrterface 
Component ( gọi là Controls )
Events và EventHandler
Tương tác giữa các đối tượng với ứng dụng.
Event Handling
When Save Clicked()
{
Display Message(“Save Button”);
}
Event
Event Handler
Các control cơ bản
 Label
 TextBox
 Button
 ListBox
 ComboBox
 CheckedListBox
Các control cơ bản
PROPERTIES
Image
TabStop
Paint
Events
Methods
Label
Các control cơ bản
PROPERTIES
Events
Methods
MaxLength
Multiline
Passwordchar
ReadOnly
Copy
Cut
TextChanged
TextBox
Các control cơ bản
PROPERTIES
Events
Methods
DialogResult
TextAlign
Button
Các control cơ bản
PROPERTIES
Items
MultiColumn
SelectedIndex
SelectedItem
SelectedItems
SelectedValue
Sorted
Text
ListBox [1]
Các control cơ bản
Events
Methods
ClearSelected
FindString
GetSelected
SetSelected
SelectedIndexChanged
SelectedValueChanged
ListBox [2]
Các control cơ bản
PROPERTIES
Events
Methods
CheckedIndices
CheckedItems
GetItemChecked
GetItemCheckState
SetItemChecked
SetItemCheckState
ItemCheck
CheckedListBox
Các control cơ bản
PROPERTIES
Events
Methods
DropDownStyle
Focused
MaxDropDownItems
Select
SelectAll
DropDown
ComboBox
Giao diện 
Từ khóa this
 Sử dụng để truy cập các thành phần trong 
form hiện hành . 
this.ControlName.PropertyName 
this.ControlName.MethodName 
this.ControlName.EventName 
Lập trình trên Windows 
với Microsoft® .NET
Giảng viên : Hồ Hoàn Kiếm
Sử dụng controls trong C#
PrintDocument
PrintPreviewControl
Printing Support Controls
Sử dụng controls trong C#
PROPERTIES
AutoZoom
Columns
Document
Rows
StartPage
UseAntiAlias
Zoom
InvalidatePreview
PrintPreviewControl
Sử dụng controls trong C#
Dialog Boxes
Uses 
Types (by Presentation)
To display information and messages for 
the user
As a means of accepting user-input 
Sử dụng controls trong C#
Dialog Types
Types of Dialog (by definition)
•OpenFileDialog
•PageSetUpDialog
•FontDialog
•ColorDialog
•SaveFileDialog
•PrintPreviewDialog
•PrintDialog
Custom dialog boxes
Common dialog boxes
Sử dụng controls trong C#
PROPERTIES
CheckFileExists
DefaultExt
FileName
FileNames
Filter
InitialDirectory
MutiSelect
ReadOnlyChecked
RestoreDirectoryShowHelp
ShowReadOnly
OpenFileDialog
Sử dụng controls trong C#
PROPERTIES
AllowVectorFonts
AllowVerticalFonts
Color
Font
MaxSizeMinSize
ShowColor
ShowEffects
ShowHelp
FontDialog
Sử dụng controls trong C#
AllFullOpen
AnyColor
Color
CustonColors FullOpen
ShowHelpSolidColorOnly
PROPERTIES
ColorDialog
Sử dụng controls trong C#
PROPERTIES
CreatePrompt
OverwritePrompt
OpenFile
SaveFileDialog
Sử dụng controls trong C#
MessageBox [1]
 MessageBox
Code for showing Messagebox:
MessageBox.Show(“[Message]”);
 MessageBox method is overloaded in twelve
more ways to give more functionality
Sử dụng controls trong C#
MessageBox [2]
•MessageBoxButtons
•MessageBoxIcon
•MessageBoxDefaultButton
•MessageBoxOptions
Sử dụng controls trong C#
MessageBoxButton
Member Name
AbortRetryIgnore
OK
OKCancel 
RetryCancel 
YesNo 
YesNoCancel 
Sử dụng controls trong C#
MessageBoxIcon
Member Name Member Name
Asterisk Question
Error Stop
Exclamation Warning 
Hand 
Information 
None 
Sử dụng controls trong C#
MessageBoxOptions
Member Name
DefaultDesktopOnly
RightAlign
RtlReading
ServiceNotification 
Sử dụng controls trong C#
MessageBoxDefaultButton
Member Name
Button1
Button2
Button3
Sử dụng controls trong C#
Visual Effects in WinForms
Transparent Forms
Control Docking
Control Anchoring
Visual Inheritance
Sử dụng controls trong C#
Transparent Forms
this.Opacity = 0.83;
Sử dụng controls trong C#
Control Anchoring
Control 
Anchored
Control not 
Anchored
Sử dụng controls trong C#
Visual Inheritance
Creates a base Form
Used at various places
Allows changes to be made in the 
inherited form
Lập trình trên Windows 
với Microsoft® .NET
Giảng viên : Hồ Hoàn Kiếm
SDI Application
SDI (1)
New 
Document
An SDI 
application can 
have only one 
active window 
at a time
MDI Application
MDI (2)
Inner Window 
(or)
Child Window
Outer Window 
(or)
Parent 
Window
MDI Application
Designing an MDI application
IsMdiContainer = true
MDI Application
Activating and Deactivating Windows
Deactivate 
child 
window
Active 
child 
window
MDI Application
As in MDI form 
during design
As with the control 
at runtime
Main Menu
 Displays a menu at runtime
 MainMenu can be created in two ways:
Using the MainMenu 
control, and menu designer 
at design time
Creating an instance of the 
MainMenu class at runtime
MainMenu myMainMenu = new MainMenu(); 
this.Menu = myMainMenu;
Main Menu
PROPERTIES
EVENTS
METHODS
Index
ShowShortCut
MenuItems
Context Menu
 Context menus give users access to frequently used 
commands by clicking the right mouse button
 Context menus are created with the help of the 
ContextMenu control
ToolBar
 It is situated immediately below the menu bar
 The buttons in a ToolBar are analogous to the items in a menu
 Different button appearance options:
• DropDownButton
• Separator
• ToggleButton
ToolBar control icon 
as in ToolBox
Form Level and Control Level Validations
Validate data in all the fields in the form
Enable and Disable controls based on
user input
KeyPress
KeyDown
KeyUp
Errors Description
Syntax Error Occurs when we enter an incorrect line of code such as a 
mistake in typing a keyword, missing punctuation, or an 
incorrectly specified variable. 
Run-time Error Occurs when a command attempts to perform an invalid 
action. 
Logic Error Occurs when an application executes without syntax 
errors or run-time errors, but the results are not which 
were intended. 
Types of Errors
ErrorProvider Control
Properties Methods
BlinkRate GetError
BlinkStyle SetError
ContainerControl 
Icon 
ErrorProvider control can be used to 
display an error icon when the user enters 
invalid data.
Exception Handling
Exceptions can be handled with the help of :
Lập trình cơ sở dữ liệu 
với ADO.Net
Giảng viên : Hồ Hoàn Kiếm
Lập trình cơ sở dữ liệu
Stored
into
Database
Client
ADO.net
Data access technology
Lập trình cơ sở dữ liệu
Client-Server Application
Client Server
Provides data to
client
C
lie
n
t 
S
o
ft
w
a
re
D
a
ta
b
a
se
Lập trình cơ sở dữ liệu
Data Access Components
Database
User Server Running
Database
Database
Requests data
Sends data
Server DownClient Software
Requests data
Error Message
DAC
(ODBC, OLE DB, 
ADO, ADO.net)
Giới thiệu về ADO.Net
Hổ trợ bởi .Net Platform
Sử dụng công nghệ XML để chuyển đổi dữ liệu. 
Tương tác vói tất cả các lọai cơ sở dữ liệu.
Khả năng thực thi nhanh .
Sử dụng cho các lọai ứng dụng client-server .
Giới thiệu về ADO.Net
ADO.net architecture
Database
XML
XML
Connection
A
D
O
A
D
O
.N
E
T
Command
Recordset
XxxConnection
XxxCommand
DataSet
XxxTransaction
XxxDataReader
XxxDataAdapter
Mô hình chuyển đổi giữa ADO và ADO.NET
Các loại .NET Data Provider
 XxxConnection – for example, SqlConnection
 XxxTransaction – for example, SqlTransaction
 XxxException – for example, SqlException
 XxxError – for example, SqlError
 XxxCommand – for example, SqlCommand
 XxxParameter – for example, SqlParameter
 XxxDataReader – for example, SqlDataReader
 XxxDataAdapter – for example, 
SqlDataAdapter
DataSet
SQL Server .NET 
Data Provider
OLE DB .NET 
Data Provider
SQL Server 7.0
(and later)
OLEDB sources
(SQL Server 6.5)
Mô hình ADO .NET Object 
Connected và Disconnected trong 
ADO.NET
SqlConnection
SqlDataAdapter
DataSet
SQL Server 7.0
(and later)
SQL Server 7.0
(and later)
SqlConnection
SqlCommand
SqlDataReader
ADO.NET and XML
 ADO.NET is tightly integrated with XML
 Using XML in a disconnected ADO.NET 
application
XML Web Services
DataSet
Request data
1
SQL query
2
Results
3XML4
Updated XML5
SQL 
updates6
Data SourceClient
DataSet
Lập trình trên Windows 
với Microsoft® .NET
Giảng viên : Hồ Hoàn Kiếm
SqlDataAdapter
 Bộ lọc dữ liệu.
 Phân trang dữ liệu.
 Phương thức 
fill(datasource);
fill(datasource,start,number,”tablename”)
SqlCommand
 Thực thi câu lệnh SQL.
 Phương thức :
ExecuteNonQuery()
ExecuteScala()
ExecuteReader() 
DataSet
 Chứa dữ liệu trả về từ DataAdapter.
 “Cơ sở dữ liệu thu nhỏ”.
 Gồm các đối tượng :
- DataTable
- DataRelation
DataSet
 Mô hình DataSet
DataTable
 Chứa dữ liệu trả về DataSet hoặc 
DataAdapter.
 Gồm các đối tượng :
- DataColumn
- DataRow
Create a DataTable
 myTable.Columns.Add("ItemID",Type.GetType("System.Int
32"));
 myTable.Columns.Add("ItemName",Type.GetType("Syste
m.String"));
 myTable.Columns.Add("Quantity",Type.GetType("System.I
nt32"));
 myTable.Columns.Add("Quantity",Type.GetType("System.fl
oat"));
 // thêm column mới vào table 
 myTable.Columns.Add(myColumn); 
 myTable.Columns.Add("SubTotal",Type.GetType("System.I
nt32"),"Quantity*Price");
 myTable.PrimaryKey = new 
DataColumn[]{myTable.Columns[0]};
Insert data into DataTable
 // Thêm 10 dòng vào Table
 DataRow myRow; 
 for(int i = 0; i < 10; i++)
 { 
 myRow = myTable.NewRow(); 
 myRow["ItemID"] = i + 1; 
 myRow["Price"] = i*2+1; 
 myRow["ItemName"] = (60+i); 
 myRow["Quantity"] = i + 1; 
 myTable.Rows.Add(myRow); 
 }
DataView
 Dùng để quản lý và thao tác dữ liệu trong 
DataTable
 Dùng để lọc , sắp xếp dữ liệu trong 
DataTable
DataView
DataBinding
 Hiển thị dữ liệu trong DataTablevào các 
control ( TextBox, ComboBox, DataGrid)
 Gồm 2 loại chính :
- Binding Sample
- Binding Complex
            Các file đính kèm theo tài liệu này:
 tailieu.pdf tailieu.pdf