Bài giảng Lập trình ứng dụng Windows Form in VB.NET 2005 - Buổi 3: DialogBox và Printing

™MessageBox.Show MessageBox.Show; ; DialogResult DialogResult

™MsgBox;

™InputBox

™DialogResult

™ColorDialog,

™FontDialog,

™OpenFiletDialog

™FolderBrowserDialog

pdf18 trang | Chuyên mục: Visual Basic 6.0 | Chia sẻ: dkS00TYs | Lượt xem: 2200 | Lượt tải: 3download
Tóm tắt nội dung Bài giảng Lập trình ứng dụng Windows Form in VB.NET 2005 - Buổi 3: DialogBox và Printing, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
sageBox.Show; DialogResult
™MsgBox; 
™InputBox
™ DialogResult
™ ColorDialog, 
™FontDialog, 
™OpenFiletDialog
™FolderBrowserDialog
Windows Form programming with VB.Net 2005. 2
™Show Form 
Form có hai Method Show là:
1. Show(): Form Modalless Dialog Box
2. ShowDialog(): Form dạng Modal Dialog Box
™ Ghi chú: 
- Modalless Dialog Box: có thể chuyển Active qua 
lại giữa các Form trong một chương trình
- Modal Dialog Box: Phải thực hiện và Đóng Form 
được gọi thì mới Active Form gọi
- Vd Gọi Form: .Show() hoặc 
. ShowDialog()
†04/07/2010
†2
Windows Form programming with VB.Net 2005. 3
™MessageBox.Show(P1,P2,P3,P4)
P1: String: Thông tin cần hiển thị
P2: Caption: Tieu đề của MessageBox
P3: MessageBoxButtons: AbortRetryIgnore , OK , 
OKCancel , RetryCancel , YesNo ,YesNoCancel 
P4:MessageBoxIcon: Error, Information , None, 
Question , Stop , Warning 
Hàm trả về giá trị của DialogResult.OK, Cancel , 
Abort, Retry,Cancel , Yes,No
Windows Form programming with VB.Net 2005. 4
™Vd:
Dim result As new DialogResult 
resul =MessageBox.Show("Bạn có muốn Thoát 
chương trình không?", "Thông báo", 
MessageBoxButtons.YesNo, 
MessageBoxIcon.Question)
If result =Windows.Forms.DialogResult.Yes
Then
[Thực hiện công việc]
Else
[Không thực hiện công việc]
End If
†04/07/2010
†3
Windows Form programming with VB.Net 2005. 5
™MsgBox(P1,P2,P3)
P1: String: Thông tin cần hiển thị
P2: MsgBoxStyl`e: các kiểu hiển thị tương tự 
MessageBox
P3: Title: Tiêu đề MessageBox 
Hàm trả về giá trị của MsgBoxResult.OK, 
Cancel , Abort, Retry,Cancel , Yes,No
Windows Form programming with VB.Net 2005. 6
™Vd: MsgBox()
Dim msg, title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult
msg = "Do you want to continue?" 
style = MsgBoxStyle.DefaultButton2 Or _
MsgBoxStyle.Critical Or MsgBoxStyle.YesNo
title = "MsgBox Demonstration" 
response = MsgBox(msg, style, title)
If response = MsgBoxResult.Yes Then 
' Perform some action.
Else
' Perform some other action.
End If
†04/07/2010
†4
Windows Form programming with VB.Net 2005. 7
™Vd: MessageBox.Show()
Private Sub frmMain_FormClosing(…,…) …
Dim result As New DialogResult
result = MessageBox.Show("Bạn có muốn Thoát 
chương trình không?", "Form closing", 
MessageBoxButtons.YesNo, 
MessageBoxIcon.Question)
If result = Windows.Forms.DialogResult.No Then
' Không cho đóng
e.Cancel = True
End If
End Sub
Windows Form programming with VB.Net 2005. 8
™InputBox
Public Function InputBox( ByVal Prompt As 
String, 
Optional ByVal Title As String = "", 
Optional ByVal DefaultResponse As String = "", 
Optional ByVal Xpos As Integer = -1, 
Optional ByVal YPos As Integer = -1 _
) As String
¾Hàm trả về giá trị là String được nhập vào hoặc 
giá trị mặc định (Default) nếu có; xPos: tọa độ 
tính từ bên trái màn hình; yPos: tọa độ tính từ 
phía trên màn hình xuống khi hiển thị InputBox
†04/07/2010
†5
Windows Form programming with VB.Net 2005. 9
™Vd: InputBox()
Dim msg, title, strDefault As String
Dim myValue As Object
msg = "Enter a value between 1 and 3"
title = "InputBox Demo"
strDefault = "1" 
myValue = InputBox(msg, title, strDefault, 50, 100)
' nếu click Cancel thì nhận giá trị strDefault
If myValue Is "" Then myValue = strDefault
Windows Form programming with VB.Net 2005. 10
™ColorDialog
†04/07/2010
†6
Windows Form programming with VB.Net 2005. 11
™ColorDialog
¾Properties:
- AllowFullOpen: định nghĩa màu theo ý người 
dùng (True or False)
- FullOpen: cho phép bảng màu mở rộng khi 
show (True or False)
- Color: Get or Set the Color is selected 
¾Method:
ShowDialog: hiển thị hộp thoại Color trả về kết 
quả của DialogResult là OK hoặc Cancel
Windows Form programming with VB.Net 2005.
VD:
12
Khi click Color button Æ OK đổi màu cho 
Textbox và label
†04/07/2010
†7
Windows Form programming with VB.Net 2005. 13
™FontDialog
Windows Form programming with VB.Net 2005. 14
™FontDialog
¾Properties:
Font: Get or Set FontName
Color: Get or Set FontColor
¾Method:
ShowDialog: hiển thị dialog Font trả về kết quả 
của DialogResult là OK hoặc Cancel
Mục đích của Control này dùng để thay đổi font 
và màu của font.
†04/07/2010
†8
Windows Form programming with VB.Net 2005.
VD:
15
Khi click Font button Æ OK đổi Font cho 
Textbox, label, button...
Windows Form programming with VB.Net 2005. 16
™OpenFileDialog
†04/07/2010
†9
Windows Form programming with VB.Net 2005. 17
™OpenFileDialog
¾Properties:
- FileName: trả về tên file đã được chọn 
- FileNames: trả về tên tất cả file đã được chọn 
- MultiSelect: True or False
- Filter: Lọc các dạng file theo người dùng
- FilterIndex: Chỉ ra Index hiện tại trong Filter
- InitialDirectory: Khởi tạo thư mục, vd: “C:\”
¾Method:
- Open: Mở file đã được chọn
- ShowDialog:
Windows Form programming with VB.Net 2005. 18
™VD:
Private Sub bntInsertImage_Click(… , …) 
OpenFileDialog1.Filter = "Picture files 
(*.jpg;*.gif)|*.jpg;*.gif| All files (*.*)|*.*"
OpenFileDialog1.FilterIndex = 1
If OpenFileDialog1.ShowDialog() = 
Windows.Forms.DialogResult.OK Then
PictureBox1.ImageLocation = 
OpenFileDialog1.FileName
End If
End Sub
†04/07/2010
†10
Windows Form programming with VB.Net 2005. 19
™FolderBrowserDialog
Windows Form programming with VB.Net 2005. 20
™FolderBrowserDialog
¾Properties:
- SelectedPath: Get or Set Path of Folder
¾Method:
ShowDialog: hiển thị dialog Font trả về kết quả 
của DialogResult là OK hoặc Cancel
†04/07/2010
†11
Windows Form programming with VB.Net 2005. 21
™VD:
Private Sub bnt_Click(… , …) 
If FolderBrowserDialog1.ShowDialog() = 
Windows.Forms.DialogResult.OK Then
lblPath.Text = "Path: " & 
FolderBrowserDialog1.SelectedPath
End If
End Sub
Windows Form programming with VB.Net 2005.
Bài tập:
22
Get File name: openFile hình thay cho hình hiện tại
Get Paht: lấy đường dẫn (Path) cho hiển thị tại lblPath (Path:)
Color: đổi màu nền cho form, label, buttom, textbox….
Font: đổi font cho form, label, buttom, textbox….
Viết Code cho event FormClosing có Messsage hỏi có YesNo nếu click No 
thì không đóng form
†04/07/2010
†12
Windows Form programming with VB.Net 2005. 23
B. Printing
™ PrintDocument
™ PrintPreviewDialog
™ PrintPreviewControl
™ PageSetupDialog
™ PrintDialog
Windows Form programming with VB.Net 2005. 24
™PrintDocument
¾Properties:
- DocumentName: chỉ ra tên document được in 
¾Method: Print: ra lệnh in Document
¾Event: 
-BeginPrint: thực thi trước khi in trang đầu tiên
-EndPrint: thực thi sau khi in trang cuối cùng
-PrintPage: In các thành phần được đưa vào 
Document qua đối tượng e.Graphics.Draw…
†04/07/2010
†13
Windows Form programming with VB.Net 2005. 25
VD
Private Sub PrintDocument1_PrintPage(…)
e.Graphics.DrawString("Happy new year", New 
Font("tahoma", 50), Brushes.Black, 160, 125) 
e.Graphics.DrawImage(Image.FromFile("Blue.jpg
"), 200, 200, 500, 500)
End Sub
Windows Form programming with VB.Net 2005. 26
™PrintPreviewControl
PrintPreview
Control
†04/07/2010
†14
Windows Form programming with VB.Net 2005. 27
™PrintPreviewControl
¾Properties:
- AutoZoom: True or False 
- Columns: số trang hiển thị theo chiều ngang
- Rows: số trang hiển thị theo chiều dọc
- Document : chỉ ra Document được Preview 
(Object là PrintDocument)
- Size : chỉ định chiều cao – rộng của control
- Zoom : Phong to thu nhỏ Document với giá trị 
là double (vd: 0.25; 0.50; 1.0…)
Windows Form programming with VB.Net 2005. 28
VD
Private Sub cboZoom_SelectedIndexChanged (…)
Dim ZoomNo As Integer = 0
Dim strZoom = cboZoom.SelectedItem
strZoom = Replace(strZoom, "%", "")
ZoomNo = CInt(strZoom)
PrintPreviewControl1.AutoZoom = True
' PrintPreviewControl1.Size = New Size(ZoomNo 
* 10, ZoomNo * 10)
PrintPreviewControl1.Document = 
PrintDocument1
PrintPreviewControl1.Zoom = ZoomNo / 100
End Sub
†04/07/2010
†15
Windows Form programming with VB.Net 2005. 29
™PrintPreviewDialog
¾Properties:
- DocumentName: chỉ 
ra document được 
Preview (Object là 
PrintDocument)
¾Method:
- ShowDialog: Hiển thị 
hộp thoại 
PrintPreview
Windows Form programming with VB.Net 2005. 30
VD
Private Sub bntPrintPreview_Click (… …)
PrintPreviewDialog1.Document = 
PrintDocument1
PrintPreviewDialog1.ShowDialog() 
End Sub
†04/07/2010
†16
Windows Form programming with VB.Net 2005. 31
™PageSetupDialog
¾Properties:
- Document: tài liệu cân 
Page Setup (Object là 
PrintDocument)
- PageSettings: đối tượng 
thuộc namespace 
System.Drawing.Printing.
PageSettings
¾Method:
- ShowDialog: Hiển thị hộp 
thoại Page Setup
Windows Form programming with VB.Net 2005. 32
VD
Private Sub btnPageSetup_Click (…)
Dim pgSetting As New 
System.Drawing.Printing.PageSettings
PageSetupDialog1.PageSettings = pgSetting
PageSetupDialog1.Document = PrintDocument1
PageSetupDialog1.ShowDialog()
End Sub
†04/07/2010
†17
Windows Form programming with VB.Net 2005. 33
™PrintDialog
Windows Form programming with VB.Net 2005. 34
™PrintDialog
¾Properties:
- Document: Object là PrintDocument
- AllowPrintToFile: cho phép in ra File
- AllowSelection: cho phep in From… To …
- AllowSomePages: cho phép in trang chỉ ra 
¾Method:
- ShowDialog: Hiển thị hộp thoại PrintDialog để
chọn máy in
- Print: thực hiện In
†04/07/2010
†18
Windows Form programming with VB.Net 2005. 35
Private Sub btnPrint_Click (…)
PrintDocument1.DefaultPageSettings.Landscape = True
PrintDialog1.Document = PrintDocument1
PrintDialog1.AllowPrintToFile = True
PrintDialog1.AllowSelection = True
PrintDialog1.AllowSomePages = True
Dim result As DialogResult = 
PrintDialog1.ShowDialog()
If result = Windows.Forms.DialogResult.Yes Then
PrintDocument1.Print()
End If
End Sub
VD
Windows Form programming with VB.Net 2005.
Bài tập:
36
Zoom: phóng to thu nhỏ PrintDocument 
Page Setup: gọi PageSetupDialog cho PrintDocument
Print Preview: gọi PrintPreviewDialog cho PrintDocument
Print : gọi PrintDialog cho PrintDocument

File đính kèm:

  • pdfBài giảng Lập trình ứng dụng Windows Form in VB.NET 2005 - Buổi 3_DialogBox và Printing.pdf
Tài liệu liên quan