1 Attachment(s)
[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.
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.
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.
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?
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:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1().Items.Clear()
Dim f As New DirectoryInfo("C:\dell")
Dim dirs() As DirectoryInfo = f.GetDirectories()
For Each d As DirectoryInfo In dirs
ListBox1.Items.Add(d)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim sString As String = ListBox1.SelectedItem.FullName
'or = ListBox1.SelectedItem.root which will yield C:\
'or = ListBox1.SelectedItem.parent which will yield dell
'or = ListBox1.SelectedItem.name which willyield drivers
'drivers is what will appear in the listbox and what you click on
MsgBox(sString)
End Sub
End Class
http://i755.photobucket.com/albums/x...12/dirinfo.jpg
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!
Re: GetDirectories and Listbox's help
Quote:
Originally Posted by
blacksaibot
[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. :lol:
p.s.
I cant get your last code to work.
http://www.vbforums.com/showpost.php...67&postcount=5
Re: GetDirectories and Listbox's help
Quote:
Originally Posted by
sRLS
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
Re: GetDirectories and Listbox's help
Quote:
Originally Posted by
sRLS
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. :lol:
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!
Re: GetDirectories and Listbox's help
blacksaibot
Unfortunately our firewall blocks your attachment.
Re: GetDirectories and Listbox's help
Well that sucks...
The picture is not important... the code example is.
Re: GetDirectories and Listbox's help
Ok, Edgemeal's example works quite well. But it drills down and down and down. :lol:
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.
Re: GetDirectories and Listbox's help
Quote:
Originally Posted by
sRLS
Ok, Edgemeal's example works quite well. But it drills down and down and down. :lol:
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.
Re: GetDirectories and Listbox's help
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
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.