我正在嘗試在 VB.Net 中構建這個檔案復制實用程式,并且我有這個視窗:

當前檔案夾按鈕打開一個檔案夾瀏覽器對話框,我將對話框中的選定路徑保存到一個字串變數,然后我將其傳遞給一個函式。該函式將該檔案夾中存在的所有檔案和目錄添加到當前檔案夾串列框中。
現在我需要所有檔案復選框的這個檔案路徑,當觸發它時會列出當前檔案夾串列框中的所有子目錄及其內容。
有什么方法可以動態提取該檔案路徑變數而無需對其進行硬編碼?或者我可以創建自定義事件處理程式并在當前檔案夾按鈕處理程式中觸發它們嗎?
這是我的代碼:
Public Class Form1
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FB As FolderBrowserDialog = New FolderBrowserDialog()
Dim srcpath As String
Dim flag As Integer = 1
FB.ShowDialog()
FB.ShowNewFolderButton = True
If (DialogResult.OK) Then
srcpath = FB.SelectedPath()
End If
listfiles(srcpath)
End Sub
Public Function listfiles(srcpath As String)
Dim dir As DirectoryInfo = New DirectoryInfo(srcpath)
Dim dirs As DirectoryInfo() = dir.GetDirectories()
Dim d As DirectoryInfo
Dim files As FileInfo() = dir.GetFiles()
Dim file As FileInfo
For Each file In files
CurrentFolderListBox.Items.Add(file.Name)
Next
For Each d In dirs
CurrentFolderListBox.Items.Add(d)
Next
'If CheckBox1.Checked = True Then
' CheckBox1_CheckedChanged(sender, New System.EventArgs())
'End If
End Function
Public Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChange
Dim item As DirectoryInfo
For Each i As DirectoryInfo In CurrentFolderListBox.Items
item = i
Next
End Sub
非常感激任何的幫助。
uj5u.com熱心網友回復:
那么,對于串列側 lbox 和右側 lbox?
為什么不用資料源填充每個串列框?您可以擁有 1、2 甚至 5 列資料。ListBox 可以顯示兩列。
所以,很多時候一個串列框會有兩個值。“值”基于您選擇的內容(通常是 PK 資料庫行值),然后您有“文本”值進行顯示。
來自 FoxPro、ms-access、vb.net 甚至 asp.net?
傳統的串列框能夠“存盤”一組值供您選擇。
那么,為什么不直接將檔案串列放到一個動態的“資料結構”中。你可以非常多地使用結構、類或其他任何東西。
但是,不妨使用資料表,因為串列框支持“系結”到表。
因此,在 ListBox 設定中,您有以下兩個設定:

所以上面是我們的“展示”
然后將“值”設定為完整的檔案名,如下所示:

所以現在我們可以說創建一個這樣的表單:

因此,我們選擇“來自檔案夾”的代碼如下所示:
Private Sub cmdSelFrom_Click(sender As Object, e As EventArgs) Handles cmdSelFrom.Click
Dim f As New FolderBrowserDialog
If f.ShowDialog = DialogResult.OK Then
txtFromFolder.Text = f.SelectedPath
ListBox1.DataSource = GetFileData(txtFromFolder.Text)
End If
End Sub
Public Function GetFileData(sFolder As String) As DataTable
Dim rstData As New DataTable
rstData.Columns.Add("FullFile", GetType(String))
rstData.Columns.Add("FileName", GetType(String))
' get all files from this folder
Dim folder As New DirectoryInfo(sFolder)
Dim fList() As FileInfo = folder.GetFiles
For Each MyFile As FileInfo In fList
Dim OneRow As DataRow = rstData.NewRow
OneRow("FullFile") = MyFile.FullName
OneRow("FileName") = MyFile.Name
rstData.Rows.Add(OneRow)
Next
Return rstData
End Function
so, we setup a two column "thing" (in this case a data table).
We fill it with our two values (FileName and FullFile).
So, we now have this:

I have selected two files on the left side, and thus we get this:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
For Each MySel As DataRowView In ListBox1.SelectedItems
Debug.Print(MySel.Item("FileName"))
Debug.Print(MySel.Item("FullFile"))
Next
End Sub
OutPut:
a.pdf
C:\Test2\a.pdf
b.pdf
C:\Test2\b.pdf
We could even include say file size in that table. But the "basic" concept here is that we can store save data items into the list box, and that REALLY makes the code simple, since the list box now has JUST the file for display, but also enables us to have the full path name also.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/437684.html
