chương 3 windows forms nội dung lập trình c# trên windows tạo ứng dụng windows forms...
TRANSCRIPT
![Page 1: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/1.jpg)
Windows Forms
Chương 3
![Page 2: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/2.jpg)
2
Nội dung
Lập trình C# trên Windows
Tạo ứng dụng Windows Forms từ đầu
Tạo ứng dụng Windows Forms từ Wizard
Tổng quan các đối tượng trong Windows Forms
Lớp Application
Lớp Form
Các Control thông dụng
• Label, LinkLabel, Textbox, Button
• Checkbox, CheckboxList, RadioButton
• PictureBox và Bitmap
![Page 3: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/3.jpg)
3
Nội dung
Bố cục control
• Panel
• Anchor
• Dock
• Splitter
MessageBox
Các dialog thông dụng
Custom Form
• Truyền dữ liệu cho form bằng tham số
• Truyền dữ liệu cho form bằng property
![Page 4: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/4.jpg)
4
Nội dung
Menu:
• MenuStrip, ContextMenuStrip
• StatusStrip, ToolStrip, ToolStripContainer
Ứng dụng SDI, MDI
• Modal Form
• Modeless Form
Các Dialog thông dụng
Mouse và Keyboard
![Page 5: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/5.jpg)
Lập trình C# trên Windows
![Page 6: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/6.jpg)
6
Lập trình trên WindowsKhái niệm
Message (Thông điệp)
• Một message là một con số nguyên được quy ước trước giữa Windows và các ứng dụng (Application)
• Các dữ liệu nhập (từ bàn phím, từ chuột, …) đều được Windows chuyển thành các message và một số thông tin kèm theo message
• Ví dụ
– 0x0001 WM_CREATE
– 0x0002 WM_DESTROY
– 0x0003 WM_MOVE
– 0x0005 WM_SIZE
– 0x0012 WM_QUIT
![Page 7: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/7.jpg)
7
Lập trình trên WindowsKhái niệm
System Queue (Hàng đợi hệ thống):
• Windows chứa các message trong một hàng
đợi gọi là hàng đợi hệ thống.
Application Queue (Hàng đợi ứng dụng ):
• Các ứng dụng có hàng đợi riêng để chứa các
message của ứng dụng gọi là hàng đợi ứng
dụng.
Windows sẽ tự động phân bố các message
từ System Queue đến các Application Queue
![Page 8: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/8.jpg)
8
Lập trình trên WindowsKhái niệm
Hardware
input
Hệ điều hành Windows
Ứng dụng A
Nhận và
xử lý
Ứng dụng B
Nhận và
xử lý
Message loop
Message loop
Hàng đợi
hệ thống
Hàng đợi
của
ứng dụng A
Hàng đợi
của
ứng dụng B
![Page 9: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/9.jpg)
9
Lập trình trên WindowsEvent-driven programming model
Message loop (vòng lặp thông điệp)
• Mỗi Application tại một thời điểm có một message loop để lấy các message trong Application Queue về để phân bố cho các cửa sổ (Window) trong Application
Hàm Window Procedure
• Mỗi cửa sổ (Window) trong Application đều có một hàm Window procedure để xử lý các message do message loop nhận về
![Page 10: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/10.jpg)
10
Lập trình trên WindowsEvent-driven programming model
WM_PAINT
WM_MOUSEMOVE
WM_KEYDOWNOnKeyDown
OnMouseMove
OnPaint
Messages
Message
queue
Application Message handlers
Message loop
Window procedure
Re
trie
ve
d m
es
sa
ges
Dis
patc
hed
m
es
sa
ges
![Page 11: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/11.jpg)
11
Lập trình trên WindowsEvent-driven programming model
Event-driven programming model:
• Ứng dụng phản ứng các sự kiện (nhấn phím, click chuột,
...) bằng cách xử lý các message do Windows gởi đến
• Một ứng dụng Windows điển hình thực hiện một lượng
lớn các xử lý để phản hồi các message nó nhận. Và giữa
các message nó chờ message kế tiếp đến
Message queue: Các message được chờ trong
message queue cho đến khi chúng được nhận để xử lý
![Page 12: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/12.jpg)
12
Lập trình trên WindowsEvent-driven programming model
Hàm Main: Hàm Main tạo một cửa sổ và vào message loop
Message loop:
• Nhận các message và phân bố chúng đến Window Procedure của các cửa sổ
• Message loop kết thúc khi nhận được WM_QUIT (chọn Exit từ menu File, click lên close button)
Window Procedure:
• Phần lớn các đoạn mã đặt trong Window Procedure.
• Window Procedure xử lý các message gởi đến cửa sổ
• Window Procedure điển hình chứa câu lệnh switch lớn với mỗi case là một message riêng.
Message handler: Code cung cấp để xử lý message cụ thể gọi là message handler
![Page 13: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/13.jpg)
13
Lập trình trên WindowsEvent-driven programming model
Event – driven programming model trong C#
• Message Loop Application.Run()
• Window Form
• Window Procedure WndProc(ref Message m)
• Phần lớn các Message handlers được cài đặt sẵn trong các lớp có thể nhận message (Control, Form, Timer, …) dưới dạng các hàm protected:
protected void OnTenMessage(xxxEventArgs e)
– xxxEventArgs có thể là EventArgs hay các lớp con của EventArgs
• Mỗi message có một biến event tương ứng.
• Các Message handlers mặc nhiên gọi các event tương ứng của message
• Các hàm gán cho event gọi là event handler
![Page 14: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/14.jpg)
14
Lập trình trên WindowsEvent-driven programming model
WM_PAINT
WM_MOUSEMOVE
WM_KEYDOWNOnKeyDown
OnMouseMove
OnPaint
Messages
Message
queue
Application
Message handlers
gọi các sự kiện tuơng ứng
Application.Run()
WndProc(ref Message m)
Re
trie
ve
d m
es
sa
ges
Dis
patc
hed
m
es
sa
ges
![Page 15: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/15.jpg)
Tạo ứng dụng
Windows Forms từ đầu
![Page 16: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/16.jpg)
16
Các bước cơ bản để tạo ứng dụng Windows
Bước 1:
• Thiết kế giao diện
Bước 2:
• Xử lý các message do Windows gởi đến
Bước 3:
• Xử lý nghiệp vụ
![Page 17: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/17.jpg)
17
Các bước cơ bản để tạo ứng dụng Windows
Cách 1: Trực tiếp – thừa kế
• Thiết kế giao diện
– Tạo lớp thừa thừa kế từ lớp Form
– Bố cục các control
– Thiết lập các property cho các control
• Xử lý các thông điệp do Windows gởi đến:
bằng cách override các message handler
• Xử lý các nghiệp vụ trên các message handler
![Page 18: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/18.jpg)
18
Các bước cơ bản để tạo ứng dụng Windows
Cách 2: Gián tiếp qua các field event
• Thiết kế giao diện
– Bố cục các control
– Thiết lập các property cho các control
• Bắt các sự kiện: bằng cách viết các event
handler
• Xử lý nghiệp vụ trên các event handler
![Page 19: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/19.jpg)
19
Các bước cơ bản để tạo ứng dụng Windows
Bước 1: Tạo Empty Project• File New Project
• Project Type: Visual C# Windows
• Template: Empty Project
Bước 2: Thêm references• Click phải lên References Add Reference...
– System.Windows.Forms
– System.Drawing
– [System.Core]
Bước 2: using các namespaceusing System.Windows.Forms;using System.Drawing;
Bước 3: Thêm class file• Click phải lên project Add Class...
Bước 4: Viết code
Bước 5: menu Project Property Output type: Windows Application
![Page 20: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/20.jpg)
20
Dùng Form, Không thừa kế
class Program
{
static void Main()
{
Form form = new Form();
form.Text = “First Application”;
Application.Run(form);
}
}
![Page 21: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/21.jpg)
21
Dùng Form, Không thừa kế
class Program
{
static void Main()
{
Form form = new Form();
form.Text = "WinForm";
form.BackColor = Color.Green;
form.Width = 300;
form.Height = 300;
form.MaximizeBox = false;
form.Cursor = Cursors.Hand;
form.StartPosition = FormStartPosition.CenterScreen;
Application.Run(form);
}
}
Thuộc tính
![Page 22: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/22.jpg)
22
Dùng Form, Không thừa kế
class Program
{
static void Main()
{
Form form = new Form();
form.Text = “WinForm”;
form.Click += form_Click;
Application.Run(form);
}
static void form_Click(object sender, EventArgs e)
{
MessageBox.Show("Ban da click vao form");
}
}
Event Handler
![Page 23: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/23.jpg)
23
Dùng Form, Không thừa kế
class Program
{
static void Main()
{
Form form = new Form();
form.Text = "WinForm";
Button button = new Button();
button.Text = "OK";
button.Location = new Point(100, 100);
button.Click += new EventHandler(button_Click);
form.Controls.Add(button);
Application.Run(form);
}
static void button_Click(object sender, EventArgs e)
{
MessageBox.Show("Ban da click vao nut OK");
}
}
Thêm control vào form
![Page 24: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/24.jpg)
24
Dùng form bằng cách kế thừa
class MainForm:Form
{
public MainForm()
{
this.Text = "WinForm";
button = new Button();
button.Text = "OK";
button.Location = new Point(100, 100);
button.Click += new EventHandler(button_Click);
this.Controls.Add(button);
}
void button_Click(object sender, EventArgs e)
{
MessageBox.Show("Ban da click vao nut OK");
}
private Button button;
}
![Page 25: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/25.jpg)
25
Dùng form bằng cách kế thừa
class Program
{
static void Main()
{
MainForm form = new MainForm();
Application.Run(form);
}
}
![Page 26: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/26.jpg)
26
Dùng form bằng cách kế thừa
Bắt các sự kiện trên form
• Cách 1: Thông qua field event
class MainForm:Form
{
public MainForm()
{
this.Text = "WinForm";
this.Click += form_Click;
}
void form_Click(object sender, EventArgs e)
{
MessageBox.Show("Ban da click vao form");
}
}
![Page 27: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/27.jpg)
27
Dùng form bằng cách kế thừa
class MainForm:Form
{
public MainForm()
{
this.Text = "WinForm";
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
MessageBox.Show("Ban da click vao form");
}
}
Bắt các sự kiện trên form
• Cách 2: Thông Qua override các message handler
![Page 28: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/28.jpg)
Tạo ứng dụng
Windows Forms từ Wizard
![Page 29: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/29.jpg)
29
Tạo ứng dụng bằng Wizard
Bước 1: Tạo Empty Project
• File New Project
• Project Type: Visual C# Windows
• Template: Windows Forms Application
![Page 30: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/30.jpg)
30
Tạo ứng dụng bằng Wizard
Các thành phần của cửa sổ thiết kế
Form đang thiết kế
Toolbox
Properties Windows
Solution Windows
![Page 31: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/31.jpg)
31
Tạo ứng dụng bằng Wizard
Bước 2: Thiết kế giao diện: Kéo các đối tượng từ Toolbox vào form
![Page 32: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/32.jpg)
32
Tạo ứng dụng bằng Wizard
Bước 3: Thiết lập các Property cho các đối tượng trên form thông qua Properties Windows
Object
Drop-Down
Hiển thị
theo vần
Hiển thị
theo loại
Properties Events
Giải thích ý nghĩa
của mục đang chọn
![Page 33: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/33.jpg)
33
Tạo ứng dụng bằng Wizard
Bước 4: Bắt các sự kiện cho các đối tượng trên form từ Properties Windows
![Page 34: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/34.jpg)
34
Tạo ứng dụng bằng Wizard
Bước 5: Xử lý nghiệp vụ bằng các viết code cho
các event handler
![Page 35: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/35.jpg)
35
Code do Wizard sinh ra
Lớp Program
![Page 36: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/36.jpg)
36
Code do Wizard sinh ra
Lớp MainForm
![Page 37: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/37.jpg)
37
Code do Wizard sinh ra
Phương thức InititiallizeComponent()
![Page 38: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/38.jpg)
Tổng quan các đối tượng
trong Windows Forms
![Page 39: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/39.jpg)
39
Cấu trúc của ứng dụng
Ứng dụng Windows Forms có 3 phần chính
• Application
• Các Form trong Application
• Các Controls và Components trên Form
![Page 40: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/40.jpg)
Lớp Application
![Page 41: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/41.jpg)
41
ApplicationKhái niệm
Lớp Application cung cấp các phương thức tĩnh và các property tĩnh để quản lý ứng dụng
• Các phương thức start, stop ứng dụng, xử lý Windows messages,
• Các property lấy thông tin về ứng dụng
• Lớp này không thể thừa kế
Namespace
• System.Windows.Form
Assembly
• System.Windows.Form (System.Windows.Form.dll)
![Page 42: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/42.jpg)
42
ApplicationProperties
public sealed class Application
{ // Properties
public static string CommonAppDataPath { get; }
public static RegistryKey CommonAppDataRegistry { get; }
public static string CompanyName { get; }
public static CultureInfo CurrentCulture { get; set; }
public static InputLanguage CurrentInputLanguage { get; set;}
public static string ExecutablePath { get; }
public static string LocalUserAppDataPath { get; }
public static bool MessageLoop { get; }
public static FormCollection OpenForms {get; }
public static string ProductName { get; }
public static string ProductVersion { get; }
public static bool RenderWithVisualStyles { get; }
public static string SafeTopLevelCaptionFormat { get; set; }
public static string StartupPath { get; }
public static string UserAppDataPath { get; }
public static RegistryKey UserAppDataRegistry { get; }
public static bool UseWaitCursor { get; set; }
public static VisualStyleState VisualStyleState { get; set; }
}
![Page 43: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/43.jpg)
43
ApplicationMethods
public sealed class Application
{ // Methods
public static void AddMessageFilter(IMessageFilter value);
public static void DoEvents();
public static void EnableVisualStyles();
public static void Exit();
public static void ExitThread();
public static bool FilterMessage(ref Message message);
public static ApartmentState OleRequired();
public static void RaiseIdle(EventArgs e);
public static void RegisterMessageLoop(MessageLoopCallback callback);
public static void RemoveMessageFilter(IMessageFilter value);
public static void Restart();
public static void Run();
public static void Run(ApplicationContext context);
public static void Run(Form mainForm);
public static void UnregisterMessageLoop();
public static void SetCompatibleTextRenderingDefault(bool defaultValue);
}
![Page 44: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/44.jpg)
44
ApplicationEvents
public sealed class Application
{
// Events
public static event EventHandler ApplicationExit;
public static event EventHandler EnterThreadModal;
public static event EventHandler Idle;
public static event EventHandler LeaveThreadModal;
public static event ThreadExceptionEventHandler ThreadException;
public static event EventHandler ThreadExit;
}
![Page 45: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/45.jpg)
45
Application
Một số phương thức thông dụng
• Run(Form) bắt đầu message loop của ứng dụng
• Exit() dừng message loop
• DoEvents() xử lý các message trong khi chương
trình đang trong vòng lặp
• EnableVisualStyles() các control sẽ vẽ với kiểu
visual nếu control và hệ điều hành hổ trợ
• Restart() dừng ứng dụng và Tự động restart lại
![Page 46: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/46.jpg)
46
Application
Một số property thông dụng
• ExecutablePath Đường dẫn đến file .exe
• StartupPath Đường dẫn đến thư mục chứa
file .exe
• UseWaitCursor Hiện cursor dạng Wait
Event thông dụng
• Idle Xuất hiện khi ứng dụng hoàn thành việc
xử lý
![Page 47: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/47.jpg)
47
Application
Ví dụ
class Program
{
static void Main()
{
Application.Run(new MainForm);
}
}
class MainForm:Form
{
private Button btnClose;
public MainForm()
{
btnClose = new Button();
…
btnClose.Click += btnClose_Click;
this.Controls.Add(btnClose);
}
private btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
![Page 48: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/48.jpg)
48
Application
Bài tập luyện tập
• Tìm đường dẫn đến file .exe
• Tìm đường dẫn đến thư mục chứa file .exe
• Shutdown ứng dụng và Tự động restart lại
• Thực hiện những công việc khi ứng dụng rãnh rỗi
• Hiện Cursor dạng Wait khi ứng dụng đang bận
thực thi công việc
• Xử lý trường hợp một phương thức thực thi mất
nhiều thời gian
![Page 49: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/49.jpg)
.NET Component
![Page 50: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/50.jpg)
50
.NET ComponentKhái niệm
Component là một thành phần phần mềm
Lớp Component là lớp cơ sở của tất cả các
component
Namespace:
• System.ComponentModel
Assembly
• System (System.dll)
![Page 51: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/51.jpg)
51
.NET ComponentKhái niệm
Các component trong .NET gồm có các thành viên:
– Properties
– Methods
– Events
– [Các thành viên protected]
Các Component không hổ trợ tương tác với người
dùng bằng form giao diện tự nhiên
Nonvisual design surface
![Page 52: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/52.jpg)
52
.NET ComponentSơ đồ thừa kế
System.Object
System.MarshalByRefObject
System.ComponentModel.ComponentSystem.Data.Common.DbConnection
System.Diagnostics.PerformanceCounter
System.Diagnostics.Process
System.Timers.Timer
System.Windows.Forms.Control
System.Windows.Forms.ErrorProvider
System.Windows.Forms.HelpProvider
System.Windows.Forms.ImageList
System.Windows.Forms.Menu
System.Windows.Forms.NotifyIcon
System.Windows.Forms.StatusBarPanel
System.Windows.Forms.Timer
System.Windows.Forms.ToolBarButton
System.Windows.Forms.ToolStripItem
System.Windows.Forms.ToolStripPanelRow
System.Windows.Forms.ToolTip
![Page 53: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/53.jpg)
53
.NET ComponentLớp
public class Component : MarshalByRefObject, IComponent, IDisposable
{
// Events
public event EventHandler Disposed;
// Methods
public Component();
public void Dispose();
protected virtual void Dispose(bool disposing);
protected override void Finalize();
protected virtual object GetService(Type service);
public override string ToString();
// Properties
protected virtual bool CanRaiseEvents { get; }
public IContainer Container { get; }
protected bool DesignMode { get; }
protected EventHandlerList Events { get; }
public virtual ISite Site { get; set; }
}
![Page 54: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/54.jpg)
Control
![Page 55: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/55.jpg)
55
ControlKhái niệm
Control là Component có giao diện
• Giao diện ứng dụng gồm một tập các control, giúp người dùng tương tác với ứng dụng
• Cho phép hiển thị dữ liệu (output) hay cho phép nhập dữ liệu vào (input)
Lớp Control là lớp cơ sở cho các control
Namespace
• System.Windows.Forms
Assembly
• System.Windows.Forms (System.Windows.Forms.dll)
![Page 56: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/56.jpg)
56
ControlKhái niệm
Các control thông dụng
• Label
• Textbox
• Button
• Radio button
• Checkbox
• ListBox
• ComboBox
• Scrollbar
![Page 57: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/57.jpg)
57
ControlSơ đồ thừa kế
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ButtonBase
System.Windows.Forms.DataGrid
System.Windows.Forms.DataGridView
System.Windows.Forms.DateTimePicker
System.Windows.Forms.GroupBox
System.Windows.Forms.Label
System.Windows.Forms.ListControl
System.Windows.Forms.ListView
System.Windows.Forms.MdiClient
System.Windows.Forms.MonthCalendar
System.Windows.Forms.PictureBox
System.Windows.Forms.PrintPreviewControl
System.Windows.Forms.ProgressBar
System.Windows.Forms.ScrollableControl
System.Windows.Forms.ScrollBar
System.Windows.Forms.Splitter
System.Windows.Forms.StatusBar
System.Windows.Forms.TabControl
System.Windows.Forms.TextBoxBase
System.Windows.Forms.ToolBar
System.Windows.Forms.TrackBar
System.Windows.Forms.TreeView
System.Windows.Forms.WebBrowserBase
![Page 58: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/58.jpg)
58
ControlEvents
public class Control : Component, ...
{ // Events
// Property thay doi
public event EventHandler AutoSizeChanged;
public event EventHandler BackColorChangedanged
public event EventHandler BackgroundImageCyoutChanged
public event EventHandler BackgroundImageLayoutChanged;
public event EventHandler BindingContextChanged;
public event EventHandler CausesValidationChanged;
public event EventHandler ClientSizeChanged;
public event EventHandler ContextMenuChanged;
public event EventHandler ContextMenuStripChanged;
public event EventHandler CursorChanged;
public event EventHandler DockChanged;
public event EventHandler EnabledChanged;
}
![Page 59: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/59.jpg)
59
ControlEvents
public class Control : Component, ...
{ // Events
// Property thay doi
public event EventHandler FontChanged;
public event EventHandler ForeColorChanged;
public event EventHandler LocationChanged;
public event EventHandler RegionChanged;
public event EventHandler ParentChanged;
public event EventHandler RightToLeftChanged;
public event EventHandler SizeChanged;
public event EventHandler TabIndexChanged;
public event EventHandler TabStopChanged;
public event EventHandler TextChanged;
public event EventHandler VisibleChanged;
}
![Page 60: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/60.jpg)
60
ControlEvents
public class Control : Component, ...
{ // Events
// Focus
public event EventHandler Enter;
public event EventHandler Leave;
public event EventHandler GotFocus;
public event EventHandler LostFocus;
public event EventHandler Validated;
public event CancelEventHandler Validating;
}
![Page 61: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/61.jpg)
61
ControlEvents
public class Control : Component, ...
{ // Events
// Key
public event KeyEventHandler KeyDown;
public event KeyPressEventHandler KeyPress;
public event KeyEventHandler KeyUp;
public event PreviewKeyDownEventHandler PreviewKeyDown;
// Mouse
public event MouseEventHandler MouseDown;
public event EventHandler MouseEnter;
public event EventHandler MouseHover;
public event EventHandler MouseLeave;
public event MouseEventHandler MouseMove;
public event MouseEventHandler MouseUp;
public event MouseEventHandler MouseWheel;
}
![Page 62: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/62.jpg)
62
ControlEvents
public class Control : Component, ...
{ // Events
// Appearance
public event InvalidateEventHandler Invalidated;
public event PaintEventHandler Paint;
// Layout
public event LayoutEventHandler Layout;
public event EventHandler MarginChanged;
public event EventHandler Move;
public event EventHandler PaddingChanged;
public event EventHandler Resize;
// DragDrop
public event DragEventHandler DragDrop;
public event DragEventHandler DragEnter;
public event EventHandler DragLeave;
public event DragEventHandler DragOver;
public event GiveFeedbackEventHandler GiveFeedback;
public event QueryContinueDragEventHandler QueryContinueDrag;
}
![Page 63: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/63.jpg)
63
ControlEvents
public class Control : Component, ...
{ // Events
// Behavior
public event UICuesEventHandler ChangeUICues;
public event ControlEventHandler ControlAdded;
public event ControlEventHandler ControlRemoved;
public event HelpEventHandler HelpRequested;
public event EventHandler ImeModeChanged;
public event QueryAccessibilityHelpEventHandler
QueryAccessibilityHelp;
public event EventHandler StyleChanged;
public event EventHandler SystemColorsChanged;
}
![Page 64: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/64.jpg)
64
ControlEvents
public class Control : Component, ...
{ // Events
// Action
public event EventHandler Click;
public event EventHandler DoubleClick;
public event EventHandler MouseCaptureChanged;
public event MouseEventHandler MouseClick;
public event MouseEventHandler MouseDoubleClick;
// Private
public event EventHandler HandleCreated;
public event EventHandler HandleDestroyed;
}
![Page 65: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/65.jpg)
65
ControlMethods
public class Control : Component, ...
{ // Mot so Methods
public void Show();
public void Hide();
public bool Focus();
public Graphics CreateGraphics();
public void DrawToBitmap(Bitmap bitmap, Rectangle targetBounds);
public void BringToFront();
public void SendToBack();
public Point PointToClient(Point p);
public Point PointToScreen(Point p);
public Rectangle RectangleToClient(Rectangle r);
public Rectangle RectangleToScreen(Rectangle r);
protected virtual void WndProc(ref Message m);
…
protected virtual void OnClick(EventArgs e)
…
}
![Page 66: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/66.jpg)
66
ControlProperties
public class Control : Component, ...
{ // Mot so properties
// Layout
public virtual Anchor { get; set; }
public virtual DockStyle Dock { get; set; }
public virtual bool AutoSize { get; set; }
public Rectangle ClientRectangle { get; }
public Size ClientSize { get; set; }
public int Height { get; set; }
// Appearance
public virtual Color BackColor { get; set; }
public virtual Image BackgroundImage { get; set; }
public virtual Cursor Cursor { get; set; }
public virtual Font Font {get; set; }
public virtual Color ForeColor { get; set; }
…
}
![Page 67: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/67.jpg)
Lớp MessageBox
![Page 68: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/68.jpg)
68
Lớp MessageBox
Message Box hiện một thông báo hay một hướng dẫn cho user
Lớp MessageBox chỉ chứa một phương thức tĩnh duy nhất: Show(…)
DialogResult Show(string text, string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton,
MessageBoxOptions options);
Namespace
• System.Windows.Forms
Assembly
• System.Windows.Forms (System.Windows.Forms.dll)
![Page 69: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/69.jpg)
69
Lớp MessageBox
Các Enumerations
• MessageBoxButtons
• MessageBoxIcon
• MessageBoxDefaultButton
• MessageBoxOptions
• DialogResult
public enum MessageBoxButtons
{
OK,
OKCancel,
AbortRetryIgnore,
YesNoCancel,
YesNo,
RetryCancel
}
public enum MessageBoxIcon
{
Asterisk = 0x40,
Error = 0x10,
Exclamation = 0x30,
Hand = 0x10,
Information = 0x40,
None = 0,
Question = 0x20,
Stop = 0x10,
Warning = 0x30
}
![Page 70: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/70.jpg)
70
Lớp MessageBox
public enum MessageBoxDefaultButton
{
Button1 = 0,
Button2 = 0x100,
Button3 = 0x200
}
public enum MessageBoxOptions
{
DefaultDesktopOnly = 0x20000,
RightAlign = 0x80000,
RtlReading = 0x100000,
ServiceNotification = 0x200000
}
![Page 71: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/71.jpg)
71
Lớp MessageBox
public enum DialogResult
{
None,
OK,
Cancel,
Abort,
Retry,
Ignore,
Yes,
No
}
![Page 72: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/72.jpg)
Lớp Form
![Page 73: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/73.jpg)
73
Lớp FormKhái niệm
Lớp Form thể hiện một cửa sổ (window) hay một
dialog box tạo nên giao diện của ứng dụng
Thông thường tạo custom form bằng cách thừa kế từ
lớp Form
Namespace
• System.Windows.Form
Assembly:
• System.Windows.Form (System.Windows.Form.dll)
![Page 74: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/74.jpg)
74
Lớp FormSơ đồ thừa kế
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ScrollableControl
System.Windows.Forms.ContainerControl
System.Windows.Forms.Form
![Page 75: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/75.jpg)
75
Lớp FormSơ đồ thừa kế
public class Form : ContainerControl
{ // Methods
public void Activate();
public void AddOwnedForm(Form ownedForm);
public void Close();
public void LayoutMdi(MdiLayout value);
// Properties
public bool ShowIcon { get; set; }
public bool ShowInTaskbar { get; set; }
public Size Size { get; set; }
public FormStartPosition StartPosition { get; set; }
// Events
public event EventHandler Closed;
public event CancelEventHandler Closing;
public event EventHandler Deactivate;
// Mics
protected override void DefWndProc(ref Message m);
protected override void WndProc(ref Message m);
protected override void Dispose(bool disposing);
…
}
![Page 76: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/76.jpg)
76
Lớp FormProperties
![Page 77: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/77.jpg)
77
Lớp FormProperties
![Page 78: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/78.jpg)
78
Lớp FormEvents
![Page 79: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/79.jpg)
79
Lớp FormEvents
![Page 80: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/80.jpg)
80
Lớp FormMethods
Một số phương thức thông dụng• Show(), ShowDialog(), Hide(), Close()
• CenterToScreen(), DrawToBitmap(),
Invalidate()
• CreateGraphic();
Một số property thông dụng khác (không có trong Design)
• MdiParent, MdiChilden
• DialogResult
• Controls
• Modal
class MyForm : Form
{
public MyForm()
{
this.ShowInTaskbar = false;
this.Location = new Point(10, 10);
this.Size = new Size(100, 100);
}
}
![Page 81: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/81.jpg)
81
Lớp FormChu trình đời sống của form
Move
Load
VisibleChanged
Activated
Shown
Paint
Deactivate
Closing
Closed
![Page 82: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/82.jpg)
82
Lớp FormMột số vấn đề liên qua đến Form
Tạo custom form
• Click phải lên project trong solution
• Chọn Add Windows Forms
• Cho custom form thừa kế từ lớp Form
Hiện custom form
• Modal Form
– formName.ShowDialog();
• Modeless Form
– formName.Show ();
![Page 83: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/83.jpg)
83
Lớp FormMột số vấn đề liên qua đến Form
Nút OK và Cancel
• Trước khi có thể nhận dữ liệu từ Form đang
hiển thị, phương thức ShowDialog() phải trở
về, có nghĩa là form phải đóng lại
• Xác định button nào đã nhấn (OK hay Cancel,
…) chúng ta dùng property DialogResult
![Page 84: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/84.jpg)
84
Lớp FormMột số vấn đề liên qua đến Form
Nút OK và Cancel
• Cách 1
private void btnOK_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
![Page 85: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/85.jpg)
85
Lớp FormMột số vấn đề liên qua đến Form
Nút OK và Cancel
• Nhận xét
– Khi gọi phương thức Close() thì phương thức
ShowDialog() mặc nhiên sẽ trả về
DialogResult.Cancel (cũng là giá trị của
property DialogResult)
– Chúng ta có thể trả về các giá trị khác của
enum DialogResultenum DialogResult {
Abort, Cancel, // kết quả mặc nhiên khi gọi Form.Close() Ignore, No, None, OK, Retry, Yes
}
![Page 86: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/86.jpg)
86
Lớp FormMột số vấn đề liên qua đến Form
Nút OK và Cancel
• Nhận xét
– Kiểm tra giá trị trả về từ phương thức
ShowDialog() là cách nhanh nhất đề kiểm tra
giá trị của thuộc tính DialogResult
– Hai đoạn mã sau là tương đương
dlg.ShowDialog();
DialogResult res = dlg.DialogResult;
if (res == DialogResult.OK)
{
}
if (dlg.ShowDialog() == DialogResult.OK)
{
}
![Page 87: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/87.jpg)
87
Lớp FormMột số vấn đề liên qua đến Form
Nút OK và Cancel
• Cách 2
private void btnOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
Chú ý:
-Nút OK không phải phím mặc nhiên
-Nút Cancel không được gọi khi phím ESC nhấn
-Nút Enter không được gọi khi phím Enter nhấn
-Khi gán thuộc tính DialogResult thì không cần gọi
phương thức Close()
![Page 88: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/88.jpg)
88
Lớp FormMột số vấn đề liên qua đến Form
Nút OK và Cancel
• Gán phím Enter/ESC cho nút OK/Cancel
– Dùng Designer thiết lập 2 thuộc tính của form:
AcceptButton và CancelButton
private void InitializeComponent()
{
…
this.AcceptButton = this.btnOK;
this.CancelButton = this.btnCancel;
…
}
![Page 89: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/89.jpg)
89
Lớp FormMột số vấn đề liên qua đến Form
Nút OK và Cancel
• Cách 3: (Dùng Designer)
– Thiết lập property DialogResult của nút OK:
DialogResult.OK
– Thiết lập property DialogResult của nút Cancel:
DialogResult.Cancel
– Trên form
Thiết lập property AcceptButton: btnOK
Thiết lập property CancelButton: btnCancel
![Page 90: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/90.jpg)
90
Lớp FormMột số vấn đề liên qua đến Form
Nút OK và Cancel
• Cách 3: (Dùng Designer)
private void InitializeComponent()
{
…
this.btnOK.DialogResult = DialogResult.OK;
this.btnCancel.DialogResult = DialogResult.Cancel;
…
this.AcceptButton = this.btnOK;
this.CancelButton = this.btnCancel;
…
}
![Page 91: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/91.jpg)
91
Lớp FormMột số vấn đề liên qua đến Form
Thiết lập giá các trị tùy theo Form là Modal Form hay Modeless Form
• Tùy thuộc việc dùng Show() hay ShowDialog(), property Modal sẽ có giá trị true hay false
• Cách các form được hiển thị không được biết khi form được tạo, cho nên giá trị Modal không thể xác định trong constructor mà phải xác định trong sự kiện load hay các sự kiện sau đó.
![Page 92: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/92.jpg)
92
Lớp FormMột số vấn đề liên qua đến Form
private void Form_Load(object sender, EventArgs e)
{
if (this.Modal)
{
// Khởi tạo giá trị khi là form Modal
this.FormBorderStyle = FormBorderStyle.FixedDialog;
}
else
{
// Khởi tạo giá trị khi là form Modeless
this.FormBorderStyle = FormBorderStyle.Sizable;
}
}
![Page 93: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/93.jpg)
93
Lớp FormMột số vấn đề liên qua đến Form
Trao đổi dữ liệu giữa các Form
• Với Modal Form
![Page 94: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/94.jpg)
94
Lớp FormMột số vấn đề liên qua đến Form
Trao đổi dữ liệu giữa các Form
• Với Modeless Form
![Page 95: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/95.jpg)
95
Lớp FormMột số vấn đề liên qua đến Form
Kiểm tra có muốn đóng Form hay không
• Xử lý sự kiện Closing
• Nếu không muốn đóng form: e.Cancel = true
Thứ tự active của các control trên form
(TabIndex)
• View Tab Order
![Page 96: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/96.jpg)
96
Lớp Form
Bài tập luyện tập
• Hãy hiện ra một messagebox để xác nhận
user muốn đóng form
• Hiện form ở giữa màn hình
• Không cho user thay đổi kích thước form
• Không cho form hiện trong taskbar
• Hiện form phủ cả màn hình (bao gồm taskbar)
• Tạo form kiểu splash screen
• Thay đổi icon của form
![Page 97: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/97.jpg)
Bố cục các controls trên
Form
![Page 98: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/98.jpg)
98
Bố cục các controls trên form
Anchor: Chỉ ra các cạnh của container để biết control sẽ
thay đổi kích thước như thế nào
khi cha nó thay đổi kích thước
• Các cạnh của container
– Left, Top, Right, Bottom
Dock: Chỉ ra các cạnh của control
sẽ bám vào container
• Left, Top, Right, Bottom, Fill
![Page 99: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/99.jpg)
99
Bố cục các controls trên form
Lớp Splitter: Một splitter control cho phép
thay đổi kích thước của các control đã dock
Thứ tự active của các control trên form
(TabIndex)
• View Tab Order
![Page 100: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/100.jpg)
Các control thông dụng
![Page 101: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/101.jpg)
101
Các control thông dụng
![Page 102: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/102.jpg)
102
Text Control
![Page 103: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/103.jpg)
103
Label
Label control dùng để cung cấp chuỗi mô tả cho control
Một số property thông dụng
• Text, TextAlign Image, ImageAlign, Visible
• BackColor, ForeColor
• Font
Một số phương thức thông dụng
• Hide(), Show()
Một số event thông dụng
• Paint
![Page 104: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/104.jpg)
104
TextBox
TextBox control dùng để nhập chuỗi vào ứng dụng• Cho phép nhập nhiều dòng
• Tạo mặt nạ để nhập password
Một số property thông dụng• Text, CharacterCasting, ReadOnly, PasswordChar (Multiline=false),
MaxLength
• Multiline, ScrollBars, WordWrap, Lines[], AcceptTab, AcceptReturn
Một số phương thức thông dụng • Clear(), Cut(), Paste(), Copy(), Undo(), Select(), SelectAll(),
DeselectAll(), ProcessCmdKey()
Một số event thông dụng• Click, Enter, Leave,TextChanged, MultilineChanged, KeyPress
![Page 105: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/105.jpg)
105
TextBox
Auto Completion Text• Controls: TextBox và ComboBox
• AutoCompleteSource– RecentlyUsedList
– HistoryList
– AllUrl = RecentlyUsedList + HistoryList
– ListItems
• …
• AutoCompleteMode– Append
– Suggest
– SuggestAppend
![Page 106: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/106.jpg)
106
TextBox
Auto Completion Text
• Custom Source
– AutoCompleteCustomSource: danh sách data
– AutoCompleteSource: CustomSource
• Viết code
– AutoCompleteStringCollection data = new
AutoCompleteStringCollection();
data.Add(“Le");
data.Add(“Tran");
data.Add(“Nguyen");
cb.AutoCompleteCustomSource = data;
![Page 107: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/107.jpg)
107
TextBox
Bài tập luyện tập
• Hãy ngăn cản
– menu context khi click phải lên textbox
– Phím Ctrl+V
• Textbox tự động chuyển thành chữ hoa hay
chữ thường khi nhập liệu
• Dùng Textbox để nhập password
• Hãy giới hạn số ký tự trong Textbox
• Hãy focus đến control tiếp theo khi nhấn enter
![Page 108: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/108.jpg)
108
LinkLabel
LinkLabel control: Là một label cho phép chứa các hyperlink
Một số property thông dụng
• LinkBehaviour , LinkVisited
• VisitedLinkColor, LinkColor,ActiveLinkColor, DisabledLinkColor
• LinkArea (một liên kết), Links (nhiều liên kết)
Một số event thông dụng
• LinkClicked
![Page 109: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/109.jpg)
109
LinkLabel
Bài tập luyện tập
• Hãy tạo form About giới thiệu về chương trình,
có các hyperlink trên đó
![Page 110: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/110.jpg)
110
Status Bar
Công dụng:
• Hiện tình trạng hiện tại của ứng dụng
![Page 111: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/111.jpg)
111
Button
Button control: cho phép user click lên nó để thực hiện một hành động
Một số property thông dụng
• Text, Image, TextAlign, ImageAlign, DialogResult
Một số phương thức thông dụng
• PerformClick()
Một số event thông dụng
• Click, MouseEnter, MouseLeave
![Page 112: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/112.jpg)
112
Button
Các cách chọn Button control
• Dùng mouse click button
• Gọi sự kiện Click của button
• Focus button (tab) sau đó nhấn SPACEBAR hay
ENTER
• Nhấn Access Key (ALT + Ký tự gạch dưới)
• Nếu button là “access” button của form nhấn
ENTER
• Nếu button là “Cancel” button của form nhấn ESC
• Gọi phương thức PerformClick()
![Page 113: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/113.jpg)
113
Button
Access Key cho control
• Access Key là ký tự gạch dưới trong text của
menu, menuitem, label của các controls như
button
• User có thể “click” vào control bằng cách nhấn
ALT + Access key
Tạo Access Key
• buttonName.Text = “&Print”;
![Page 114: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/114.jpg)
114
Button
Bài tập luyện tập
• Thay đổi text của button
– Thay đổi text của một button và thay đổi chức
năng tương ứng
• Hai phương thức vào một sự kiện
• Tạo Button động
– Cho nhập thông tin (vi trí, text, …) rồi hiện
button trên form
• Popup Text cho button (HelpProvider)
![Page 115: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/115.jpg)
115
Button
Bài tập luyện tập
• Làm thế nào để click button bằng lập trình
• Làm thế nào phát sinh sự kiện click của button
trong những khoảng thời gian khi mouse đang
click lên button được (giống scrollbar button)
• Làm thế nào đặt ảnh hay icon lên button
![Page 116: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/116.jpg)
CheckBox và RadioButton
![Page 117: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/117.jpg)
117
CheckBox
Properties
• Text – chữ hiện kế bên checkbox
• Checked
– Checked = true –check box đã được check
– Checked = false –check box chưa được check
Events
• CheckedChanged – sự kiện phát sinh khi
thay đổi trạng thái check
![Page 118: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/118.jpg)
118
CheckBox
Các dạng khác của CheckBox
• ThreeState = true: CheckBox có 3 trạng thái
• Appearance =Button: CheckBox là một button
CheckBox 3 trạng thái
• Dùng property CheckState để kiểm tra nó có là một trong 3 trạng thái
– Checked
– Unchecked
– Indeterminate
![Page 119: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/119.jpg)
119
Radio Buttons
Radio buttons tương tự checkbox, tuy nhiên
• Các button trong cùng nhóm chỉ có một button
được check tại một thời điểm
• Một nhóm: Các radio button được đặt trong
cùng container – thường là panel hay group
box
![Page 120: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/120.jpg)
120
Radio Buttons
Property Checked – Cho biết button có được
check hay không
Event CheckedChanged – Sự kiện phát sinh
khi check box được check hay không được
check
![Page 121: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/121.jpg)
List Controls
![Page 122: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/122.jpg)
122
List Controls
Có 3 list controls
• ListBox
• CheckedListBox
• ComboBox
Lớp cơ sở: thừa kế từ
lớp trừu tượng ListControl
![Page 123: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/123.jpg)
123
List Controls
Properties
• Items – danh sách các item có trong list
• Sorted = true: tự động sắp xếp theo từ điển
• SelectedIndex, SelectedIndices, SelectedItem,
SelectedItems và Text – cung cấp các cách khác nhau
để truy cập các mục đã chọn
Methods
• int FindString(string s) – tìm chuỗi s có trong list hay
không
Events
• SelectedIndexChanged
![Page 124: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/124.jpg)
124
List Controls
Thêm item vào item list
• listName.Items.Add(“”);
• listName.Items.AddRange(String []);
Chèn item vào item list
• listName.Items.Insert(index, data);
Xóa:
• listName.Items.Remove(data);
• listName.Items.RemoveAt(index);
• listName.Items.Clear();
Tìm kiếm
• listName.Items.Indexof(object obj);
![Page 125: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/125.jpg)
125
ListBox
ListBox control cho phép hiển thị danh sách các mục để user chọn
Properties• MultiColumn – chia list thành các cột khi dữ liệu không
hiển thị hết một lúc trên list
• SelectionMode – quy định chế độ chọn các mục trong list
• TopIndex – Cuộn ListBox đến TopIndex
Methods• void ClearSelected()
• bool GetSelected(int index)
• void SetSelected(int index, bool value)
![Page 126: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/126.jpg)
126
ComboBox=LISTBOX+TEXTBOX
ComboBox control cho phép hiển thị danh sách các
mục dạng drop down để user chọn
Properties
• MaxDropDownItems, DropDownStyle
Methods
• void Select(int start, int length)
• void SelectAll()
Events
• DropDown
![Page 127: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/127.jpg)
127
CheckedListBox
CheckedListBox control – Hiện danh sách các
checkbox
Properties
• CheckedItems và CheckedIndices – truy cập mục đã
chọn
• MultiColumn – chia list thành các cột khi dữ liệu không
hiển thị hết một lúc trên list
• SelectionMode – quy định chế độ chọn các mục trong
list
• TopIndex – Cuộn CheckedListBox đến TopIndex
![Page 128: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/128.jpg)
128
CheckedListBox
Methods
• void ClearSelected()
• bool GetSelected(int index)
• void SetSelected(int index, bool value)
• bool GetItemChecked(int index)
• CheckState GetItemCheckState(int index)
• void SetItemChecked(int index, bool value)
• void SetItemCheckedState(int index, CheckState value)
Events
• ItemCheck
![Page 130: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/130.jpg)
130
List Controls
Bài tập luyện tập
• Làm thế nào check hay unchecked tất cả các
mục trong CheckListBox
• Làm thế nào mặc nhiên chọn item đầu tiên
trong ComboBox
• Cuộn Listbox hay ChecklistBox sao cho thấy
được mục vừa mới thêm gần nhất
![Page 131: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/131.jpg)
131
Data binding
Có thể kết nối control với dữ liệu thông qua property DataSource với
• DataSet, Mảng (tĩnh và động)
Ví dụ:string[] cityChoices = {
"Seattle", "New York", "Signapore", "Montreal"};
lstCity.DataSource = cityChoices;
![Page 132: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/132.jpg)
132
Data binding
Items trong list control là một tập các object.
Chú ý: Để hiển thị item trong trong list, list
control gọi phương thức ToString()
Có thể kết hợp các đối tượng thuộc các
lớp tự tạo với list control
![Page 133: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/133.jpg)
ImageList
![Page 134: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/134.jpg)
134
ImageList
ImageList là một kiểu collection đặc biệt chứa các ảnh có kích thước và độ sâu màu được xác định trước.
Các control khác nếu có hổ trợ dùng ImageList thì dùng các ảnh trong ImageList thông qua chỉ mục.• ListView, TreeView, ToolBar, Button, …
Properties• Images – collection các ảnh
• ImageSize – kích thước của các ảnh
• TransparentColor – Định nghĩa màu trong suốt
Method• Draw(…) Vẽ ảnh lên bề mặt GDI+
![Page 135: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/135.jpg)
135
ImageList
Thêm/xóa/sắp xếp các ảnh trong ImageList
• Cách 1: Dùng Designer để thêm các ảnh (bmp, jpg, ico, …) vào
– Click (…) kế bên thuộc tính Image trong Properties
![Page 136: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/136.jpg)
136
ImageList
Thêm/xóa/sắp xếp các ảnh trong ImageList
• Chú ý: các file ảnh được thêm vào resource
file khi dùng designer
• Cách 2: Nạp các file ảnh từ file vào ImageList
![Page 137: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/137.jpg)
137
ImageList
ImageList iconImages = new ImageList();
// Cấu hình ImageList
iconImages.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
iconImages.ImageSize = new System.Drawing.Size(16, 16);
// Lấy các file trong thư mục hiện tại
string[] iconFiles = Directory.GetFiles(Application.StartupPath,
"*.ico");
// Thêm vào ImageList
foreach (string iconFile in iconFiles)
{
Icon newIcon = new Icon(iconFile);
iconImages.Images.Add(newIcon);
}
![Page 138: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/138.jpg)
ListView và TreeView
![Page 139: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/139.jpg)
139
ListView
![Page 140: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/140.jpg)
140
ListView
Listview là control dùng để trình bày danh sách các mục mà có thể trình bày bằng 1 trong 4 cách khác nhau
• Các item trong ListView là một đối tượng ListViewItem
![Page 141: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/141.jpg)
141
ListView
![Page 142: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/142.jpg)
142
ListView
![Page 143: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/143.jpg)
143
ListView
Properties
• Items – Một collection các đối tượng ListViewItem được hiển thị trong ListView
• Columns – Một collection các đối tượng ColumnHeader được dùng trong Details View
• SelectedItems, SelectedIndices
• LargeImageList = imageList object
• SmallImageList = imageList object
• MultiSelect = bool
• View: LargeIcon, SmallIcon, List, Details, Tile
• Sorting = Acending/Descending
• FullRowSelect = bool
![Page 144: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/144.jpg)
144
ListView
Methods
• ListViewItem GetItemAt(int x, int y) – lấy item tại (x,y)
• void Clear() – Xóa các item và các cột
Events
• ColumnClick, ItemCheck
![Page 145: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/145.jpg)
145
ListView
Thêm một item đơn giản
• ListViewItem item = new ListViewItem(string);
• listView.Items.Add(item);
Thêm item có ảnh
• Tạo imagelist
• Gán imagelist cho LargeImageList/SmallImageList
• item.ImageIndex=chỉ số hình trong imagelist
Thêm item có nhiều subitem
• string[] data = {string1, string2, …};
• ListViewItem item = new ListViewItem(data);
• listView.Items.Add(item);
![Page 146: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/146.jpg)
146
ListView
Nhận dữ liệu trên ListView
• ListViewItem item = lstSignature.SelectedItems[0];
• string sub1 = item.SubItems[0].Text;
• string sub2 = item.SubItems[1].Text;
Tạo detail
• View: Detail
• GridLines: true
• Columns: Thêm các column
![Page 147: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/147.jpg)
147
ListView
![Page 148: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/148.jpg)
148
ListView
Thay đổi View
• listView.View = View.SmallIcon
– SmallIcon
– LargeIcon
– Details
– List
– Tile
• Chú ý: Details phải có column
![Page 149: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/149.jpg)
149
ListView
Một số properties khác
• AllowColumnReorder
• AutoArrange
• CheckBoxes, CheckedIndices, CheckedItems
• GridLines
Một số methods khác
• BeginUpdate(), EndUpdate()
![Page 150: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/150.jpg)
150
TreeView
TreeView là một collection các node dạng
phân cấp
Các node trong cây thuộc lớp TreeNode, có
thể được lồng không giới hạn.
![Page 151: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/151.jpg)
151
TreeView
Properties
• Nodes
• SelectedNode, ShowRootLines , ImageIndex,
ShowPlusMinus
Methods
• ExpandAll(), GetNodeAt(), GetNodeCount(),
CollapseAll()
Events
• AfterCheck, AfterCollapse, BeforeCheck,
BeforeCollapse, AfterSelect, AfterExpand,
BeforeSelect, BeforeExpand
![Page 152: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/152.jpg)
152
TreeView
Thêm một node
• Cách 1: tree.Nodes.Add(string);
• Cách 2:
– TreeNode newNode = new TreeNode();
– newNode.Text = “…";
– newNode.Tag = “…";
– tree.Nodes.Add(newNode);
![Page 153: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/153.jpg)
153
TreeView
Cấu trúc TreeView
• Các nodes có thể lồng nhiều cấp
• Thêm node con:
– Tìm node cha
– Thêm node con vào node cha
• Ví dụ 1 TreeNode node;node = tree.Nodes.Add(“cha”);node.Nodes.Add(“con”);
![Page 154: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/154.jpg)
154
TreeView
Cấu trúc TreeView
• Ví dụ 2:
TreeNode[] nodes = new TreeNode[n];
nodes[0] = new TreeNode(“cha”);
nodes[0].Nodes.Add(“con”);
…
tree.Nodes.AddRange(nodes);
• Ví dụ 3:
tree.BeginUpdate();
…
tree.EndUpdate();
![Page 155: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/155.jpg)
155
TreeView
Duyệt các phần tử của TreeView: 2 kỹ thuật
cơ bản
• Dùng kỹ thuật đệ quy
void XuLyCacNode(TreeNodeCollection nodes)
{
foreach (TreeNode node in nodes)
{
// Xử lý node
XuLyCacNode (node.Nodes)
}
}
![Page 156: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/156.jpg)
156
TreeView
Duyệt các phần tử của TreeView: 2 kỹ thuật
cơ bản
• Dùng các properties của node
– Parent
– FirstNode
– LastNode
– PrevNode
– NextNode
![Page 157: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/157.jpg)
157
TreeView
Duyệt các phần tử của TreeView: 2 kỹ thuật cơ bản
• Dùng các properties của node
void XuLyCacNode(TreeNode node)
{
do
{
// Xử lý node
if (node.Nodes.Count>0)
XuLyCacNode(node.FirstNode);
node = node.NextNode;
} while (node != null);
}
![Page 158: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/158.jpg)
158
TreeView
Thao tác trên treeview
• Xóa node: Remove()
– Tham chiếu đến node cần xóa: Xóa node
– Tìm vị trí node: xóa node tại vị trí đó
• Xóa các node: Clear()
• Tìm kiếm vị trí: IndexOf()
![Page 159: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/159.jpg)
159
TreeView
Node ảnh
• Ảnh cho toàn TreeView
– tree.ImageList = imageList;
– tree.ImageIndex = vị trí
• Ảnh cho từng node
– tree.ImageList = imageList;
– node.ImageIndex = vị trí
– node.SelectedImageIndex = vị trí
![Page 160: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/160.jpg)
160
TreeView
Expand và collaps
• Tree
– ExpandAll()
– CollapseAll()
• Node
– Collapse(), Expand(), ExpandAll(), Toggle().
![Page 161: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/161.jpg)
161
TreeView
Một số properties khác của TreeView
• CheckBoxes = bool
• FullRowSelect = bool
• Indent = number
• ShowLines, ShowPlusMinus, và ShowRootLines
• Sorted = bool
Một số properties khác của TreeNode
• Checked = bool
• IsSelected = bool
• IsExpanded = bool
![Page 162: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/162.jpg)
NumericUpDown, DomainUpDown, TrackBar
và HScrollBar, VScrollBar
![Page 163: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/163.jpg)
163
NumericUpDown và DomainUpDown
![Page 164: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/164.jpg)
164
NumericUpDown
NumericUpDown control cho phép chọn một số (nguyên/thực) trong miền giới hạn
Properties
• Maximum, Minimum – Giá trị lớn nhất và nhỏ nhất có thể chọn
• Increment – Bước nhảy mỗi lần click
• DecimalPlaces – Số chữ số lẻ
• Value – Giá trị hiện tại của control
Methods
• void DownButton()
• void UpButton()
Events
• ValueChanged – Sự kiện xảy ra khi value thay đổi
![Page 165: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/165.jpg)
165
DomainUpDown
DomainUpDown cung cấp một danh sách các option (tương tự như List control), nhưng người dùng chỉ có thể chọn các item thông qua up/down button
Properties• Text
• SelectedIndex
• SelectedItem
• Items
Methods• void DownButton()
• void UpButton()
![Page 166: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/166.jpg)
166
DomainUpDown
Thêm chuỗi vào DomainUpDown
• // Add Items
udCity.Items.Add("Tokyo");
udCity.Items.Add("Montreal");
udCity.Items.Add("New York");
Chọn item đầu tiên
• udCity.SelectedIndex = 0;
![Page 167: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/167.jpg)
167
TrackBar
TrackBar cho phép người dùng chọn giá trị số nguyên một cách đồ họa dùng Tab across
Properties• Maximum, Minimum
• Value
• TickFrequency
• SmallChange, LargeChange
Events• ValueChanged
• Scroll
![Page 168: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/168.jpg)
168
HScrollBar và VScrollBar
Properties• Value: int
• Maximun: int
• Minimum: int
• SmallChange: int
• LargeChange: int
• RightToLeft: RightToLeft– Yes
– No
Events• Scroll, ValueChanged
![Page 169: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/169.jpg)
169
HScrollBar và VScrollBar
Bài tập luyện tập
• Viết chương trình chỉnh sửa màu có giao diện
như sau
![Page 170: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/170.jpg)
ProgressBar
![Page 171: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/171.jpg)
171
ProgressBar
![Page 172: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/172.jpg)
172
ProgressBar
ProgressBar cho phép hiển thị tiến trình hoạt động của
chương trình
Properties
• Step – Bước nhảy mỗi lần
• Maximum, Minimum – Giá trị lớn nhất và nhỏ nhất
• Value – Giá trị hiện tại
Phương thức
• void PerformStep() – Tăng một lượng Step
• void Increment(int value) – Tăng một lượng value
![Page 173: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/173.jpg)
PictureBox và Bitmap
![Page 174: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/174.jpg)
174
PictureBox
![Page 175: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/175.jpg)
175
PictureBox
Properties
• Image – Lưu trữ hình đang hiện trên PictureBox
– *.gif, *.jpg, *.jpeg, *.bmp, *.wmf, *.png, *.tiff
• SizeMode – Kiểu enum PictureBoxSizeMode
– AutoSize
– CenterImage
– Normal
– StretchSize
– Zoom
Events
• SizeModeChanged
![Page 176: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/176.jpg)
176
PictureBox
Nạp ảnh từ file
string path = “…”;
Bitmap bitmap = new Bitmap(path);
pic.Image = bimap;
pic.SizeMode=PictureBoxSizeMode.StretchImage;
![Page 177: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/177.jpg)
177
Bitmap
Image
Bitmap Metafile
![Page 178: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/178.jpg)
178
Bitmap
Bitmap bao bọc GDI+ bitmap: gồm dữ liệu pixel và các thuộc tính của bitmap
Properties• Width,Height, Size – chiều rộng, chiều cao ảnh (pixel)
Methods• void GetPixel(int x, int y)
• void SetPixel(int x, int y, Color color)
• void RotateFlip(RotateFlipType rotate);
• Save(string fileName)
• Save(string fileName, ImageFormat format)
![Page 179: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/179.jpg)
179
PictureBox và Bitmap
Bài tập luyện tập
• Viết chương trình xử lý ảnh cơ bản, gồm
những chức năng cơ bản sau
– Chọn file ảnh, Lưu ảnh
– Quay ảnh
– Hiện thị thông tin của ảnh
– Cách hiện thị ảnh (SizeMode)
– Lấy màu tại vị trí chuột trên ảnh
![Page 180: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/180.jpg)
MaskedTextBox và RichTextBox
![Page 181: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/181.jpg)
181
MaskedTextBox và RichTextBox
MaskedTextBox
RichTextBox
![Page 182: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/182.jpg)
182
MaskedTextBox
MaskedTextBox là mở rộng của TextBox
control bằng cách dùng một mặt nạ (Mask)
để hướng dẫn nhập dữ liệu đúng đắn
• Yêu cầu phải nhập hay Option
• Kiểu dữ liệu mong đợi tại vị trí cụ thể
• …
Chú ý: MaskedTextBox không hổ trợ Multiline
và Undo
![Page 183: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/183.jpg)
183
MaskedTextBox
Properties
• Mask: string
Methods
• void ResetText() – Thiết lập default text
• void Copy(), Cut(), Paste()
![Page 184: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/184.jpg)
184
MaskedTextBox
Mask
• 0 – phải nhập số ([0..9])
• 9 – số hay space (option)
• L – phải nhập ký tự chữ ([a-zA-Z])
• ? – ký tự chữ (option)
• & – phải nhập ký tự ascii
• C – nhập ký tự ascii (option)
• A – phải nhập Alphanumeric
• a – nhập Alphanumeric (option)
![Page 185: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/185.jpg)
185
MaskedTextBox
Mask
• . – thập phân
• , – phân cách phần ngàn
• : – phân cách thời gian
• / – phân cách ngày
• $ – ký hiệu tiền tệ
• < – chuyển sang chữ thường
• > – chuyển sang chữ HOA
• \ – Escape (\\ là \)
![Page 186: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/186.jpg)
186
MaskedTextBox
Ví dụ
• 00/00/0000 – ngày
• (999)-000-0000 – số điện thoại Mỹ
• $999,999.00 – tiền từ 0 đến 999999
![Page 187: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/187.jpg)
187
RichTextBox
Properties
• Text: string – Văn bản trong RichTextBox
• Rtf: string – Code của văn bản
• SelectionText, SelectionRtf: string – Một phần văn bản hay code của văn bản được chọn
• SelectionColor, SelectionBackColor : Color văn bản được chọn
• SelectionFont: Font – font của văn bản được chọn
• RedoActionName – Lấy tên action khi phương thức Redo() gọi
• UndoActionName – Lấy tên action khi phương thức Undo() gọi
• ZoomFactor – Mức zoom
![Page 188: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/188.jpg)
188
RichTextBox
Methods
• void LoadFile(string path)
• void SaveFile(string path)
• void Redo()
• void Undo()
• void Copy()
• void Cut()
• void Paste()
• void Clear() – Xóa tất cả các text
• void SelectAll() – Chọn tất cả các text
• int Find(string string str)
Events
• LinkClicked, SelectionChanged
![Page 189: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/189.jpg)
189
RichTextBox
Bài tập luyện tập
• Viết chuơng trình soạn thảo văn bản có định
dạng như MS.Word
![Page 190: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/190.jpg)
DateTimePicker, MonthCalender
và Timer
![Page 191: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/191.jpg)
191
DateTimePicker, MonthCalender và Timer
DateTimePicker
MonthCalender
Timer
![Page 192: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/192.jpg)
192
DateTimePicker và MonthCalender
Chọn một ngày Chọn một vùng ngày
![Page 193: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/193.jpg)
193
DateTimePicker
DateTimePicker cho phép user chọn một ngày
Properties
• Text, Value: Text trả về chuỗi ngày đã được định dạng,
Value trả về đối tượng DateTime
• Format: định dạng hiển thị
– long, short, time, custom
• CustomFormat:
– dd/mm/yyyy: Ngày/Tháng/Năm
– hh:mm:ss: Giờ:Phút:Giây
• MinDate, MaxDate : Chỉ ra vùng ngày user có thể chọn
![Page 194: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/194.jpg)
194
MonthCalender
MonthCalender cho phép user chọn một số ngày
trong tháng
Properties
• SelectionStart, SelectionEnd: trả về các ngày đã chọn
• SelectionRange: cấu trúc chứa SelectionStart và
SelectionEnd
• MaxDate, MinDate, MaxSelectionCount: ngày tối đa và
tối thiểu được chọn trong calender, và số ngày liên tục
có thể chọn tại một thời điểm
![Page 195: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/195.jpg)
195
Timer
Timer sinh ra sự kiện Tick theo định kỳ
Properties
• Interval: int – thời gian sự kiện xảy ra (tính bằng millisecond)
• Enabled: bool – Cho phép timer start hay stop
Methods
• void Start() – Start timer
• void Stop() – Stop timer
Events
• Tick
![Page 196: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/196.jpg)
Menu, Toolbar
và Statusbar
![Page 197: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/197.jpg)
197
Menu và Toolbar
![Page 198: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/198.jpg)
198
Menu
Một menu gồm 2 phần: Các mục menu (Item) và bộ chứa các mục menu (Container)
Container: Lớp MenuStrip• Items – Chứa các MenuItem/TextBox/ComboBox
• Visible – Ẩn hiện menu
• TextDirection – Hướng vẽ text trên item
• LayoutStyle – Cách hiển thị menu
Các mục menu: • Lớp ToolStripMenuItem
– Text
– Visible
– ShortcutKeys
– ShowShortcutKeys
– Checked
• Lớp ToolStripTextBox
• Lớp ToolStripComboBox
![Page 199: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/199.jpg)
199
Menu
Tự thiết kế menu
• HotKey: thêm ký tự & trước ký tự muốn làm hot key
Đánh tên
các mục menu
![Page 200: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/200.jpg)
200
Menu
Thêm menu chuẩn
• Click vào ký hiệu tạm giác Insert Standard
Items
![Page 201: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/201.jpg)
201
Menu
Tạo Shortcut key
• Chọn mục menu
• Vào properties thiết lập ShortcutKeys
![Page 202: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/202.jpg)
202
Menu
Các loại menu item
• MenuItem
• ComboBox
• TextBox
• Separator
Chú ý: Click mũi tên xuống tại nơi muốn tạo
menu item để chọn các loại menu item
![Page 203: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/203.jpg)
203
Menu
Xử lý sự kiện:
• Xử lý sự kiện cho từng mục menu item
– Click
– DoubleClick
– CheckedChanged
– CheckStateChanged
![Page 204: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/204.jpg)
204
Context menu
Context menu là menu ngữ cảnh xuất hiện tại các control khi click phải chuột lên control đó để trợ giúp user hiểu về control
Container• ContextMenuStrip
Các bước tạo Context menu• B1: Tạo ContextMenuStrip
• B2: Vào Properties của control muốn có menu ngữ cảnh: gán ContextMenuStrip bằng contextmenu đã tạo ở B1
• B3: Xử lý các sự kiện trên các menu item của Context menu
![Page 205: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/205.jpg)
205
ToolBar
ToolBar chứa tập các nút có những chức
năng khác nhau
Container: Lớp ToolStrip
Các item: Lớp
• ToolStripSplitButton
• ToolStripDropDownbutton
• ToolStripLabel
• ToolStripProgressBar
• ToolStripSeparator
• ToolStripComboBox
• ToolStripTextBox
![Page 206: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/206.jpg)
206
ToolBar
Các loại Item
![Page 207: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/207.jpg)
207
ToolBar
Các bước tạo toolbar
• B1: Tạo container: Kéo ToolStrip vào form
• B2: Tạo các item
– Chọn loại item
– Chọn hình
• B3: Xử lý các sự kiện cho các item
![Page 208: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/208.jpg)
208
StatusBar
Container: StatusStrip
Các loại item
![Page 209: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/209.jpg)
NotifyIcon và ToolTip
![Page 210: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/210.jpg)
210
NotifyIcon và ToolTip
NotifyIcon
ToolTip
![Page 211: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/211.jpg)
211
NotifyIcon
NotifyIcon dùng để tạo một icon trên System Tray
Properties• BalloonTipIcon: ToolTipIcon
• BalloonTipText: string
• BalloonTipTitle: string
• ContextMenuStrip
• Icon: Icon
• Text: string
Methods• void ShowBalloonTip(int timeout)
• void ShowBalloonTip(int timeout, string title, string text, ToolTipIcon icon)
![Page 212: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/212.jpg)
212
ToolTip
ToolTip một công cụ trợ giúp user hiểu rõ các
control trên form. ToolTip sẽ hiển thị một text
khi user rê chuột vào control
![Page 213: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/213.jpg)
213
ToolTip
Properties
• InitialDelay: int
• AutomaticDelay: int
• ShowAlways: bool
• BackColor, ForeColor: Color
• ToolTipTitle: string
• ToolTipIcon: ToolTipIcon.(Error, Info, None, Warning)
Events
• GetToolTip, SetToolTip, RemoveAll
![Page 214: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/214.jpg)
214
ToolTip
Các bước tạo tooltip
• B1: Kéo ToolTip vào form và thiết lập các
properties
• B2: Chọn control muốn có properties
• B3: Vào properties nhập dòng văn bản tại:
“ToolTip on têntooltip”
![Page 215: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/215.jpg)
Grouping Controls
![Page 216: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/216.jpg)
216
Grouping Controls
Các loại Grouping Controls
• Form
• Panel
• GroupBox
• Tab Control
![Page 217: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/217.jpg)
217
Form, Panel, GroupBox
Properties
• Enabled: bool
• Size: Size
• Visible: bool
Methods
• void Focus()
• void Refresh()
Events
• Enter
• Leave
• Paint
![Page 218: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/218.jpg)
218
GroupBox
Hiện border xung quanh nhóm các control
Property Text – gán Text label cho GroupBox
Control có thể được thêm vào
• Đặt control vào trong GroupBox khi thiết kế
• Viết code
![Page 219: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/219.jpg)
219
Panel
Panel giống GroupBox nhưng không có text label
Panel chứa một các control như GroupBox
• BorderStyle – get/set border style như
– BorderStyle.Fixed3D
– BorderStyle.FixedSingle
– BorderStyle.None
![Page 220: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/220.jpg)
220
TabControl
Properties
• TabPages
• SelectedTab
• ShowTooltips , Multiline, TabCount,
SelectedIndex
Events
• SelectedIndexChanged
![Page 221: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/221.jpg)
Các dialog thông dụng
![Page 222: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/222.jpg)
222
Các dialog thông dụng
Các loại Dialog
• Custom Dialog Boxes
• Common Dialog Boxes
– OpenFileDialog
– SaveFileDialog
– FontDialog
– ColorDialog
– FolderBrowserDialog
– PageSetUpDialog
– PrintPreviewDialog
– PrintDialog
![Page 223: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/223.jpg)
223
Các dialog thông dụng
Bốn bước sử dialog
• Cấu hình dialog (thiết lập các properties)
• Hiện dialog bằng phương thức ShowDialog()
• Xác định người dùng nhấn OK hay Apply (đựa
trên kiểu trả về DialogResult)
• Viết code thực hiện dựa trên hành động của
user trên dialog
![Page 224: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/224.jpg)
224
Các dialog thông dụng: OpenFileDialog
OpenFileDialog dùng để chọn một file để mở ra xử lý
Properties
• FileName: string
• FileNames: string[]
• Filter: string
![Page 225: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/225.jpg)
225
Các dialog thông dụng: OpenFileDialog
Properties
• DefaultExt: string
• AddExtension: bool
• Title: string
• IntialDirectory: string
• ShowReadOnly: bool
• ReadOnlyChecked: bool
• MultiSelect: bool
Methods
• DialogResult ShowDialog()
• Strean OpenFile()
![Page 226: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/226.jpg)
226
Các dialog thông dụng: SaveFileDialog
SaveFileDialog dùng để chọn/nhập một file
để lưu nội dung lên đĩa
![Page 227: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/227.jpg)
227
Các dialog thông dụng: SaveFileDialog
Properties
• OverwritePrompt: bool
• FileName: string
• FileNames: string[]
• Filter: string
• DefaultExt: string
• AddExtension: bool
• Title: string
• IntialDirectory: string
Methods
• Stream OpenFile()
• DialogResult ShowDialog()
![Page 228: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/228.jpg)
228
Các dialog thông dụng: FontDialog
FontDialog dùng để chọn font chữ có trên
máy
![Page 229: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/229.jpg)
229
Các dialog thông dụng: FontDialog
Properties
• Font
• Color
• MaxSize, MinSize
• ShowColor: bool
• ShowApply: bool
• ShowEffects: bool
Methods
• DialogResult ShowDialog()
• void Reset()
![Page 230: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/230.jpg)
230
Các dialog thông dụng: ColorDialog
ColorDialog dùng để chọn màu có trên máy
![Page 231: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/231.jpg)
231
Các dialog thông dụng: ColorDialog
Properties
• Color
• AllowFullOpen: bool
• FullOpen: bool
Methods
• DialogResult ShowDialog()
• void Reset()
![Page 232: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/232.jpg)
232
Các dialog thông dụng: FolderBrowserDialog
FolderBrowserDialog dùng để chọn thư mục
có trên máy
![Page 233: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/233.jpg)
233
Các dialog thông dụng: FolderBrowserDialog
Properties
• Description: string
• RootFolder: Environment.SpecialFolder
• SelectedPath: string
• ShowNewFolderButton: bool
Methods
• DialogResult ShowDialog()
• void Reset()
![Page 234: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/234.jpg)
MDI
![Page 235: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/235.jpg)
235
Tạo MDI
Tạo Form cha
• IsMdiContainer = true
Tạo form con
• Tạo object form con:
ChildFormName f = new ChildFormName()
• f.MdiParent = this;
• f.Show();
![Page 236: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/236.jpg)
236
Tìm các form trong MDI
Tìm form con đang active
• this.ActiveMdiChild
Tìm form cha
• this.MdiParent
Tìm các form con
• this.MdiChildren
![Page 237: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/237.jpg)
237
Đồng bộ dữ liệu
Chú ý:
• Form cha lưu các form con
• Mỗi form con lưu 1 form cha
Đồng bộ dữ liệu giữa các form con
• B1: Con gởi message nhờ form cha đồng bộ dữ liệu
• B2: Form cha duyệt các form con để đồng bộ dữ liệu
![Page 238: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/238.jpg)
238
Sắp xếp các form con
Tạo menu item chứa danh sách form con• Tạo menu item
• Thiết lập MdiWindowListItem của menu container = menu item trên
Sắp xếp các form con• this.LayoutMdi(MdiLayout.ArrangeIcons);
Cascade, TileHorizontal, TileVertical
Minimize các form con• Duyệt các form con: this.MdiChildren
• Thiết lập thuộc tính: WindowState = FormWindowState.Minimized
![Page 239: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/239.jpg)
Đọc file dữ liệu
văn bản
![Page 240: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/240.jpg)
240
File văn bản
Namespace
• System.IO
Class cơ bản
• StreamReader
• StreamWriter
![Page 241: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/241.jpg)
241
File văn bản
Đọc file văn bản
• StreamReader sr = new StreamReader(path);
• string data = sr.ReadLine();
• …
• sr.Close();
Một số lệnh hay dùng
• sr.EndOfStream
• string data = sr.ReadToEnd();
• int data = sr.Peek();
![Page 242: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/242.jpg)
242
File văn bản
Ghi file văn bản
• StreamWriter sw = new StreamWriter(path);
• sw.WriteLine(…);
• …
• sw.Close();
Một số lệnh hay dùng
• sw.Write(…);
![Page 243: Chương 3 Windows Forms Nội dung Lập trình C# trên Windows Tạo ứng dụng Windows Forms từ đầu Tạo ứng dụng Windows Forms từ Wizard Tổng quan các đối](https://reader031.vdocuments.pub/reader031/viewer/2022012323/5aaa44b97f8b9a86188dd743/html5/thumbnails/243.jpg)
243
Một số Bài tập lớn
Bài tập luyện tập: Mô phỏng các chương trình sau
• NotePad, WordPad
• Calculator
• Address Book
• Duyệt file
• Duyệt web
• Paint
• Chương trình xem ảnh
• Từ điển Anh – Việt
• Game Minesweeper