Results 1 to 15 of 15

Thread: How to get combo box to show .exe files and not full path

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    8

    How to get combo box to show .exe files and not full path

    Apologies in advance I am very new to all this and working on my first project. I am trying to get the exe filename only to show and not the full path to it. for example doom.exe. Thanks in advance.

    This is what I have so far

    Code:
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim directory = "D:\Steam Library"
            Dim files() As System.IO.FileInfo
            Dim dirinfo As New System.IO.DirectoryInfo(directory)
            files = dirinfo.GetFiles("*.exe", IO.SearchOption.AllDirectories)
    
            For Each file In files
                cmbSelectGame.Items.Add(file)
    
    
            Next
    
        End Sub
    Last edited by dday9; Jan 24th, 2021 at 01:18 PM.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: How to get combo box to show .exe files and not full path

    You are working with an object of type System.IO.FileInfo.

    Google search for that, and you will find the Microsoft documentation all about System.IO.FileInfo, and you should be able to find an appropriate Property that gives you the result you are looking for.

    It might seem rude for me to not just tell you the answer, but learning to find answers yourself will be much more valuable for you in the long run.

    Good luck.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    8

    Re: How to get combo box to show .exe files and not full path

    Quote Originally Posted by OptionBase1 View Post
    You are working with an object of type System.IO.FileInfo.

    Google search for that, and you will find the Microsoft documentation all about System.IO.FileInfo, and you should be able to find an appropriate Property that gives you the result you are looking for.

    It might seem rude for me to not just tell you the answer, but learning to find answers yourself will be much more valuable for you in the long run.

    Good luck.
    Thank you will search it up now

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to get combo box to show .exe files and not full path

    Also, don't add items in a loop like that. At the very least, add all items in one go with a single call to AddRange. Better still, bind the data by setting the DisplayMember, ValueMember and DataSource properties. That way, you can display the file names but, when the user selects one, get the full path from the SelectedValue. I'm not sure exactly what you want to do with the selection but, in most cases, you're going to need the full path of a file to do anything useful.

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    8

    Re: How to get combo box to show .exe files and not full path

    Quote Originally Posted by OptionBase1 View Post
    You are working with an object of type System.IO.FileInfo.

    Google search for that, and you will find the Microsoft documentation all about System.IO.FileInfo, and you should be able to find an appropriate Property that gives you the result you are looking for.

    It might seem rude for me to not just tell you the answer, but learning to find answers yourself will be much more valuable for you in the long run.

    Good luck.
    took me long enough to work out to add.name after file but thank you for not giving me the answer

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: How to get combo box to show .exe files and not full path

    Quote Originally Posted by Joshyb View Post
    took me long enough to work out to add.name after file but thank you for not giving me the answer


    Becoming familiar with Microsoft's documentation is one of the most important things any VB.NET developer can do. Good luck.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    8

    Re: How to get combo box to show .exe files and not full path

    I am making a games launcher so was searching for the .exe files however it is pulling all the exe files so man need to look at another option.

    I have Dim directory = "D:\Steam Library" but i want this to be generated whats in a textbox from a folderbrowserdialog. struggling with this one.

  8. #8
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: How to get combo box to show .exe files and not full path

    If your TextBox is named PathTextBox,

    Code:
    Dim directory As String
    directory = PathTextBox.Text
    To populate PathTextBox.Text with the folder selected from a FolderBrowserDialog, take a look at the documentation below to find the appropriate property to use:

    https://docs.microsoft.com/en-us/dot...g?view=net-5.0

    Good luck.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    8

    Re: How to get combo box to show .exe files and not full path

    Quote Originally Posted by OptionBase1 View Post
    If your TextBox is named PathTextBox,

    Code:
    Dim directory As String
    directory = PathTextBox.Text
    To populate PathTextBox.Text with the folder selected from a FolderBrowserDialog, take a look at the documentation below to find the appropriate property to use:

    https://docs.microsoft.com/en-us/dot...g?view=net-5.0

    Good luck.
    thanks, i can get it to populate into a text box and save but i cant get it to auto complete Dim directory = "***D:\Steam Library\steamapps\common*****"

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: How to get combo box to show .exe files and not full path

    To get the directory that the picked in the FolderBrowserDialog, use the SelectedPath property: https://docs.microsoft.com/en-us/dot...g.selectedpath

    From here you can optionally set the TextBox's Text property: https://docs.microsoft.com/en-us/dot...s.textbox.text

    Finally, you can set the ComboBox's DataSource (documentation), DisplayMember (documentation), and ValueMember (documentation).

    With all of that being said, here is an example:
    Code:
    Using browseDialog = New FolderBrowserDialog()
        If (browseDialog.ShowDialog() = DialogResult.Ok) Then
            MyTextBox.Text = browseDialog.SelectedPath
    
    
            Dim selectedDirectory = New System.IO.DirectoryInfo(browseDialog.SelectedPath)
            With MyComboBox
                .DisplayMember = "Name"
                .ValueMember = "FullName"
                .DataSource = selectedDirectory.GetFiles("*.exe", IO.SearchOption.AllDirectories)
            End With
        End If
    End Using
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    8

    Re: How to get combo box to show .exe files and not full path

    Quote Originally Posted by dday9 View Post
    To get the directory that the picked in the FolderBrowserDialog, use the SelectedPath property: https://docs.microsoft.com/en-us/dot...g.selectedpath

    From here you can optionally set the TextBox's Text property: https://docs.microsoft.com/en-us/dot...s.textbox.text

    Finally, you can set the ComboBox's DataSource (documentation), DisplayMember (documentation), and ValueMember (documentation).

    With all of that being said, here is an example:
    Code:
    Using browseDialog = New FolderBrowserDialog()
        If (browseDialog.ShowDialog() = DialogResult.Ok) Then
            MyTextBox.Text = browseDialog.SelectedPath
    
    
            Dim selectedDirectory = New System.IO.DirectoryInfo(browseDialog.SelectedPath)
            With MyComboBox
                .DisplayMember = "Name"
                .ValueMember = "FullName"
                .DataSource = selectedDirectory.GetFiles("*.exe", IO.SearchOption.AllDirectories)
            End With
        End If
    End Using

    Thank you very much!

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to get combo box to show .exe files and not full path

    Quote Originally Posted by Joshyb View Post
    Thank you very much!
    That's exactly what I implying in post #4 that you apparently ignored.

  13. #13

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    8

    Re: How to get combo box to show .exe files and not full path

    Quote Originally Posted by jmcilhinney View Post
    That's exactly what I implying in post #4 that you apparently ignored.
    Apologies I wasn't ignoring, I didn't fully understand. Like I say this is the first time ive done anything like this.

  14. #14

    Thread Starter
    New Member
    Join Date
    Jan 2021
    Posts
    8

    Re: How to get combo box to show .exe files and not full path

    Quote Originally Posted by dday9 View Post
    To get the directory that the picked in the FolderBrowserDialog, use the SelectedPath property: https://docs.microsoft.com/en-us/dot...g.selectedpath

    From here you can optionally set the TextBox's Text property: https://docs.microsoft.com/en-us/dot...s.textbox.text

    Finally, you can set the ComboBox's DataSource (documentation), DisplayMember (documentation), and ValueMember (documentation).

    With all of that being said, here is an example:
    Code:
    Using browseDialog = New FolderBrowserDialog()
        If (browseDialog.ShowDialog() = DialogResult.Ok) Then
            MyTextBox.Text = browseDialog.SelectedPath
    
    
            Dim selectedDirectory = New System.IO.DirectoryInfo(browseDialog.SelectedPath)
            With MyComboBox
                .DisplayMember = "Name"
                .ValueMember = "FullName"
                .DataSource = selectedDirectory.GetFiles("*.exe", IO.SearchOption.AllDirectories)
            End With
        End If
    End Using
    This worked for when using the browse folder button however I want the user to be able to save the steam library path and be able to select the game from launch game combo box. When user loads back in they have to select browse again. Not sure if my idea is possible or where I would start. Ill add a photo of the project below to see how I have set it up. Thanks.


    Name:  7pXf5a325aafc.jpg
Views: 134
Size:  14.6 KB

  15. #15
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: How to get combo box to show .exe files and not full path

    You can create a setting: https://docs.microsoft.com/en-us/dot...ettings-object

    Once the user picks the folder using the dialog, you would add an extra step in the code that I provided where you persist the setting value: https://docs.microsoft.com/en-us/dot...-user-settings

    Finally in the form load event, you would get the setting value and basically run the same code that I already provided: https://docs.microsoft.com/en-us/dot...ation-settings
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width