Results 1 to 15 of 15

Thread: [RESOLVED] GetDirectories and Listbox's help

  1. #1

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Resolved [RESOLVED] GetDirectories and Listbox's help

    I have this code that populates a Listbox. (works fine).
    Code:
        Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
            ListBox1.Visible = True
            TextBox1.Visible = False
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            Label2.Text = ""
            Label10.Text = ""
            Button8.Visible = True
            Button9.Visible = False
    
            ListBox1.Items.Clear()
            'The Fire Snake, @ http://www.vbforums.com/showpost.php?p=3756163&postcount=6
            Dim f As New DirectoryInfo("F:\CNC Mill\#19 CNC CMM\Inspection Reports\")
            Dim dirs() As DirectoryInfo = f.GetDirectories()
    
            For Each d As DirectoryInfo In dirs
                ListBox1.Items.Add(d)
            Next
        End Sub
    Once the Listbox is populated I want to select from it and re-populate the same Listbox with new info.
    Code:
        Private Sub listbox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
            RadioButton4.Checked = False
            sLb1SelectedFirst = ListBox1.SelectedItem
            ListBox1.Items.Clear()
            RadioButton4.Visible = True
            ListBox1.Items.Clear()
            Dim f As New DirectoryInfo("F:\CNC Mill\#19 CNC CMM\Inspection Reports\" & sLb1SelectedFirst & "\")
            Dim dirs() As DirectoryInfo = f.GetDirectories()
    
            For Each d As DirectoryInfo In dirs
                ListBox1.Items.Add(d)
            Next
        End Sub
    Only problem is I can not figure out how to pass the listbox selected item to the new path. The "&" is not allowed in my example (code error on my part).
    See attached image.

    Assume all variables are declared.
    Any hints, tips or examples are appreciated.
    Attached Images Attached Images  
    Last edited by sRLS; Feb 15th, 2011 at 12:03 PM.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  2. #2
    Lively Member blacksaibot's Avatar
    Join Date
    Jan 2009
    Location
    East Coast, USA
    Posts
    94

    Re: GetDirectories and Listbox's help

    what data type is sLB1SelectedFirst???

    if it's a string it should work because I just tested doing "sLb1SelectedFirst = ListBox1.SelectedItem" and it works fine...


    I don't understand why you get that error... so I would assume sLB1SelectedFirst is not a String to begin with. - nevermind, your listbox items are not of a string type. they're DirInfos.
    Last edited by blacksaibot; Feb 15th, 2011 at 12:35 PM. Reason: Being more helpful.

  3. #3

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: GetDirectories and Listbox's help

    Thats my problem, I cant figure out what to set it as.
    Right now it is a string.
    The first letter in variables I create always start with a designator for data type.
    IE: sLB1SelectedFirst is a string
    Just something I do to help me out.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  4. #4
    Lively Member blacksaibot's Avatar
    Join Date
    Jan 2009
    Location
    East Coast, USA
    Posts
    94

    Re: GetDirectories and Listbox's help

    It has to be a String since you're concatenating it with a string and storing it as one in your code.

    dim sLB1SelectedFirst as String = ""

    can you post code where ever you have that variable?

    Does this change anything?
    sLb1SelectedFirst = ListBox1.SelectedItem.ToString

    It sounds like the contents of the listbox1 is not even a string datatype because it's trying to convert FROM directoryinfo INTO a string.
    You are adding directoryinfos into your listbox that are not string datatypes (you're first block of code)


    FORGET ALL THAT TRASH ABOVE.

    Try doing this.

    sLb1SelectedFirst = ListBox1.SelectedItem.FullName
    This will give the full file path as a string. However, it looks like you don't want the full path within concatenation... is it just the file name you want?
    Last edited by blacksaibot; Feb 15th, 2011 at 12:21 PM.

  5. #5
    Lively Member blacksaibot's Avatar
    Join Date
    Jan 2009
    Location
    East Coast, USA
    Posts
    94

    Re: GetDirectories and Listbox's help

    I did this and it works without error. Am I going about it incorrectly? I know it's not exactly like your posted code, but... just for example's sake.

    vb Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.  
    7.         ListBox1().Items.Clear()
    8.         Dim f As New DirectoryInfo("C:\dell")
    9.         Dim dirs() As DirectoryInfo = f.GetDirectories()
    10.  
    11.         For Each d As DirectoryInfo In dirs
    12.             ListBox1.Items.Add(d)
    13.         Next
    14.  
    15.     End Sub
    16.  
    17.  
    18.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    19.  
    20.         Dim sString As String = ListBox1.SelectedItem.FullName
    21.         'or = ListBox1.SelectedItem.root    which will yield C:\
    22.         'or = ListBox1.SelectedItem.parent  which will yield dell
    23.         'or = ListBox1.SelectedItem.name    which willyield drivers
    24.         'drivers is what will appear in the listbox and what you click on
    25.  
    26.         MsgBox(sString)
    27.  
    28.     End Sub
    29.  
    30. End Class




    When I click the "drivers" directory within the listbox, it will give me the full file path of the directory.

    instead of .FullName you can use .Parent (C:\dell), .Root (C:\), or .Name (drivers).

    I am no expert with directoryInfo, but I hope this helps and if anyone else can chime in with better suggestions, please do so!
    Last edited by blacksaibot; Feb 15th, 2011 at 01:11 PM.

  6. #6

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: GetDirectories and Listbox's help

    Quote Originally Posted by blacksaibot View Post
    [I][COLOR="Gray"]It has to be a String since you're concatenating it with a string and storing it as one in your code.


    Does this change anything?
    sLb1SelectedFirst = ListBox1.SelectedItem.ToString
    Sometimes the answer is in plane sight and sometimes the answer is in plane sight, other times the answer is in plane sight but I cant see it.

    That fixed that problem but created another.

    p.s.
    I cant get your last code to work.
    http://www.vbforums.com/showpost.php...67&postcount=5
    Last edited by sRLS; Feb 15th, 2011 at 01:07 PM.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: GetDirectories and Listbox's help

    Quote Originally Posted by sRLS View Post
    IE: sLB1SelectedFirst is a string
    Since its a string have you tried converting it to a string?
    sLb1SelectedFirst = ListBox1.SelectedItem.ToString

    I tried it like this so I could drill down the dir paths also...

    Code:
    Dim sLb1SelectedFirst As String
    Dim LastPath As String
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' RESET DIR
        LastPath = ""
        ListBox1().Items.Clear()
        Dim f As New DirectoryInfo("r:\")
        Dim dirs() As DirectoryInfo = f.GetDirectories()
        For Each d As DirectoryInfo In dirs
            ListBox1.Items.Add(d)
        Next
    End Sub
    
    Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
        sLb1SelectedFirst = ListBox1.SelectedItem.ToString
        ' Combine last selected path to new selected path
        LastPath &= sLb1SelectedFirst & "\"
        ListBox1.Items.Clear()
        Dim f As New DirectoryInfo(IO.Path.Combine("r:\", LastPath))
        Dim dirs() As DirectoryInfo = f.GetDirectories()
        For Each d As DirectoryInfo In dirs
            ListBox1.Items.Add(d)
        Next
    End Sub

  8. #8
    Lively Member blacksaibot's Avatar
    Join Date
    Jan 2009
    Location
    East Coast, USA
    Posts
    94

    Re: GetDirectories and Listbox's help

    Quote Originally Posted by sRLS View Post
    Sometimes the answer is in plane sight and sometimes the answer is in plane sight, other times the answer is in plane sight but I cant see it.

    That fixed that problem but created another.
    Look at my other response (with all the code and the screen shot) also check the other guy's response (I have yet to see exactly what he's done at the moment).

    Still a problem? Post it up!

  9. #9

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: GetDirectories and Listbox's help

    blacksaibot
    Unfortunately our firewall blocks your attachment.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  10. #10
    Lively Member blacksaibot's Avatar
    Join Date
    Jan 2009
    Location
    East Coast, USA
    Posts
    94

    Re: GetDirectories and Listbox's help

    Well that sucks...

    The picture is not important... the code example is.

  11. #11

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: GetDirectories and Listbox's help

    Ok, Edgemeal's example works quite well. But it drills down and down and down.
    I only need to make two selections. The first is controlled (folder is hard coded). The second choice needs to end the drill down affect.
    I think I have myself stuck in an accidental loop.
    RadioButton1_CheckedChanged populates ListBox1 first.
    Code:
        Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
            ListBox1.Visible = True
            TextBox1.Visible = False
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            Label2.Text = ""
            Label10.Text = ""
            Button8.Visible = True
            Button9.Visible = False
    
            ' RESET DIR
            sLastPath = ""
            ListBox1().Items.Clear()
            Dim f As New DirectoryInfo("F:\CNC Mill\#19 CNC CMM\Inspection Reports\")
            Dim dirs() As DirectoryInfo = f.GetDirectories()
            For Each d As DirectoryInfo In dirs
                ListBox1.Items.Add(d)
            Next
        End Sub
    Then Listbox1_Click re-populates from what is selected.
    Code:
        Private Sub Listbox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
            RadioButton4.Checked = False
            sLb1SelectedFirst = ListBox1.SelectedItem.ToString
            RadioButton4.Visible = True
            ListBox1.Items.Clear()
    
            ' Combine last selected path to new selected path
            sLastPath &= sLb1SelectedFirst & "\"
            ListBox1.Items.Clear()
            Dim f As New DirectoryInfo(IO.Path.Combine("F:\CNC Mill\#19 CNC CMM\Inspection Reports\", sLastPath))
            Dim dirs() As DirectoryInfo = f.GetDirectories()
            For Each d As DirectoryInfo In dirs
                ListBox1.Items.Add(d)
            Next
        End Sub
    At that point I want to stop drilling down through the folders.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  12. #12
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: GetDirectories and Listbox's help

    Quote Originally Posted by sRLS View Post
    Ok, Edgemeal's example works quite well. But it drills down and down and down.

    At that point I want to stop drilling down through the folders.
    Maybe just use a counter to limit how many sub folders you can drill down?


    Code:
    ' place at top of class
    Dim SubFolderLimit as Integer = 0 ' to limit number of folders to drill down
    
    ' bla bla bla....
    ' RESET DIR
    sLastPath = ""
    SubFolderLimit = 0   ' reset folder limit 
    ListBox1().Items.Clear()
    Dim f As New DirectoryInfo("F:\CNC Mill\#19 CNC CMM\Inspection Reports\")
    Dim dirs() As DirectoryInfo = f.GetDirectories()
    For Each d As DirectoryInfo In dirs
        ListBox1.Items.Add(d)
    Next
    end sub
    
    Private Sub Listbox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
        RadioButton4.Checked = False
        RadioButton4.Visible = True
    
        SubFolderLimit += 1
    
        ' Allow only one folder to be selected
        If SubFolderLimit > 1 Then
            MessageBox("One sub folder was selected, Please reset!")
            Exit Sub
        End If
    
        ' Combine last selected path to new selected path
         sLb1SelectedFirst = ListBox1.SelectedItem.ToString     
         sLastPath &= sLb1SelectedFirst & "\"
    
        ListBox1.Items.Clear()
        Dim f As New DirectoryInfo(IO.Path.Combine("F:\CNC Mill\#19 CNC CMM\Inspection Reports\",  sLastPath))
        Dim dirs() As DirectoryInfo = f.GetDirectories()
        For Each d As DirectoryInfo In dirs
            ListBox1.Items.Add(d)
        Next
    End Sub
    EDIT replaced some code.
    Last edited by Edgemeal; Feb 15th, 2011 at 03:43 PM. Reason: WARNING AIR CODE

  13. #13

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: GetDirectories and Listbox's help

    A counter!
    Geez!
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  14. #14

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: GetDirectories and Listbox's help

    Thanks for all the help.
    This seems to be working for ListBox1 and a counter.
    Code:
        Private Sub Listbox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
            RadioButton4.Checked = False
            RadioButton4.Visible = True
    
            sLb1SelectedFirst = ListBox1.SelectedItem.ToString
    
            iSubFolderLimit += 1
            ' Allow only one folder to be selected
            If iSubFolderLimit <> 2 Then
                ' Combine last selected path to new selected path
                sLastPath &= sLb1SelectedFirst & "\"
                ListBox1.Items.Clear()
                Dim f As New DirectoryInfo(IO.Path.Combine("F:\CNC Mill\#19 CNC CMM\Inspection Reports\", sLastPath))
                Dim dirs() As DirectoryInfo = f.GetDirectories()
                For Each d As DirectoryInfo In dirs
                    ListBox1.Items.Add(d)
                Next
            End If
        End Sub
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  15. #15

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    175

    Re: [RESOLVED] GetDirectories and Listbox's help

    Minor edit:
    If iSubFolderLimit < 2 Then
    was
    If iSubFolderLimit <> 2 Then

    Double clicking a selection allowed drilling down thru folders.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

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