Adding Folders to a ListView control

bjwade62

Centurion
Joined
Oct 31, 2003
Messages
104
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)
 
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.
Visual Basic:
        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.
 
I ended up using the following code. What's the difference between the two? Yours and the one I used.

Visual Basic:
            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()

Cags said:
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.
Visual Basic:
        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.
 
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).
 
Back
Top