Common Dialog &
User Control
Nguyễn Văn Phong
Nội Dung
° Giới thiệu Common Dialog
a OpenFileDialog
a SaveFileDialog
a FontDialog
a ColorDialog
a MessageBox
a User Control
Nguyễn Văn Phong - 2010
Common Dialog
Các dialog thường được sử dụng
° Được warp thành các lớp trong FCL/BCL
OpenFileDialog
ColorDialog
Common
Dialog
SaveFileDialog FontDialog
PrintDìalog
Nguyễn Văn Phong - 2010
Common Dialog
Cách sử dụng
a 1. Kéo thả dialog vào form
■ vs sẽ tạo thể hiện chứa trong lớp form
a 2. Khai báo đối tượng và tạo thể hiện của lớp CD
■ VD: OpenFileDialog oFile = new OpenFileDialogO;
Oft
©
%
s
o
©
u
z Printing
^ Pointer
t3 i PageSetupDialog
PfintOialog
PrintDocument
Í PrintPreviewControl
PrintPreviewDialog
Ẽ Dialogs
Pointer
ColorDialog
J FolderBrowserDialog
a FontDialog
Jf] OpenFileDialog
y j SaveFileDialog
/
B Form2
Nguyễn Văn Phong - 2010
Nội Dung
a Giới thiệu Common Dialog
ü OpenFileDialog
a SaveFileDialog
a FontDialog
a ColorDialog
a MessageBox
a User Control
Nguyễn Văn Phong - 2010
OpenFileDialog
■ sử dụng để chọn file lưu trên đĩa
■ Cách sử dụng từ ToolBox
p— ^
Ẽ8 Fo rm l OS®
- Dialogs
^ Pointei
Drag & drop
•TỊ ColarDialog
J i FolderBrowserDialog
^ 3 FontDialog
r
¿1 OpenRleDialog f
■
ir l SaverleOiolog
■
a
■
■
■
a
■
■
a
ỹ l openFileDialogl
Windows Form Designer generated codq
Code phát
sinh tự động
xyz.Designer.cs
private System.Windows.Forms.OpenFileDialog openFileDialogl;
Nguyễn Văn Phong - 2010
H
OpenFileDialog
Code phát sinh của vs
xyz.Designer.cs
private void InitializeComponent()
(
this.openFileDialogl = new System.Windows.Forms.OpenFileDialog(),
this. SuspendLayout();
|
_
« i v
// openFileDialogl l a o t h e l u e n
//
this.openFileDialogl.FileName = "openFileDialogl";
//
// Forml
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F) ;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(262, 202);
this.Name = "Forml";
this.Text = "Forml";
this.Load += new System.EventHand
this.ResumeLayout(false);
#endregion
Hien thi Form
WogQ;
private System.Windows.Forms.OpenFileDialog openFileDialogl;
Nguyễn Văn Phong - 2010
■ Code tự viết - không dùng Toolbox
OpenFileDialog
// tạo thế hiện OpenFileDialog
OpenFileDialog OFileDlg = new OpenFileDialoq();
// thiêt lập các option
// cho phép chọn nhiêu file
OFileDlg.Multiselect = true;
// lọc chỉ mở những file c# source code
OFileDlg.F ilte r = "c# source code I * .cs
DialogResult result;
// hiển thị và lấy giá trị trả về
result = OFileDlg.ShowDialog();
// xử lý tiêp theo
Nguyễn Văn Phong - 2010
OpenFileDialog
Thuộc tính, phương thức
Thuôc tính
•
FileName Lấy tên file được chọn
FileNames Lấy tên tất cả các file được chọn
Filter
Xác đinh kiểu file cần mở
■
InitialDirectory Thư muc khởi tao
■ ■
Multiselect Cho phép chọn nhiêu file
Title Tiêu để của dialog
Phương thức
ShowDialog Hiển thị dialog
Sư kiên
• m
__S ' —
N g i y l 11 w i n ỉ ¡ ' o n g - 2 0 1 0
xuat men Khi user Kicn vao UK
Nội Dung
a Giới thiệu Common Dialog
a OpenFileDialog
° SaveFileDialog
a FontDialog
a ColorDialog
a MessageBox
a User Control
Nguyễn Văn Phong - 2010
SaveFileDialog
■ sử dụng để tạo file trên đĩa.
■ Cách sử dụng
a Sử dụng SaveFileDialog component trên Toolbox
■ Tương tự như OpenFileDialog!
a Tạo thể hiện của láp SaveFileDialog
// tạo thể hiện Save File Dialog
SaveFileDialog saveDlg = new SaveFileDialog0;
// save dưới dạng txt
saveDlg. Filter = "Text File I * . tx-t";
// hiển thị dialog và lấy giá trị trả về của dialg
DialogResult result = saveDlg.ShowDialog();
// nếu user chọn button "OK"
if (result == DialogResult.OK)
{
// TO DO
}
Nguyễn Văn Phong - 2010
SaveFileDialog
■ Demo: nhập van bản vào textbox, sau đó lưu
xuống file *.txt.
a Tạo ứng dụng dụng Windows Form có các
control
■ 1 label: caption của textbox
■ 1 textbox: chứa nội dung text do user nhập
■ 1 button: gọi SaveFileDialog và lưu file
■ 1 SaveFileDialog: khai báo dialog SaveFile.
Nguyễn Văn Phong - 2010
■ Thiết kê form
SaveFileDialog
a Save File Dialog
- Dialogs
Pointer
•71 ColorDialog
JF1 FolderBrowserDialog
FontDialog
n OpenFileDialog J
; y | SaveFileDialog
Nhập đoạn text
Drag & drop
Multiline=true
LUu file
ỹ l saveFileDialogl
Nguyễn Văn Phong - 2010
SaveFileDialog
Viết phần xử lý cho button “Lưu file”
System.1
b
21
22
23
24
25
26
27
28
29
3Q
// phẩn xử lý chữ button lưu
// lưu dạng text
saveFileDialogl.Filter = "Text File(*.txt) I *.txt";
if (saveFileDialogl.ShowDialog() == DialogResult.OK)
{
32
33
34
35
36
37
38
c
// sau khi user nhập tên file cần lưu và nhấn "OK"
Ị Ị tạo stream dể đọc ghi file
Stream myStream = saveFileDialogl.OpenFile();
// tạo dối tượng ghi vào file
StreainlJriter writer = new StreamWriter (myStream) ;
// lưu nội dung text của textboxl vào file
writer .Write(textBoxl.Text);
writer.Close();
myStreaiii. Close () ;
Nguyên Văn Phong - 2010
SaveFileDialog
Save As
is Save File Dialog
Nhập đoạn text
Nguyễn Há Giang
email: ngụuenha.
Hutech
Save in:
Liầ
My Recent
Documents
Desktop
My Documenls
3*
My Compute!
. Æ
My Network
^ Data (0:)
IJ}baitap3
ÖCNTT
Q D O AN CO SO
Il^Download
Q eclipse
L_3Hag
i_jHaGiang
Q Health
_)HW
1.^1 icon
o image
Qmanagebook
"I Micro Mouse
Ô N e w Folder
¡_}QuanLySinhVien
i OÍ ° 09-
I.''"t Software
Q Th ien
Q Upload
ẽ)vd
i ö WebSite 1
Õ Working
File name:
Save as type:
GianqProfile.txt
Text File(Mxt)
Lüu file
Save
Cancel
Nguyễn Văn Phong - 2010
• 1
SaveFileDialog
MỞ rộng bổ sung phần đọc file text từ demo
trên
° Open file *.txt và hiển thị nội dung của file trên
TextBox.
Bổ sung thêm button “MỞ file” và kéo thành
phần OpenFileDicj^““ '
t Dialogs
Pointer
ColorDialog
J1 FolderBrowserDialog^
A FontDialog y
OpenFileDialog
jr l SaveFileDialog
Save Ole Dialog
Nhập đoạn text
Drag & drop
M
jr ] saveFileDialog 1 ylooenFileDialoal
Nguyên Văn Phong - 2010
SaveFileDialog
■ Viết phần xử lý button “MỞ file”
43 //
chức năng mả file txt và hiển thị nội dung
44
openFileDialogl.Filter = "Text File ( * .txt) 1 * .txt";
45
4 6
if (openFileDialogl. ShowDiaJLog () == DialogResult.OK)
47
{
48
// tạo stream dọc/ghi file
49
Stream myStream = openFileDialogl.OpenFile();
50
Ị Ị tạo doi tượng dọc file
51
StreamReader reader = new StreamReader (iriyStream) ;
52
Ị Ị dọc toàn bộ nội dung vào textbox 1
53
textBoxl.Text = reader.ReadToEnđ();
54
55
reader.Close();
56
myStream.Close ();
57 }
Nguyễn Văn Phong - 2010
SaveFileDialog
Demo
Look in:
My Recent
Documents
Desktop
My Document
&
My Compute
3
My Network
¡¡¿J)Access Connections
O Downloads
Q History
Ir^Xre ato r Pro
^JModelwofks
Q My Code Snippets
;~)Mv eBooks
^ M y Music
_J My Notebook Content
£|M y Pictures
OSharpDevelop Projects
lJ SMART Notebook
L j SQL Server Management Studio Express
H_JTAl xu6ng
ii__5 Visual Studio 20(35
Hien thi ndi
• •
dung file
0ÔCC
0 CSref
0 dcqk s '
$152593233 ♦*
0 Irk
0 link tai lieu
0 minipad 1
0 pass aptech
Fie Qame-
Fies of Jype-
giansLprofile
¡Text FüefMxt)
Save File Dialog
Đ®® 1
Nhập đoạn text *•
Nguyễn Hà Giang n
Hutech
Email:
Mb file LJu file
Nguyễn Văn Phong - 2010
Nội Dung
a Giới thiệu Common Dialog
a OpenFileDialog
a SaveFileDialog
a FontDialog
a ColorDialog
a MessageBox
a User Control
Nguyễn Văn Phong - 2010
FontDialog
■ Chức năng hiển thị hộp thoại chọn font chữ
được install trong máy
a Trong ứng dụng làm việc với document, đồ họa
■ sử dụng FontDialog
a TừToolBox kéo thả FontDialog vào Form
■ sử dụng trong lúc thiết kê
a Khai báo thể hiện FontDialog và hiển thị
■ Viết code
Nguyễn Văn Phong - 2010
FontDialog
Thuộc tính, phương thức thường dùng
Thuôc tính
•
Font Get/set font đươc chon
■ ■
Color Get/set màu của font
ShovvColor Hiển thị hay không hiển thị chọn
màu
ShovvApply Hiển thị/ không button Apply
Phương thức
ShovvDialog Hiển thị dialog ra màn hình
Sư kiên
• *
Apply Kích hoạt khi user chọn apply
FontDialog
■ Demo: gọi FontDialog thiết lập font cho control
■ Tạo Form có dạng sau
Label để test font
^ Pointer
ta ColcrDialog
ĩ\ FolderBrowserDialog
|3«*1 FontDialog
y | OpenFileDialog
^1 SaveFileDialog
chon font
Nguyên Văn Phong - 2010
J3 fontDialogl
FontDialog
■ Phần xử lý cho button “Font”
21 // hiển thị chức năng chọn màu
22 fontDialogl.ShowColor = true;
23 // chọn màu mặc dinh là màu làbel
2 fontDialogl.Color = latoell.ForeColor;
25 // thiết lập font mặc định là font của label
26 fontDialogl.Font = labell.Font;
2 7
if (f ontDialogl. ShowDiaJLog () == DialogResult. OK)
2 9 {
// thiết lập font mới cho lábel
labell.Font = fontDialogl.Font;
32 // thiết lập màu mới cho label
latoell.ForeColor = fontDialogl.Color;
3 4 }
Nguyễn Văn Phong - 2010
FontDialog
Effects
□ Strikeout
□ Underline
Color
Font stjile:
Verdana
Bold
Veidana 1 Regular
'■•Ï Verdana Ref Italic
0 Vinet Hand ITC
Bn-,
'ặ Vinhan
Bold Italic
0 Vivaldi
0 Vladimir Script
Ï? VNI-Allegie
Sample
Size:
24
16
A
18
20
??
2S
28
V
AaBbYvi
■
Script
Western
HR Demo FontDialog
B @® 1
ĐẠI HỌC KỸ THUẬT CỒNG NGHÊ
\
n Font I
w
UDemoFontDialog
BiS
Đ A I H O C K Ỹ T H U Â T C Ô N G N G H Ê
■ a ■ ■
I w. I
Nguyễn Văn Phong - 2010
FontDialog
■ Bổ sung chức năng “Apply” của FontDialog
° Khi FontDialog đang hiển thị cho phép user kích
chọn nút Apply => label thay đổi font theo.
■ Cách thực hiện
■ ■
a Khai báo có hiện thị button Apply cho FontDialog
■ fontDialogl.ShowApply = true
■ Đăng ký trình xử lý sự kiện cho button “Apply”
□ Tạo trình xử lý cho sự kiện “Apply” của
FontDialog
a Đăng ký trình xử lý cho sự kiện Apply của
FontDialog
Nguyễn Văn Phong - 2010