bjwade62 Posted February 15, 2006 Posted February 15, 2006 I'm trying to add folders to a listview control. I've tried the following code with no success. Any help? lvwLeftList.Items.Add(IO.Directory.GetDirectories(cmboLeft.Text) Quote
Cags Posted February 15, 2006 Posted February 15, 2006 I don't quite understand what that line of code is supposed todo, but based on your description this is what I think you want todo. Dim dirs() As String = Directory.GetDirectories(cmboLeft.Text) lvwLeftList.BeginUpdate() For i As Single = 0 To dirs.Length - 1 lvwLeftList.Items.Add(dirs(i)) Next lvwLeftList.EndUpdate() As a side note the code you posted doesn't work because of the following... - the method GetDirectories returns a string array - the method Items.Add() accepts (among other things) a string not an array Pay close attention to the Tooltips for each overload to see what they return or accept, this could help you solve alot of your problems on your own. Quote Anybody looking for a graduate programmer (Midlands, England)?
bjwade62 Posted February 15, 2006 Author Posted February 15, 2006 I ended up using the following code. What's the difference between the two? Yours and the one I used. Dim intCharCount As Integer lvwLeftList.EndUpdate() For Each strDirectory As String In System.IO.Directory.GetDirectories(cmboLeft.Text) Me.lvwLeftList.Items.Add(strDirectory.Substring(intCharCount)) Next lvwLeftList.EndUpdate() I don't quite understand what that line of code is supposed todo, but based on your description this is what I think you want todo. Dim dirs() As String = Directory.GetDirectories(cmboLeft.Text) lvwLeftList.BeginUpdate() For i As Single = 0 To dirs.Length - 1 lvwLeftList.Items.Add(dirs(i)) Next lvwLeftList.EndUpdate() As a side note the code you posted doesn't work because of the following... - the method GetDirectories returns a string array - the method Items.Add() accepts (among other things) a string not an array Pay close attention to the Tooltips for each overload to see what they return or accept, this could help you solve alot of your problems on your own. Quote
Cags Posted February 15, 2006 Posted February 15, 2006 There isn't much difference really. My code stored the search into an array then iterated through this array. Your code uses a for each statement to iterate through without assigning it to an array first. Quite why your using Substring I can't say, you are basically telling it to use the entire string anyway (at least in the code you posted). Quote Anybody looking for a graduate programmer (Midlands, England)?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.