[RESOLVED] List of file names in combobox
I am trying to populate a combobox with a list of filenames from a selected directory. I am getting the directory OK but the combobox instead of having a list of 8 files max shown when I select the combobox has a full list of all the files selected from that directory.
Code:
Private Sub BrowseButton_Click(sender As System.Object, e As System.EventArgs) Handles BrowseButton.Click
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
TextBox1.Text = FolderBrowserDialog1.SelectedPath
End If
Dim BackupFolder As String = TextBox1.Text
For Each BackupFiles As String In My.Computer.FileSystem.GetFiles(BackupFolder, FileIO.SearchOption.SearchTopLevelOnly, "PigginGifts*.bak")
ComboBox1.Items.Add(IO.Path.GetFileName(BackupFiles))
Next
End Sub
Re: List of file names in combobox
what makes you think there's only 8 files in the folder? the way you have it set up now, your are populating the combo with all the files in that folder. if there are more then 8 files, it will still be added. so, you can limit the number of files in the folder or limit what files should be added
Re: List of file names in combobox
I agree there are more than 8 files in the folder and I am displaying them all but I have the the max download files set to 8 so I wanted the first 8 to show and then be able to scroll through the rest if required. Normally the first one will be the required one.
Re: List of file names in combobox
sorry max dropdown files set to 8.
Re: List of file names in combobox
try this:
vb Code:
Private Sub BrowseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowseButton.Click
If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
TextBox1.Text = FolderBrowserDialog1.SelectedPath
Dim BackupFolder As String = TextBox1.Text
For Each BackupFiles As String In My.Computer.FileSystem.GetFiles(BackupFolder, FileIO.SearchOption.SearchTopLevelOnly, "PigginGifts*.bak").Take(8).ToArray
ComboBox1.Items.Add(IO.Path.GetFileName(BackupFiles))
Next
End If
End Sub
Re: List of file names in combobox
i see what you are saying. there are a couple of ways to do this. the easiest might be create a list collection of all files like this:
Code:
Dim myFileList As New List(Of String)
then fill the list like this:
Code:
For Each BackupFiles As String In My.Computer.FileSystem.GetFiles(BackupFolder, FileIO.SearchOption.SearchTopLevelOnly, "PigginGifts*.bak")
myFIleList.add(IO.Path.GetFileName(BackupFiles))\
Next
Now you have a collection of all files. You can iterate of this list any way you want (8 items at a time, for instance). So, you can have a variable like CurrentCount = 0. Each time a user hits a "next set" button or label, increment the count by 8. then you can use a for loop. (ie for i = CurrentCount to myFIleList.length)
Re: List of file names in combobox
I am not trying to display the files in blocks of 8 but rather like other comboboxes I have used to display files from records where if there are more than 8 items selected then the dropdown box below the combobox has a scroll bar to display the other selected items.
Re: List of file names in combobox
The following is a simulation to show how to set MaxDropDownItems property of the ComboBox without worrying about how to populate the ComboBox.
In the code below I would be in trouble as when the DropDown was open it would extend pass my client area of my display screen. So this means you may or may not have to adjust MaxDropDownItems.
Code:
Private Sub demo()
Dim rand As Random = New Random()
ComboBox1.DataSource = Enumerable.Range(0, 100).Select(Function(i) _
(Chr(Asc("A") + rand.Next(0, 26)))).ToList()
ComboBox1.MaxDropDownItems = ComboBox1.Items.Count - 1
End Sub
Re: List of file names in combobox
I have now resolved my problem by setting the combobox1 dropdownheight to a value i.e.100 Setting the maxdropdownitems had noeffect at all and what I don't understand is why the dropdownheight had no effect till I set a new value. the default of 106 had no effect but if I typed in 106 then it did.
Is this a bug?