Click here to Skip to main content
15,938,122 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
After what feels like a stupendous amount of google searching, I've drawn a blank on how to do exactly what the title suggests. I tried various examples but to no avail, which leads me to believe that I'm either misinterpreting how the code is supposed to look or missing steps vital to have them display. I'm almost entirely new to coding in general, but even this just stumps me.

Context:

It's a simple "mod manager" tool. Installing the mod creates a folder in Documents, and the listbox lists only the folder names in that directory without their entire path.

How do I achieve this?

What I have tried:

XML
Imports System.IO


Class MainWindow
    Private Sub ListBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
        Dim fileNames = My.Computer.FileSystem.GetFiles("%userprofiles%\Documents\State of Decay 2 Mod Manager")

        For Each fileName As String In fileNames
            Dim result As String = Path.GetFileName(fileName)
            Dim unused = Listbox1.Items.Add(result)
        Next

    End Sub
End Class
Posted
Updated 5-Oct-19 23:05pm

You can enumerate the directories in a specific directory this way:
VB
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq

Public Shared Function GetDirectories(directory As String) As String()
   Dim list As New List(Of String)
   list.AddRange
   (
      Directory.EnumerateDirectories
      (
         Path.Combine
         (
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
            directory
         )
      )
   )
   Return list
      '' We have to add a final backslash to each path
      '' for the GetDirectoryName method to work as expected.
      .Select(p => Path.GetDirectoryName(p & "\"))
      .ToArray()
End Function

Usage:
VB
Dim paths As String() = GetDirectories("State of Decay 2 Mod Manager")

This is for the enumeration part.

Now, there is a remaining issue: why trying to populate the listbox on selected index changed? This event is fired whenever a distinct element in the list is selected. You would better populate this listbox just after the form has loaded, or whenever a change has been made to the contents of the base directory.
 
Share this answer
 
v3
Comments
Rynovz 6-Oct-19 5:05am    
To be perfectly honest, I didn't understand what the ListBox_SelectionChanged instruction did until Richard explained it. This is very much a first attempt at doing this. The idea seems relatively simple, but the implementation feels vastly more complex until I grasp how each line affects what the listbox does. It's very much a limitation of my coding knowledge in general. What would be a better instruction to use, and where would it be placed to have the desired effect?
phil.o 6-Oct-19 5:10am    
Well, it depends on the flow of your program. You could populate the listbox just after the form has loaded (for example, in the Form.Activated event), or whenever some changes have been made to the contents of the base directory.
Rynovz 6-Oct-19 5:22am    
It would be best to have the listbox update whenever changes to the base directory is made. When I copied your code above, I drew a blank as to where to paste it. Do I double click on the listbox and paste the code there? Or on the main window itself?
phil.o 6-Oct-19 5:45am    
You can place the shared function inside your MainWindow class. The Imports instructions usually go to the very beginning of the code file.
Then you can use this shared function from wherever you decided to implement the directory-listing.

If you double click on the listbox, it will only add an event-handler for the default event for the ListBox class, which is not what you want.
Maybe you need a quick overview of classes and functions, or of what is a Shared element?
You are not capturing the filenames until the ListBox_SelectionChanged event is called. And that will not be called until a selection is made to an item in the Listbox, which is currently empty. You should populate the ListBox when the application first runs, i.e in the MainWindow_Load method. Or use a button on the form to start it.

There is even a nice sample code in the documentation: ListBox Class (System.Windows.Forms) | Microsoft Docs[^].
 
Share this answer
 
v2
Comments
Rynovz 6-Oct-19 4:59am    
Thanks for the reply Richard. I've read the documentation and it makes a lot of sense. What I'm confused about now is the MainWindow_Load method you mentioned. I'm not sure how to make a button on the form start the code either. Once I've done it once, I'll have a good grip of it
Richard MacCutchan 6-Oct-19 5:58am    
Open the Form designer Window in Visual Studio and double click in the title part of your form. This will generate the Form_Load method. You can then add your code in there.

You mention WPF in the title of your question so I assumed you were an experienced developer. However, if you are struggling to understand basic concepts of .NET applications then you will find WPF even more difficult. I suggest you go to the CodeProject articles section where you will find many useful tutorials on basic Windows Forms development.
Rynovz 6-Oct-19 15:42pm    
Thank you for the advice. I was told to use WPF as it would make things easier, not more difficult. I appreciate the feedback greatly
Richard MacCutchan 7-Oct-19 4:19am    
Well that depends on what you are trying to do and your skill levels. And as someone new to coding I think you will find WPF far more of a challenge than Windows Forms.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900