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.
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!
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.
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!
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.
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
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).
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!
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
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!