Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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)

Posted

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.

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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.

Posted
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).
Anybody looking for a graduate programmer (Midlands, England)?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...