-
Jan 24th, 2021, 10:00 AM
#1
Thread Starter
New Member
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.
-
Jan 24th, 2021, 11:49 AM
#2
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.
-
Jan 24th, 2021, 11:52 AM
#3
Thread Starter
New Member
Re: How to get combo box to show .exe files and not full path
 Originally Posted by OptionBase1
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
-
Jan 24th, 2021, 07:49 PM
#4
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.
-
Jan 25th, 2021, 12:24 PM
#5
Thread Starter
New Member
Re: How to get combo box to show .exe files and not full path
 Originally Posted by OptionBase1
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
-
Jan 25th, 2021, 12:26 PM
#6
Re: How to get combo box to show .exe files and not full path
-
Jan 25th, 2021, 12:56 PM
#7
Thread Starter
New Member
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.
-
Jan 25th, 2021, 02:12 PM
#8
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.
-
Jan 25th, 2021, 03:54 PM
#9
Thread Starter
New Member
Re: How to get combo box to show .exe files and not full path
 Originally Posted by OptionBase1
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*****"
-
Jan 25th, 2021, 04:37 PM
#10
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
-
Jan 25th, 2021, 05:04 PM
#11
Thread Starter
New Member
Re: How to get combo box to show .exe files and not full path
 Originally Posted by dday9
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!
-
Jan 25th, 2021, 08:25 PM
#12
Re: How to get combo box to show .exe files and not full path
 Originally Posted by Joshyb
Thank you very much!
That's exactly what I implying in post #4 that you apparently ignored.
-
Jan 26th, 2021, 04:10 AM
#13
Thread Starter
New Member
Re: How to get combo box to show .exe files and not full path
 Originally Posted by jmcilhinney
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.
-
Jan 26th, 2021, 04:17 AM
#14
Thread Starter
New Member
Re: How to get combo box to show .exe files and not full path
 Originally Posted by dday9
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.
-
Jan 26th, 2021, 09:14 AM
#15
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|