Dùng Dialogs trong visual basic

15 984 5
Dùng Dialogs trong visual basic

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Dùng Dialogs trong visual basic

Chương Mười Một - Dùng Dialogs Dialogs (giao thoại) dùng để hiển thị tin tức nhận mouse hay keyboard input từ users tùy theo tình Chúng dùng để tập trung ý users vào công tác đương thời program nên hữu dụng chương trình Windows Có nhiều dạng Dialogs, thứ áp dụng cho hoàn cảnh riêng biệt Trong chương nầy ta bàn qua loại Dialogs nghiên cứu cách ta dùng chúng: Message Boxes Input Boxes Common Dialogs Custom Dialogs Message Boxes Message Boxes dùng để nhắc nhở user chuyện gì, địi hỏi phản ứng từ user Thí dụ ta chấm dứt program MSWord mà chưa lưu trử hồ sơ MSWord nhắc ta lưu trử Dialog đây: Trong trường hợp nầy user click buttons Nếu click Yes xúc tiến việc lưu trử hồ sơ trước kết thúc program MSWord Nếu click No MSWord lặng lẽ kết thúc Nếu click Cancel có nghĩa user đổi ý việc chấm dứt program trở lại tiếp tục dùng MSWord Ta dùng routine MsgBox để hiển thị Message Box coding hình đây: Parameter (thông số) thứ MsgBox text message Close the program down?, parameter thứ nhì tập hợp icon (vbQuestion) số buttons (vbOKCancel) cách cộng hai constants: vbQuestion + vbOKCancel (hai buttons OK Cancel), parameter thứ ba title (tiêu đề) Dialog Trong thí dụ MSWord bên Constant icon buttons vbExclamation + vbYesNoCancel (ba buttons Yes, No Cancel) Cancel) Ta chọn số loại buttons theo bảng đây:n số loại buttons theo bảng đây: Cancel) loại buttons theo bảng đây:i buttons theo bảng đây:ng đây:i đây:ây: Constant Các buttons vbOKOnly OK vbOKCancel OK Cancel vbYesNo Yes No vbRetryCancel Retry Cancel vbYesNoCancel Yes No Cancel vbAbortRetryIgnore Abort Retry Ignore Constant icons ta dùng vbCritical, vbQuestion, vbExclamation vbInformation Khi Message Box mở ra, program ngừng lại đợi user phản ứng Ta nói Message Box hiển thị Modal Mode, dành ý tạm ngưng execution khác program Sau user click button, Message Box biến program tiếp tục chạy từ hàng code hàng MsgBox Trong thí dụ ta dùng MsgBox Sub, ta dùng MsgBox Function để biết user vừa click button Function MsgBox returns value (trả giá trị) mà ta thử biết để theo thi hành Thí dụ như: như: Private Sub CmdPrompt_Click() Dim ReturnValue As Integer ReturnValue = MsgBox("Close the program down", vbQuestion + vbOKCancel, "Exit Program") Select Case ReturnValue Case vbOK MsgBox "You clicked OK" Case vbCancel MsgBox "You clicked Cancel" End Select End Sub Các tr số loại buttons theo bảng đây: Visual Basic intrinsic constants mà Cancel) Function MsgBox returns Cancel).: Trị số Tên Const OK VbOK Cancel vbCancel Abort vbAbort Retry vbRetry Ignore vbIgnore Yes vbYes No VbNo Bạn hiển thị Text message Message Box thành nhiều hàng cách dùng Constant vbCrLf (CarriageReturn Cancel) LineFeed) đây:ể đánh dấu chỗ ngắt khúc sau: đây:ánh dấu chỗ ngắt khúc sau:u chỗ ngắt khúc sau:ng chỗ ngắt khúc sau: ngắt khúc sau:t khúc sau: MsgBox "This is the first line" & vbCrLf & " followed by the second line" Nếu bạn thấy thường dùng MsgBox với icon buttons, có Text message khác nhau, bạn viết Global Subroutine BAS module để dùng lại nhiều lần Thí dụ bạn có Global Sub sau: Public Sub DisplayError(ByVal ErrMess As String ) MsgBox ErrMess, vbCritical + vbOKOnly, "Error" End Sub Mỗi lần muốn hiển thị Error message bạn cần gọi Sub DisplayError với Text message mà không sợ dùng lầm lẫn icon Sau nầy muốn đổi cách hiển thị Error message cần edit chỗ Nếu user muốn bạn lưu trữ tất errors xẩy lúc run-time, bạn cần thêm vài hàng code Sub DisplayError để viết Error message vào text file Input Boxes Với Message Boxes, user click lên button Đơi ta muốn user đánh vào thêm kiện, trường hợp ta dùng Input Boxes Input Boxes giố loại buttons theo bảng đây:ng giố loại buttons theo bảng đây:ng Message Box, chun nhận input data từ user khơng n input data từ user không user Cancel) không hiể đánh dấu chỗ ngắt khúc sau:n th icon Thí dụ:t icon Thí dụ như:: Private Sub CmdGreeting_Click() Dim strReply As String strReply = InputBox$("Please enter your name", "What 's your name?", "John", 2000, 1000) MsgBox "Hi " & strReply & ", it 's great to meet you!", vbOKOnly, "Hello" End Sub Để ý parameters Function InputBox$ Parameter thứ Text message, parameter thứ hai Title Dialog, parameter thứ ba Default Input Value Đây value hiển thị sẵn Input Box xuất hiện, input user thường đánh vào user cần click nút OK đủ Hai parameters cuối Optional (nhiệm ý, có được, khơng có khơng sao) Nó X,Y coordinates Input Box đơn vị twips Hệ thống tọa độ lấy góc bên trái làm chuẩn với X=0, Y=0 Input Box có hai dạng Functions:   InputBox$ - returns String đàng hoàng InputBox - returns String nằm Variant variable N u bại buttons theo bảng đây:n click nút Cancel returned Value Cancel) empty string, bại buttons theo bảng đây:n đánh dấu chỗ ngắt khúc sau: test empty string đây:ể đánh dấu chỗ ngắt khúc sau: nhận input data từ user không n diện trường hợp nầy.n trường hợp nầy.ng hợp nầy.p nầy.y Dưới đây:i đây:ây Cancel) icon Thí dụ:t thí dụ như: dùng Function InputBox: Private Sub CmdFortuneTeller_Click() Dim varValue As Variant Dim intAge As Integer varValue = InputBox("Please enter your age", "How old are you?", "18") If IsNumeric(varValue) Then intAge = Val(varValue) If intAge < 20 Then MsgBox "You are a young and ambitious person", vbOKOnly, "Observation" Else MsgBox "You are a matured and wise person", vbOKOnly, "Observation" End If Else MsgBox "Oh oh! - please type your age!", vbCritical + vbOKOnly, "Input Error" End If End Sub Khi nên dùng Input Boxes Mặc dầu Input Boxes dễ dùng, thực tế ta dùng lý sau đây:    Ta khơng thể làm lúc user input data, phải đợi sau user click OK bắt đầu xử lý input textstring Ngược lại ta dùng Textbox Form thông thường, ta code Event handlers Events KeyPress hay Change để kiểm soát keystrokes user Input Boxes cho ta đánh vào text string Nhiều ta muốn user đánh vào nhiều thứ nên cần phải có form riêng Sau cùng, Input Boxes xem không đẹp mắt Program dùng Input Boxes khơng chun nghiệp, ta cần phải dùng Custom Dialogs Common Dialogs Bạn có để ý thấy programs Windows có dialogs để Open Save files ? Và tất programs có dialogs để chọn màu, font chữ hay để in ? Đó Dialogs thông dụng thuộc Common Dialog Library MSWindows cho phép program gọi Muốn dùng Dialogs VB6 ta phải reference Comdlg32.ocx IDE Menu command Project | Components chọn Apply Microsoft Common Dialog Control 6.0 Microsoft Common Dialog Control 6.0 cho ta sáu dại buttons theo bảng đây:ng Dialogs tùy theo gọn số loại buttons theo bảng đây:i Method nà Cancel).o: Tên Method Open File ShowOpen Save File ShowSave Color ShowColor Font ShowFont Print ShowPrinter Help ShowHelp Open Save File Dialogs Bại buttons theo bảng đây:n m icon Thí dụ:t Project đây:i với đây:i icon Thí dụ:t button tên CmdOpen Form1 Cancel) đây:ánh Cancel).o code sau đây:ây cho Sub CmdOpen_Click: Private Sub CmdOpen_Click() On Error GoTo DialogError With CommonDialog1 CancelError = True ' Generate Error number cdlCancel if user click Cancel InitDir = "E:\VB6" ' Initial (i.e default ) Folder .Filter = "Executables (*.exe) | *.exe| Batch Files (*.bat)| *.bat" FilterIndex = ' Select ""Executables (*.exe) | *.exe" as default DialogTitle = "Select a program to run" ShowOpen ' Lauch the Open Dialog MsgBox "You selected " & FileName, vbOKOnly + vbInformation, "Open Dialog" End With Exit Sub DialogError: If Err.Number = cdlCancel Then MsgBox "You clicked Cancel!", vbOKOnly + vbInformation, "Open Dialog" Exit Sub Else MsgBox "Error in Dialog's use: " & Err.Description, vbOKOnly + vbCritical, "Error" Exit Sub End If End Sub Hãy chạy program click button Open, program hiển thị error message đây: Đó ta qn bỏ Microsoft Common Dialog Control 6.0 vào Form1 Vậy bạn doubleclick icon ToolBox Bây chạy program lại click button Open để hiển thị Open Dialog Bạn chọn folder tùy ý cách di chuyển từ folder nầy qua folder khác hay thay đổi disk drive Nếu bạn click vào bên phải combobox File of type, dropdown thấy bạn dropdown đây:ể đánh dấu chỗ ngắt khúc sau: cho thấu chỗ ngắt khúc sau:y bại buttons theo bảng đây:n đánh dấu chỗ ngắt khúc sau: chọn số loại buttons theo bảng đây:n icon Thí dụ:t hai loại buttons theo bảng đây:i Files liện trường hợp nầy.t kê statement: Filter = "Executables (*.exe) | *.exe| Batch Files (*.bat)| *.bat" Sau chọn Filename có sẵn hay đánh tên vào File name textbox, bạn click Open Sau đó, CommonDialog1.Filename chứa tên file bạn chọn hay đánh vào Vì ta cho CancelError = True nên user click Cancel program generate Error số 32755 (cdlCancel) Ở ta bắt Error cách dùng On Error GoTo DialogError thử Err.Number= cdlCancel để hiển thị Error message đây: Save Dialog tương tự Open Dialog, ta dùng method ShowSave để hiển thị Trong thí dụ ta định nghĩa properties CommonDialog1 code Bạn dùng Properties Windows để định nghĩa chúng đây: Ngồi ra, bạn dùng trang Properties CommonDialog1 để định nghĩa Properties lúc thiết kế cách right click Commondialog1 Form1 chọn Properties: Properties Pages Dialog hiển thị với Tab Open/Save As có sẵn lúc đầu, bạn đánh tin tức sau: Color Dialog Color Dialog cho user cách chọn màu dễ dùng Ngoài màu có sẵn, user tự tạo màu cho thêm vào bảng màu cung cấp, gọi Windows Palette cách click button Add to Custom Colors Bạn tạo màu cách click chỗ có màu theo ý bảng màu lớn hình vng nắm hình tam giác bên phải kéo lên, kéo xuống để thay đổi độ đậm màu hiển thị hộp vuông Color|Solid Khi vừa ý với màu hiển thị, bạn click button Add to Custom Colors, màu cho thêm vào nhóm Custom Colors nằm phía dưới, bên trái Ta dùng method ShowColor đây:ể đánh dấu chỗ ngắt khúc sau: hiể đánh dấu chỗ ngắt khúc sau:n th Color Dialog Sau user đây:ã chọn số loại buttons theo bảng đây:n icon Thí dụ:t mà Cancel).u rồi, ta i, ta đánh dấu chỗ ngắt khúc sau: tr c ti p assign cho property ForeColor hay BackColor control Trong thí a icon Thí dụ:t control Trong thí dụ như: đây:i đây:ây mà Cancel).u mà Cancel) user vừ user không a chọn số loại buttons theo bảng đây:n đây:ượp nầy.c assigned cho background control Trong thí a picturebox Picture1: Private Sub CmdSelectColor_Click() On Error GoTo NoColorChosen With CommonDialog1 CancelError = True ' Entire dialog box is displayed, including the Define Custom Colors section Flags = cdlCCFullOpen ShowColor ' Launch the Color Dialog Picture1.BackColor = Color ' Assign selected color to background of Picture1 Exit Sub End With NoColorChosen: ' Get here if user clicks the Cancel button MsgBox "You did not select a color!", vbInformation, "Cancelled" Exit Sub End Sub Font Dialog Font Dialog cho ta chọn Font cho ảnh hay printer chọn màu để dùng cho chữ Font Ta dùng method ShowFont đây:ể đánh dấu chỗ ngắt khúc sau: hiể đánh dấu chỗ ngắt khúc sau:n th FontDialog Các chi ti t trình bà Cancel).y Font Dialog tùy thuột icon Thí dụ:c Cancel).o tr số loại buttons theo bảng đây: control Trong thí a Flags sau: Constant Trị số Hiệu cdlCFScreenFonts Chỉ hiển thị Fonts printer hổ trợ cdlCFPrinterFonts Chỉ hiển thị Fonts ảnh, chưa tất printer hổ trợ cdlCFBoth Hiiển thị Fonts ảnh printer cdlCFScalableOnly &H20000 Chỉ hiển thị scalable Fonts TrueType fonts mà bạn cài vào máy Nếu bạn muốn cho user nhiệm ý để chọn màu thêm 256 vào trị số Flags Dưới đây:i đây:ây Cancel) code đây:ể đánh dấu chỗ ngắt khúc sau: cho user chọn số loại buttons theo bảng đây:n Font Cancel) mà Cancel).u control Trong thí a Label1 Private Sub CmdSelectFont_Click() On Error GoTo NoFontChosen CommonDialog1.CancelError = True ' Causes the dialog box to list only the screen fonts supported by the system CommonDialog1.Flags = cdlCFScreenFonts + 256 ' Add 256 to include Color option CommonDialog1.ShowFont ' Launch the Font Dialog With Label1.Font Bold = CommonDialog1.FontBold Italic = CommonDialog1.FontItalic Name = CommonDialog1.FontName Size = CommonDialog1.FontSize Strikethrough = CommonDialog1.FontStrikethru Underline = CommonDialog1.FontUnderline End With Label1.ForeColor = CommonDialog1.Color Label1.Caption = "Hello world!!!, this is a Font Dialog Demo" Exit Sub NoFontChosen: MsgBox "No font was chosen!", vbInformation, "Cancelled" Exit Sub End Sub Chú ý: Nếu bạn quên cho Flags số nói program cho Error message sau: Print Dialog Print Font cho ta giao diện giống Microsoft Office để chọn nhiệm ý việc in Với Print Dialog ta chọn printer với đặc tính cách click button Properties hay button Preferences Ta định in từ trang đến trang document in copies Chỉ có điều phải lưu ý user dùng Print Dialog để chọn Printer khác mà Print Dialog ta chọn Property PrinterDefault = True Printer trở thành Default Printer có hiệu lực vĩnh viễn Windows user thay đổi lại Khác với Color Font Dialogs, Print Dialog không đòi hỏi ta phải cho trị số Property Flags Ta cần dùng Method ShowPrinter để hiển thị Print Dialog Ba properties thường dùng sau user chọn nhiệm ý Print Dialog Copies, FromPage ToPage Để cho user default values properties nầy, bạn để sẵn trị số trước hiển thị Print Dialog Dưới đây:i đây:ây Cancel) code mẫu dùng print Dialog:u dùng print Dialog: Private Sub CmdSelectPrinter_Click() With CommonDialog1 FromPage = ToPage = Copies = ShowPrinter End With End Sub Help Dialog Ta dùng method ShowHelp để hiển thị thông tin giúp đỡ, nhớ phải cho CommonDialog trị số properties HelpFile HelpCommand Private Sub CmdHelp_Click() CommonDialog1.HelpFile = "YourProgram.hlp" CommonDialog1.HelpCommand = cdlHelpContents CommonDialog1.ShowHelp End Sub Để biết thêm chi tiết cách dùng ShowHelp, highlight chữ HelpContext source code VB6 ấn phím F1 chọn MsComDlg Custom Dialogs Nhiều Message Box, Input Box hay dạng Common Dialogs khơng thích hợp cho hồn cảnh lập trình Trong trường hợp bạn dùng Form bình thường để làm thành Dialog nhà, vườn Nó cơng chút, thứ có màu sắc giống Forms khác chương trình, thứ hai ta muốn làm tùy ý Chỉ có bất lợi chương trình dùng nhiều tài ngun hơn, nói thẳng cần thêm memory Sau ta thử triển khai Login Form tổng quát, dùng nhiều trường hợp Khi khởi động, program nầy hiển thị Login form yêu cầu user đánh vào tên mật Sau đó, tên mật hợp lệ Form program Cách ta thực cho program khởi động với Sub Main BAS Module Sub Main gọi Sub GetUserInfo (cũng nằm Module) để hiển thị form frmLogin Modal mode để làm việc cách Message Box, Input Box hay Common Dialogs Khi form frmLogin dấu kín statement Me.Hide execution Sub GetUserInfo dropdown thấy bạn ti p tụ như:c đây:ể đánh dấu chỗ ngắt khúc sau: chi ti t đây:i n Cancel).o textboxes txtUserName Cancel) txtPassword đây:ượp nầy.c trảng đây: v local variables strUserName Cancel) strPassword Mã nguồi, ta n control Trong thí a Sub Main Cancel) Sub GetUserInfo đây:ượp nầy.c liện trường hợp nầy.t đây:i đây:ây: Sub Main() Dim strUserName As String Dim strPassword As String ' Call local Sub getUserInfo to obtain UserName and Password GetUserInfo strUserName, strPassword If strUserName = "" Then MsgBox "Login failed or aborted", vbInformation, "login Aborted" Else MsgBox "User " & strUserName & " logged in with password " & strPassword, vbInformation, "Login accepted" ' Check UserName and Password here ' If valid password then show the Main form of the program which is implemented separately ' frmMain.Show End If End Sub Private Sub GetUserInfo(ByRef sUserName As String, ByRef sPassword As String) ' Invoke frmLogin form in Modal mode frmLogin.Show vbModal ' As soon as frmLogin is hidden, the execution gets here sUserName = frmLogin.txtUserName ' assign the form's txtUserName to sUserName sPassword = frmLogin.txtPassword ' assign the form's txtPassword to sPassword Unload frmLogin ' Unload form frmLogin End Sub Login form hiển thị đây: Sau user điền chi tiết click OK, tạm thời ta hiển thị thông điệp để xác nhận chi tiết Trong tương lai, bạn viết thêm code để kiểm tra xem tên mật có hiệu lực khơng Có vài chi tiết form frmLogin để làm việc giống giống Common Dialog:      Ta cho property BorderStyle frmLogin Fixed Dialog Ta cho Property PasswordChar textbox txtPassword "*" để user điền mật khẩu, ta thấy hàng dấu hoa thị Ta cho Property StartupPosition form CenterScreen Property Default button cmdOK True để user ấn phím Enter form coi tương đương với click button cmdOK Tương tợ thế, Property Cancel button cmdCancel True để user ấn phím Esc form coi tương đương với click button cmdCancel Tại buttons theo bảng đây:m thờng hợp nầy.i coding control Trong thí a event click control Trong thí a cmdOK Cancel) cmdCancel đơn giản liệt kê đây:ơn giản liệt kê n giảng đây:n liện trường hợp nầy.t kê đây:i đây:ây: Private Sub CmdCancel_Click() ' Clear txtUserName and txtPassword txtUserName = "" txtPassword = "" ' Hide the Form to get out of Modal mode Me.Hide End Sub Private Sub CmdOK_Click() ' Hide the Form to get out of Modal mode Me.Hide End Sub ... mắt Program dùng Input Boxes khơng chun nghiệp, ta cần phải dùng Custom Dialogs Common Dialogs Bạn có để ý thấy programs Windows có dialogs để Open Save files ? Và tất programs có dialogs để chọn... nên dùng Input Boxes Mặc dầu Input Boxes dễ dùng, thực tế ta dùng lý sau đây:    Ta khơng thể làm lúc user input data, phải đợi sau user click OK bắt đầu xử lý input textstring Ngược lại ta dùng. .. cách dùng ShowHelp, highlight chữ HelpContext source code VB6 ấn phím F1 chọn MsComDlg Custom Dialogs Nhiều Message Box, Input Box hay dạng Common Dialogs khơng thích hợp cho hồn cảnh lập trình Trong

Ngày đăng: 22/08/2012, 11:04

Hình ảnh liên quan

Ta dùng routine MsgBox để hiển thị Message Box như coding trong hình dưới đây: - Dùng Dialogs trong visual basic

a.

dùng routine MsgBox để hiển thị Message Box như coding trong hình dưới đây: Xem tại trang 1 của tài liệu.
Chương Mười Một - Dùng Dialogs - Dùng Dialogs trong visual basic

h.

ương Mười Một - Dùng Dialogs Xem tại trang 1 của tài liệu.
Ta chọn số và loại buttons theo bảng dưới đây: - Dùng Dialogs trong visual basic

a.

chọn số và loại buttons theo bảng dưới đây: Xem tại trang 2 của tài liệu.
một màu rồi cho nó thêm vào trong bảng màu được cung cấp, gọi là Windows Palette bằng cách click button Add to Custom Colors. - Dùng Dialogs trong visual basic

m.

ột màu rồi cho nó thêm vào trong bảng màu được cung cấp, gọi là Windows Palette bằng cách click button Add to Custom Colors Xem tại trang 9 của tài liệu.
Bạn tạo ra một màu bằng cách click chỗ có màu theo ý trong bảng màu lớn hình vuông rồi nắm hình tam giác bên phải kéo lên, kéo xuống để thay đổi độ đậm của màu như hiển thị trong hộp vuông Color| - Dùng Dialogs trong visual basic

n.

tạo ra một màu bằng cách click chỗ có màu theo ý trong bảng màu lớn hình vuông rồi nắm hình tam giác bên phải kéo lên, kéo xuống để thay đổi độ đậm của màu như hiển thị trong hộp vuông Color| Xem tại trang 9 của tài liệu.

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan