Search code examples
vb.netwinformseventhandler

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?


I am trying to build this file copy utility in VB.Net, and I have this window: enter image description here

The current folder button opens up a folder browser dialog, and I save the selected path from the dialog to a string variable which I then pass to a function. The function adds all files and directories present in that folder to the current folder listbox.

Now I need this filepath for the all files checkbox, which when triggered lists all the subdirectories and their contents in the currentfolder listbox.

Is there any way I can extract that filepath variable dynamically without hardcoding it? Or can I create custom event handlers and trigger them inside the current folder button handler?

Here is my code:

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

Any help would be most appreciated.


Solution

  • Well, for the list side lbox, and the right side lbox?

    Why not fill each list box with a data source? You can have 1, or 2, or even 5 columns of data. The ListBox can display TWO of the columns.

    So, VERY often a list box will have two values. the "value" based on what you select (often a PK database row value), and then you have the "text" value for display.

    from FoxPro, ms-access, vb.net, and even asp.net?

    A list box traditional has had the ability to "store" a set of values for your selection.

    So, why not just put out the file list to a on the fly "data structure". You can quite much use a struct, a class or whatever.

    However, might as well use a data table, since listbox supports "binding" to a table.

    So, in the ListBox settings, you have these two settings:

    enter image description here

    so above is our "display"

    And then set the "value" to the FULL file name like this:

    enter image description here

    So now we can say create a form like this:

    enter image description here

    So, our code to select the "from folder" can look like this:

    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:

    enter image description here

    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.