HOW TO:使用 DocumentList 控制項

更新:2007 年 11 月

如果應用程式是以使用檔案為主,則您可以使用 DocumentList 控制項,在 [我的文件] 資料夾中顯示自訂的資料夾和檔案清單。這個方式類似於 Microsoft Pocket Word 和 Microsoft Pocket Excel 的操作方式。此控制項會提供下列功能給使用者:

  • 選取、刪除、複製、移動和重新命名檔案及資料夾。

  • 依檔案名稱、日期或大小排序。

  • 以電子郵件附件形式傳送檔案。

  • 透過紅外線將檔案傳送至另一個裝置。

若要實作 DocumentList 控制項

  1. DocumentList 建立 Pocket PC Windows 應用程式。

  2. 指定可以用 Filter 屬性存取的檔案類型。

  3. 指定最初是以 FilterIndex 屬性顯示的檔案。

  4. SelectedDirectory 屬性指定預設資料夾。

  5. 提供用來處理 DocumentActivated 事件的程式碼。

  6. 提供用來處理 DeletingDocument 事件的程式碼。

  7. 提供用來處理 SelectedDirectoryChanged 事件的程式碼。

範例

這個範例會針對表單的 DocumentList 控制項設定 Parent 屬性,使其佔用表單的整個工作區 (Client Area)。如果您要使該控制項佔用較小的區域,可以將這個控制項放在 Panel 中並指定其長度。DocumentList 的寬度應為表單的寬度。

 ' Set up file extension filters for a
 ' DocumentList and set the initial folder
 ' to the Busines folder under My Documents.
 Sub SetupDocList()

     ' Assumes an instance of DocumentList,
     ' documentList1, has been declared.
     With DocumentList1
         .Parent = Me
         .Filter = " |*.*| |*.txt;*.xml| |*.pwi;*.pdt| " & _
             "|*.pxl;*.psw| |*.jpg;*.gif;*.bmp| |*.wav;*.wmv;*.mpg;"
         .FilterIndex = 0
         .SelectedDirectory = "Business"
     End With

 End Sub

' Handle the DocumentedActivated
' event with code to open the file.
 Private Sub DocList_DocumentActivated(ByVal sender As Object, _
     ByVal docevent As Microsoft.WindowsCE.Forms.DocumentListEventArgs) _
     Handles DocumentList1.DocumentActivated

     StatusBar1.Text = "Activated: " & docevent.Path
 ' Add code to open the selected file.

 End Sub

 ' Handle the DeletingDocument 
 ' event with code to close the file.
 Private Sub DocList_DeletingDocument(ByVal sender As Object, _
     ByVal docevent As Microsoft.WindowsCE.Forms.DocumentListEventArgs) _
     Handles DocumentList1.DeletingDocument

     StatusBar1.Text = "Deleted: " & docevent.Path
     ' Add code to close any instances of the file.

 End Sub

 ' Handle the SelectedDirectoryChanged
 ' event with code that sets the correct
 ' path for opening and closing files.
 Private Sub DocList_SelectedDirectoryChanged( _
     ByVal sender As Object,  ByVal e As System.EventArgs) _
     Handles DocumentList1.SelectedDirectoryChanged

     StatusBar1.Text = "Folder: " & DocumentList1.SelectedDirectory
     ' Add code to access the selected folder to open and close files.    

 End Sub
// Set up file extension filters for a
// DocumentList and set the initial folder
// to the Busines folder under My Documents.
 private void SetupDocList()
 {

     // Assumes an instance of DocumentList,
     // documentList1, has been declared.
     documentList1.Parent = this;

     // Create event handlers for DocumentList events.
     documentList1.DocumentActivated +=
         new DocumentListEventHandler(this.OnDocActivated);

     documentList1.SelectedDirectoryChanged +=
         new EventHandler(this.OnFolderSel);

     documentList1.DeletingDocument +=
         new DocumentListEventHandler(this.OnDelDoc);

     documentList1.Filter = " |*.*| |*.txt;*.xml| |*.pwi;*.pdt| " +
         "|*.pxl;*.psw| |*.jpg;*.gif;*.bmp| |*.wav;*.wmv;*.mpg;";
     documentList1.FilterIndex = 0;
     documentList1.SelectedDirectory = "Business";
 }

 private void OnDelDoc(object obj, DocumentListEventArgs DocArgs)
 {
     statusBar1.Text += "Deleted: " + DocArgs.Path;

     // Add code to close any instances of the file.
 }

 private void OnDocActivated(object obj, DocumentListEventArgs DocArgs)
 {
     statusBar1.Text = "Activated: " + DocArgs.Path;

     // Add code to open the selected file.
 }
 private void OnFolderSel(object obj, EventArgs eventg)
 {
     statusBar1.Text = "Folder: " + documentList1.SelectedDirectory;

     // Add code to access the selected folder to open and close files.
 }

編譯程式碼

這個範例需要下列命名空間的參考:

請參閱

參考

DocumentList

其他資源

Pocket PC 開發和 .NET Compact Framework