Giống như hàm FileOpenDlg(), FileSaveAsDlg() có bốn đối số chuỗi: đường dẫn mặc định, tên mặc định, loại tệp và dấu nhắc hội thoại. Giống như hàm FileOpenDlg() không mở tệp theo cùng cách mà hàm FileSaveAsDlg() không lưu tệp. Hàm FileSaveAsDlg() trả về một chuỗi biểu thị đường dẫn tệp. Người dùng có thể chọn tên mới cho tệp hoặc chọn ghi đè lên tệp hiện có.
Dưới đây là mã code sử dụng hàm FileSaveAsDlg () để lưu tệp với tên mới và sau đó mở bản đồ và trình duyệt cho tệp mới này:
' Program: File Save As Dialog Program.mb ' Purpose: Demonstrate the FileSaveAsDlg() function 'Includes Include "mapbasic.def" 'Declares Declare Sub Main() Declare Sub WorkWithCopyOfTable(ByVal sTarget As String) Define DEF_TABLE_TO_COPY "STATES" Define DEF_NEW_TABLE_NAME "STATES_NEW" ' Procedure: Main() ' Purpose: Main Control Sub Main () Dim sNewTable As String ' Make sure the table to be copied exists. If (FileExists(DEF_TABLE_TO_COPY + ".tab")) Then ' Use FileOpenDlg() to get file path. ' You will use this path to save a copy of the table later. sNewTable = FileSaveAsDlg(ApplicationDirectory$(), DEF_NEW_TABLE_NAME + ".tab", "TAB", "Make copy of " + DEF_TABLE_TO_COPY + ":") ' Verify that user did not click CANCEL. If (sNewTable <> "" ) Then Open Table DEF_TABLE_TO_COPY ' Save old table under a new name and path. Commit Table DEF_TABLE_TO_COPY As sNewTable Close Table DEF_TABLE_TO_COPY ' Call routine to process only the copy. Call WorkWithCopyOfTable(sNewTable) Else ' User clicked Cancel. Note "No name chosen for copy. Exiting program." End If Else ' The call to FileExists() returned False. Note "Table to be copied does not exist." End If End Sub ' Procedure: WorkWithCopyOfTable() ' Purpose: Carry out tasks on table Sub WorkWithCopyOfTable(ByVal sTargetTable As String) ' Work with copied table here. If (sTargetTable <> "") Then Open Table sTargetTable ' Use the TableInfo() function to obtain the tablename sTargetTable = TableInfo(0, TAB_INFO_NAME) Map From sTargetTable ' Open a map window Browse * From sTargetTable ' Open a browser Else Note "No Table Found" End Program End If End Sub
Khi đoạn code này được chạy trong MapInfo, một hộp thoại sẽ mở ra tương tự như sau:

Nếu người dùng quyết định lưu tệp thì MapInfo sẽ mở bảng mới và sau đó tạo bản đồ và trình duyệt cho bảng này.

Hàm FileSaveAsDlg() đã đề cập trước đây không thực sự lưu tệp, thay vào đó, nó cung cấp cho người dùng lựa chọn tên tệp để lưu thông qua hộp thoại. Người dùng có thể chọn tên tệp này hoặc chọn một tệp khác có đường dẫn khác khi sử dụng tùy chọn này. Để lưu dữ liệu dưới tên tệp này do người dùng chọn, chúng ta cần đưa ra câu lệnh Commit Table.
Bình luận bằng Facebook Comments