Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you fill a listbox only with directory names? 1

Status
Not open for further replies.

emaduddeen

Programmer
Mar 22, 2007
184
US
Hi Everyone,

Can you show me the code needed to fill a listbox with only directory (folder) names?

Thanks.

Truly,
Emad
 
Hi Everyone,

I found out how to get the folder name into a listbox.

Here's the code I used:

Code:
    Private Sub btnMaintenance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaintenance.Click
        strDestinationDrive = Nothing

        FindThumbDrive()

        If strDestinationDrive <> Nothing Then ' A drive letter was assigned.
            Try

                frmMaintenance.lstBackupFolders.Items.Clear()

                ' Put the folder names from the thumb drive into the listbox
                ' prior to showing the maintenance form.
                '-----------------------------------------------------------
                For Each strFolder As String In _
                    IO.Directory.GetDirectories(strDestinationDrive)
                    
                    frmMaintenance.lstBackupFolders.Items.Add(strFolder)
                Next

                frmMaintenance.ShowDialog()

            Catch ex As Exception
                ' Something went wrong so let the user know about it.
                '----------------------------------------------------
                MessageBox.Show("Ooooooooops, I couldn't finish. " & vbCrLf & _
                    "Please get in touch with us so we can help you." & vbCrLf & vbCrLf & _
                    "Please let us know about this message: " & vbCrLf & vbCrLf & _
                     ex.Message, "Ooooooooppppppppsssssss. Something Went Wrong", _
                     MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

Truly,
Emad
 
What you did is normally the best way to handle that sort of situation using a for each and just .Add -ing. If you don't need to do anything else then since it is a ListBox and you are dealing with an array of strings you could also do this:

Code:
    Private Sub btnMaintenance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaintenance.Click
        strDestinationDrive = Nothing

        FindThumbDrive()

        If strDestinationDrive <> Nothing Then ' A drive letter was assigned.
            Try

                frmMaintenance.lstBackupFolders.Items.Clear()

                ' Put the folder names from the thumb drive into the listbox
                ' prior to showing the maintenance form.
                '-----------------------------------------------------------
                Dim alldir As String() = IO.Directory.GetDirectories(strDestinationDrive)
                frmMaintenance.lstBackupFolders.Items.AddRange(alldir)

                frmMaintenance.ShowDialog()

            Catch ex As Exception
                ' Something went wrong so let the user know about it.
                '----------------------------------------------------
                MessageBox.Show("Ooooooooops, I couldn't finish. " & vbCrLf & _
                    "Please get in touch with us so we can help you." & vbCrLf & vbCrLf & _
                    "Please let us know about this message: " & vbCrLf & vbCrLf & _
                     ex.Message, "Ooooooooppppppppsssssss. Something Went Wrong", _
                     MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub
Of course if you wanted to do more with each directory string then you would need to keep the for each.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi Sorwen,

Thanks for the code.

I will try your version later since it uses less code.

Truly,
Emad
 
NP. It isn't any better per say (it might end up infinitesimally quicker if a really large list), but wanted you to know you have the option available.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top