1 / 42

Common Dialog

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

marlis
Download Presentation

Common Dialog

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Common Dialog

  2. Nội Dung • Giới thiệu Common Dialog • OpenFileDialog • SaveFileDialog • FontDialog • ColorDialog • MessageBox

  3. 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 • PrintPreviewDialog

  4. Color & Font Dialogs

  5. Common 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(); Common Dialog

  6. Nội Dung • Giới thiệu Common Dialog • OpenFileDialog • SaveFileDialog • FontDialog • ColorDialog • MessageBox

  7. OpenFileDialog • Sử dụng để chọn file lưu trên đĩa • Cách sử dụng từ ToolBox Drag & drop Code phát sinh tự động dlgColor.ShowDialog( ) dlgFont.ShowDialog( ) dlgPrint.ShowDialog( ) xyz.Designer.cs

  8. OpenFileDialog • Code phát sinh của VS xyz.Designer.cs Tạo thể hiện Hiển thị Form openFileDialog1.ShowDialog();

  9. OpenFileDialog • Code tự viết – không dùng Toolbox

  10. OpenFileDialog

  11. OpenFileDialog 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; } } 11

  12. OpenFileDialog 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 OpenFileDialog openFileDialog 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; } } 12

  13. OpenFileDialog 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 tin OD.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"); } } } 13

  14. Nội Dung • Giới thiệu Common Dialog • OpenFileDialog • SaveFileDialog • FontDialog • ColorDialog • MessageBox

  15. SaveFileDialog • 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

  16. SaveFileDialog • 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.

  17. SaveFileDialog • Thiết kế form Drag & drop Multiline=true

  18. SaveFileDialog • Viết phần xử lý cho button “Lưu file” System.IO

  19. SaveFileDialog

  20. 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 OpenFileDialog thả vào form Drag & drop

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

  22. SaveFileDialog • Demo Hiển thị nội dung file

  23. Nội Dung • Giới thiệu Common Dialog • OpenFileDialog • SaveFileDialog • FontDialog • ColorDialog • MessageBox

  24. FontDialog • 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

  25. FontDialog

  26. FontDialog • Demo: gọi FontDialog thiết lập font cho control • Tạo Form có dạng sau Label để test font Drag & drop Button chọn font

  27. FontDialog • Phần xử lý cho button “Font”

  28. FontDialog

  29. 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 • 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

  30. FontDialog • Code minh họa

  31. Nội Dung • Giới thiệu Common Dialog • OpenFileDialog • SaveFileDialog • FontDialog • ColorDialog • MessageBox

  32. ColorDialog • 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

  33. ColorDialog • Demo chọn màu: chọn màu nền cho form Drag & drop

  34. ColorDialog private void colorToolStripMenuItem_Click(object sender, EventArgs e) { colorDialog1.ShowDialog(); lst1.ForeColor = colorDialog1.Color; } 34

  35. ColorDialog • Phần xử lý cho button “Color”

  36. Nội Dung • Giới thiệu Common Dialog • OpenFileDialog • SaveFileDialog • FontDialog • ColorDialog • MessageBox

  37. MessageBox • 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

  38. MessageBox • Có khoảng 21 phiên bản của Show • Một số phiên bản thường sử dụng • DialogResult Show(string text); • DialogResult Show(string text, string caption); • DialogResult Show(string text, string caption, MessageBoxButtons button); • DialogResult Show(string text, string caption, MessageBoxButtons button, MessageBoxIcon icon); • …

  39. MessageBox • 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 MessageBoxButtons

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

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

  42. Tóm tắt • Common Dialog • OpenFileDialog - SaveFileDialog • FontDialog - ColorDialog • MessageBox • Khai báo và sử dụng • MessageBoxIcon • MessageBoxButtons

More Related