Nếu bạn tìm kiếm Dialog Statement trong MapBasic Help, bạn sẽ thấy mục đích của nó là tạo các hộp thoại tùy chỉnh. Trong một bài viết trước, chúng tôi đã sử dụng câu lệnh Note, đây là câu lệnh cơ bản nhất trong các hộp thoại tiêu chuẩn MapInfo. Phần Trợ giúp trên các hộp thoại cho biết: “The Dialog statement lets you create a custom dialog box. If you want to display a standard dialog box (for example, a File>Open dialog box), use one of the following statements or functions: Ask() function, Note statement, ProgressBar statement, FileOpenDlg() function, FileSaveAsDlg() function, or GetSeamlessSheet() function).”
Hàm Ask() hiển thị hộp thoại hỏi người dùng câu hỏi có hay không.
Ví dụ:
Dim more As Logical
more = Ask(“Do you want to continue?”, “OK”, “Stop”)
Xem thêm đoạn code sau:
Include “MAPBASIC.DEF”
Include “ICONS.DEF”
Include “MENU.DEF”
Declare Sub Main
Declare Sub About
Declare Sub End_Program
Define AppName “Ask Function Example”
Define AppVersion 1.0
‘Defining Dialog Width and Height
‘This allows you to specify width and height clauses in terms of characters (i.e., Width 30dW, Height 10dH).
Define dW *4 ‘Four dialog units equals one character in width
Define dH *8 ‘Eight dialog units equals one character in height
‘********************
Sub Main
‘*********************
Call About
End Sub Main
‘********************
Sub About
‘********************
Dialog
Title “About ” + AppName + ” (Version ” + AppVersion + “)”
Control StaticText title “This simple application demonstrates how the Ask function works.” Position 3dW, 1dH
Control StaticText title “To see how it works:” Position 3dW, 3dH
Control StaticText title “- Look at the Sub End_Program section of the code” Position 3dW, 4dH
Control StaticText title ” You have the option of closing the application ” Position 3dW, 5dH
Control StaticText title “If you select Cancel the program closes” Position 3dW, 7dH
Control StaticText title “If you select Exit the application will display the Ask dialogue box” Position 3dW, 8dH
Control StaticText title “This gives you the option of cancelling or continuing the program” Position 3dW, 9dH
Control StaticText title “This is a simple example of how to use the Ask() function” Position 3dW, 10dH
Control StaticText title Chr$(169) + ” 2016 – Joe Short” Position 50dW, 14dH
Control OKButton Title “&Exit” Calling End_Program
Control CancelButton
End Sub About
‘********************
Sub End_Program
‘********************
Dim Finished as Logical
Finished = Ask(“Are you sure you want to remove the application ” + AppName + “?”, “&Yes”, “&No”)
If not Finished then Dialog Preserve
Else
End Program
End if
End Sub End_Program
‘*********************
Đọan code gọi thủ tục Sub About hiển thị hộp thoại chứa tiêu đề và ba loại điều khiển (control): StaticText, OKButton và CancelButton.

Một số điểm cần lưu ý là cách xác định các câu lệnh Define cho phép cập nhật các thông tin thông qua biến mà không cần phải thực hiện nhiều thay đổi trong mã code. Tiêu đề “About ” + AppName + ” (Version ” + AppVersion + “)” và dW và dH là những ví dụ về cách sử dụng điều này. Biểu tượng bản quyền © được tạo bằng cách sử dụng Chr $ (169) trong đoạn lệnh ở trên.
Nếu bạn nhấp vào nút Exit, hộp thoại sẽ xuất hiện như dưới đây:

Nhấp vào nút No sẽ đưa bạn trở lại hộp thoại thông tin. Nhấp vào nút Yes sẽ xóa chương trình khỏi MapInfo.
Nếu bạn tra cứu câu lệnh Dialog Preserve trong MapBasic Help sẽ thấy đạon mã code như sau:
Sub confirm_cancel
If Ask(“Do you really want to lose your changes?”,
“Yes”, “No” ) = FALSE Then
Dialog Preserve
End If
End Sub
Mã này kiểm tra xem người dùng có muốn mất bất kỳ thay đổi nào không. Nếu phản hồi là No (FALSE) thì câu lệnh Dialog Preserve sẽ kích hoạt lại hộp thoại gốc. Để biết thêm thông tin các lệnh trên, xem câu lệnh Dialog trong MapBasic Help.
Bình luận bằng Facebook Comments