common dialog

42
1 Common Dialog Common Dialog

Upload: marlis

Post on 20-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Common Dialog. Nội Dung. Giới thiệu Common Dialog OpenFileDialog SaveFileDialog FontDialog ColorDialog MessageBox. Common Dialog. Các dialog thường được sử dụng Được warp thành các lớp trong FCL/BCL. OpenFileDialog SaveFileDialog FontDialog ColorDialog PrintDialog - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Common Dialog

1

Common DialogCommon Dialog

Page 2: Common Dialog

2

Nội DungNội Dung

Giới thiệu Common DialogGiới thiệu Common Dialog OpenFileDialog SaveFileDialog FontDialog ColorDialog MessageBox

Page 3: Common Dialog

3

Common DialogCommon Dialog

Các dialog thường được sử dụng Được warp thành các lớp trong FCL/BCL

OpenFileDialog SaveFileDialog FontDialog ColorDialog PrintDialog PrintPreviewDialog

Page 4: Common Dialog

Color & Font Dialogs

Page 5: Common Dialog

5

Common DialogCommon Dialog

Cách sử dụng 1. Kéo thả dialog vào form

VS sẽ tạo thể hiện chứa trong lớp form 2. Khai báo đối tượng và tạo thể hiện của lớp CD

VD: OpenFileDialog oFile = new OpenFileDialog();

Com

mon

Dia

log

Com

mon

Dia

log

Page 6: Common Dialog

6

Nội DungNội Dung

Giới thiệu Common Dialog OpenFileDialogOpenFileDialog SaveFileDialog FontDialog ColorDialog MessageBox

Page 7: Common Dialog

7

OpenFileDialogOpenFileDialog

Sử dụng để chọn file lưu trên đĩa Cách sử dụng từ ToolBox

Code phát Code phát sinh tự độngsinh tự động

Drag & dropDrag & drop

xyz.Designer.csxyz.Designer.csdlgColor.ShowDialog( )dlgFont.ShowDialog( )dlgPrint.ShowDialog( )

Page 8: Common Dialog

8

OpenFileDialogOpenFileDialog

Code phát sinh của VSxyz.Designer.csxyz.Designer.cs

Tạo thể hiệnTạo thể hiện

openFileDialog1.ShowDialog();

openFileDialog1.ShowDialog();

Hiển thị FormHiển thị Form

Page 9: Common Dialog

9

OpenFileDialogOpenFileDialog

Code tự viết – không dùng Toolbox

Page 10: Common Dialog

10

OpenFileDialogOpenFileDialog

Thuộc tính, phương thứcThuộc tính, phương thức

Thuộc tínhThuộ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 định kiểu file cần mở

InitialDirectory Thư mục khởi tạo

Multiselect Cho phép chọn nhiều file

Title Tiêu đề của dialog

Phương thứcPhương thức

ShowDialog Hiển thị dialog

Sự kiệnSự kiện

FileOk Xuất hiện khi user kích vào OK

Page 11: Common Dialog

11

OpenFileDialogOpenFileDialog

Ví dụ: Chọn tập tin

private void mởFileToolStripMenuItem_Click(object sender, EventArgs e) { //Gán tập tin đang chọn nếu trước đó đã chọn openFileDialog1.FileName = txtFile.Text; if (openFileDialog1.ShowDialog() == DialogResult.OK) {

//Gán tập tin chọn vào điều khiển TextBox txtFile.Text = openFileDialog1.FileName; }

}

Page 12: Common Dialog

12

OpenFileDialogOpenFileDialog

Ví dụ: Đối tượng OpenFileDialog

private void mởFileToolStripMenuItem_Click(object sender, EventArgs e) {

//Khai báo và khởi tạo đối tượng OpenFileDialogopenFileDialog OD = new openFileDialog();//Gán tập tin đang chọn nếu trước đó đã chọn

OD.FileName = txtFile.Text; if (OD.ShowDialog() == DialogResult.OK) {

//Gán tập tin chọn vào điều khiển TextBox txtFile.Text = OD.FileName;

}

}

Page 13: Common Dialog

13

OpenFileDialogOpenFileDialog

Ví dụ: Chọn nhiều tập tin

private void mởFileToolStripMenuItem_Click(object sender, EventArgs e) { //Khai báo và khởi tạo đối tượng OpenFileDialog

OpenFileDialog OD = new OpenFileDialog();//Cho phép người sử dụng chọn nhiều tập tinOD.Multiselect = true;//Gán tập tin đang chọn nếu trước đó đã chọn

if (OD.ShowDialog() == DialogResult.OK) { //Duyệt qua từng tập tin

foreach (string name in OD.FileName) {

richTextBox1.AppendText(name + "\r\n"); }

} }

Page 14: Common Dialog

14

Nội DungNội Dung

Giới thiệu Common Dialog OpenFileDialog SaveFileDialogSaveFileDialog FontDialog ColorDialog MessageBox

Page 15: Common Dialog

15

SaveFileDialogSaveFileDialog

Sử dụng để tạo file trên đĩa. Cách sử dụng

Sử dụng SaveFileDialog component trên Toolbox Tương tự như OpenFileDialog!

Tạo thể hiện của lớp SaveFileDialog

Page 16: Common Dialog

16

SaveFileDialogSaveFileDialog

Demo: nhập văn bản vào textbox, sau đó lưu xuống file *.txt. 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.

Page 17: Common Dialog

17

SaveFileDialogSaveFileDialog

Thiết kế form

Drag & dropDrag & drop

Multiline=trueMultiline=true

Page 18: Common Dialog

18

SaveFileDialogSaveFileDialog

Viết phần xử lý cho button “Lưu file”

System.ISystem.IOO

Page 19: Common Dialog

19

SaveFileDialogSaveFileDialog

Page 20: Common Dialog

20

SaveFileDialogSaveFileDialog

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 OpenFileDialog thả vào form

Drag & dropDrag & drop

Page 21: Common Dialog

21

SaveFileDialogSaveFileDialog

Viết phần xử lý button “Mở file”

Page 22: Common Dialog

22

SaveFileDialogSaveFileDialog

DemoHiển thị nội Hiển thị nội

dung filedung file

Page 23: Common Dialog

23

Nội DungNội Dung

Giới thiệu Common Dialog OpenFileDialog SaveFileDialog FontDialogFontDialog ColorDialog MessageBox

Page 24: Common Dialog

24

FontDialogFontDialog

Chức năng hiển thị hộp thoại chọn font chữ được install trong máy Trong ứng dụng làm việc với document, đồ họa…

Sử dụng FontDialog Từ ToolBox kéo thả FontDialog vào Form

Sử dụng trong lúc thiết kế Khai báo thể hiện FontDialog và hiển thị

Viết code

Page 25: Common Dialog

25

FontDialogFontDialog

Thuộc tính, phương thức thường dùngThuộc tính, phương thức thường dùng

Thuộc tínhThuộc tính

Font Get/set font được chọn

Color Get/set màu của font

ShowColor Hiển thị hay không hiển thị chọn màu

ShowApply Hiển thị/ không button Apply

Phương thứcPhương thức

ShowDialog Hiển thị dialog ra màn hình

Sự kiệnSự kiện

Apply Kích hoạt khi user chọn apply

Page 26: Common Dialog

26

FontDialogFontDialog

Demo: gọi FontDialog thiết lập font cho control Tạo Form có dạng sau

Drag & dropDrag & drop

Label để test fontLabel để test font

Button chọn fontButton chọn font

Page 27: Common Dialog

27

FontDialogFontDialog

Phần xử lý cho button “Font”

Page 28: Common Dialog

28

FontDialogFontDialog

Page 29: Common Dialog

29

FontDialogFontDialog

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

Khai báo có hiện thị button Apply cho FontDialog fontDialog1.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

Đăng ký trình xử lý cho sự kiện Apply của FontDialog

Page 30: Common Dialog

30

FontDialogFontDialog

Code minh họa

Page 31: Common Dialog

31

Nội DungNội Dung

Giới thiệu Common Dialog OpenFileDialog SaveFileDialog FontDialog ColorDialogColorDialog MessageBox

Page 32: Common Dialog

32

ColorDialogColorDialog

Hiển thị chức năng chọn màu Sử dụng theo 2 cách

Kéo ColorDialog từ Toolbox thả vào form… Viết code tạo đối tượng…

ColorDialog sử dụng giá trị màu có kiểu Color Màu ARGB

Alpha Red Green Blue

Page 33: Common Dialog

33

ColorDialogColorDialog

Demo chọn màu: chọn màu nền cho form

Drag & dropDrag & drop

Page 34: Common Dialog

34

ColorDialogColorDialog

private void colorToolStripMenuItem_Click(object sender, EventArgs e)

{

colorDialog1.ShowDialog();

lst1.ForeColor = colorDialog1.Color;

}

Page 35: Common Dialog

35

ColorDialogColorDialog

Phần xử lý cho button “Color”

Page 36: Common Dialog

36

Nội DungNội Dung

Giới thiệu Common Dialog OpenFileDialog SaveFileDialog FontDialog ColorDialog MessageBoxMessageBox

Page 37: Common Dialog

37

MessageBoxMessageBox

Hiển thị hộp thoại chứa thông tin chỉ dẫn đến user

Bao gồm các phần Text Button Symbol

Lớp MessageBox có phương thức tĩnh Show để hiển thị dialog. Bắt giá trị trả về để biết phản ứng của user

Page 38: Common Dialog

38

MessageBoxMessageBox

Có khoảng 21 phiên bản của Show Một số phiên bản thường sử dụng

DialogResult ShowShow(string text); DialogResult ShowShow(string text, string caption); DialogResult ShowShow(string text, string caption,

MessageBoxButtons button); DialogResult ShowShow(string text, string caption,

MessageBoxButtons button, MessageBoxIcon icon); …

Page 39: Common Dialog

39

MessageBoxMessageBox

Các button hiển thị theo message Cho phép user chọn lựa các phản ứng với

message Được định nghĩa trong MessageBoxButtons

OK

OKCancel

AbortRetryIgnore

YesNoCancel

YesNo

RetryCancel

MessageBoxButtonsMessageBoxButtons

Page 40: Common Dialog

40

MessageBoxMessageBox

MessageBoxIcon: định nghĩa các icon kèm theo message

MessageBoxIcon.Asterisk

MessageBoxIcon.Information

MessageBoxIcon.Error

MessageBoxIcon.Stop

MessageBoxIcon.Hand

MessageBoxIcon.Exclamation

MessageBoxIcon.Warning

MessageBoxIcon.Question

MessageBoxIcon.None

Page 41: Common Dialog

41

MessageBoxMessageBox

Một số các MessageBox minh họa

Page 42: Common Dialog

42

TTóóm tắtm tắt

Common Dialog OpenFileDialog - SaveFileDialog FontDialog - ColorDialog

MessageBox Khai báo và sử dụng MessageBoxIcon MessageBoxButtons