Results 1 to 9 of 9

Thread: [RESOLVED] View files in folder based on button

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    57

    Resolved [RESOLVED] View files in folder based on button

    Hey all pretty new at this. Situation I am in. I can already bring up list of files in a director. Issues I am trying to figure out is.

    Form 1

    Button 1 - folder1
    Button 2 - folder2
    Button 3 - folder3

    When a button is pushed then
    Form2.Show() is done

    form2 I have

    Code:
    Dim directory = "c:\dir\"   (i need folder# to insert after the dir\ but not sure how to send it. 
            Dim files() As System.IO.FileInfo
            Dim dirinfo As New System.IO.DirectoryInfo(directory)
            files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
            For Each File In files
                ListBox1.Items.Add(File)
            Next
    So when somebody hit button 1 - 3 it will display whatever files are in that directory in the list box.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: View files in folder based on button

    You’d need to modify the constructor in Form2, so you could pass in the folder path when the form is created. Show me your Form2 code and I can explain how to modify it...

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: View files in folder based on button

    It’ll be something like this...

    Code:
    Public Class Form2
    
        Public folderPath As String
    
        Public Sub New(ByVal folderPath As String)
            Me.folderPath = folderPath
        End Sub
    
        ‘ your code here. Me.folderPath is your directory path
    
    End Class
    Then to call it...

    Code:
    Dim frm As New Form2(“c:\aDirectory”)
    frm.Show

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: View files in folder based on button

    Actually, I missed a line. The first line in sub new should be...

    Code:
    InitializeComponent

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    57

    Re: View files in folder based on button

    Thank paul. This is what I currently have in my form 2

    Code:
    Public Class EntryLogs
        Private Sub EntryLogs_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim directory = "c:\elagent\tpia\"  (The tpia is one of the folders I need to be able to change from what is chosen in form 1
            Dim files() As System.IO.FileInfo
            Dim dirinfo As New System.IO.DirectoryInfo(directory)
            files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
            For Each File In files
                ListBox1.Items.Add(File)
            Next
        End Sub
    Form 1 code is:

    Code:
        Private Sub ViewDailyLogToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles ViewDailyLogToolStripMenuItem2.Click
            EntryLogs.Show()
            'opendir = "tpia"
        End Sub
    
        Private Sub ViewDailyLogToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ViewDailyLogToolStripMenuItem1.Click
            EntryLogs.Show()
            'opendir = "chat"
        End Sub
    
        Private Sub ViewDailyLogToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewDailyLogToolStripMenuItem.Click
            EntryLogs.Show()
            'opendir = "phone"
        End Sub
    As you can tell I currently have opendir marked out due to errors. So I need to have the tpia change to chat, phone, etc.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: View files in folder based on button

    Check post #3 and post #4

    You just need to add in my code, then open EntryLogs as an instance as I showed in post #3

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    57

    Re: View files in folder based on button

    Thanks Paul it works perfect.

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2019
    Posts
    57

    Re: [RESOLVED] View files in folder based on button

    Hey paul sorry for the bother.
    Just finishing up the program I am working and was wondering with the code you provided is there a way to reverse the order of the list in the listbox, I have tried the default listbox1.items.reverse with no luck except for breaking it. If not no worries. I am sure the people are using it know how to scroll to the bottom of the list...lol

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] View files in folder based on button

    Firstly, you should not be doing this:
    vb.net Code:
    1. For Each File In files
    2.     ListBox1.Items.Add(File)
    3. Next
    Either call AddRange to add the lot in one go or else set the DataSource to bind.

    As for reversing, files is an array and there is an Array.Reverse method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width