有没有办法从 vb .net 中的子例程中提取变量?或者我如何声明自定义事件处理程序并以链接方式触发它们?

Is there a way to extract a variable from a subroutine in vb .net? or how do I declare custom event handlers and trigger them in a linked fashion?

我正在尝试在 VB.Net 中构建此文件复制实用程序,并且我有此 window:

当前文件夹按钮会打开一个文件夹浏览器对话框,我将从对话框中选择的路径保存到一个字符串变量中,然后将其传递给一个函数。该函数将该文件夹中存在的所有文件和目录添加到当前文件夹列表框中。

现在我需要所有文件复选框的文件路径,触发时会列出当前文件夹列表框中的所有子目录及其内容。

有什么方法可以动态提取文件路径变量而无需对其进行硬编码?或者我可以创建自定义事件处理程序并在当前文件夹按钮处理程序中触发它们吗?

这是我的代码:

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

如有任何帮助,我们将不胜感激。

那么,对于列表侧 lbox 和右侧 lbox?

为什么不用数据源填充每个列表框?您可以有 1 列、2 列,甚至 5 列数据。 ListBox 可以显示两列。

因此,列表框通常会有两个值。基于你select的“值”(通常是PK数据库行值),然后你有显示的“文本”值。

来自 FoxPro、ms-access、vb.net,甚至 asp.net?

传统的列表框能够为您的 selection“存储”一组值。

那么,为什么不直接将文件列表放到一个动态的“数据结构”中呢?您可以使用结构、class 或其他任何东西。

但是,还不如使用数据 table,因为列表框支持“绑定”到 table。

因此,在 ListBox 设置中,您有以下两个设置:

所以上面是我们的“展示”

然后将“值”设置为完整文件名,如下所示:

所以现在我们可以说创建一个这样的表单:

因此,我们 select“来自文件夹”的代码如下所示:

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

因此,我们设置了一个两列的“事物”(在本例中为数据 table)。

我们用我们的两个值(FileName 和 FullFile)填充它。

所以,我们现在有这个:

我在左侧 select 编辑了两个文件,因此我们得到:

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

输出:

a.pdf
C:\Test2\a.pdf
b.pdf
C:\Test2\b.pdf

我们甚至可以在 table 中包含文件大小。但是这里的“基本”概念是我们可以将数据项存储+保存到列表框中,这确实使代码变得简单,因为列表框现在只有要显示的文件,而且还使我们能够拥有完整路径名字也。