Combobox Selected Item Help

kite

New member
Joined
Nov 12, 2008
Messages
4
Programming Experience
Beginner
Hello,

I'm a little stuck on how to filter out filenames in my listbox based on a selected item in my combobox.
Does anyone know of any good examples (preferably on the web) as i'm having no luck so far.

Thanks
 
Please provide a FULL and CLEAR description of what you want to do. What is being selected in the ComboBox such that it can be used to filter this list of file names? Where does this list of file names come from?
 
Thanks for replying. Here is a rundown of my program:

1. The user clicks on the 'Select Folder' button
2. The user selects a folder and clicks 'Ok' on the browse folder dialog
3. The filenames contained in that selected folder are displayed in the textbox
4. The combobox is then populated with the extensions of the files listed in the textbox
5.The user should then be able to filter the filenames in the textbox by clicking on a certain extension in the combobox. For example, if the user wants to clear out the .txt and.jpg files and keep the .doc files in the textbox, then the user should click on the .doc extension listed in the combobox.

I've tried to get the filtering function to work but to no avail. Here is my attempt:

VB.NET:
Dim ext As String = "*.*"
        Dim listOfExts As New ArrayList

        For Each filename As String In My.Computer.FileSystem.GetFiles(FolderBrowserDialog1.SelectedPath, _
            FileIO.SearchOption.SearchTopLevelOnly, ext)

            ext = Path.GetExtension(filename).ToUpper
            If Not listOfExts.Contains(ext) Then
                listOfExts.Add(ext)

            End If
        Next

        listOfExts.Sort()
        CboExtensions.DataSource = listOfExts

        lblTotalItems.Text = txtBoxFiles.Lines.Length

       End Sub

Thanks again.
 
Your original post said listbox so that's what I'm going with.

This is working fine for populating a listbox with the files in the selected directory and filtering them when choosing an extension from the drop down list.

VB.NET:
	Private Sub btnSelectFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles btnSelectFolder.Click

		If Me.FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

			Dim extList As New List(Of String)
			Me.ComboBox1.Items.Clear()
                        Me.ListBox1.Items.Clear()

			Dim di As New IO.DirectoryInfo(Me.FolderBrowserDialog1.SelectedPath)
			Me.ListBox1.Items.AddRange(di.GetFiles())

			For Each fi As IO.FileInfo In di.GetFiles()
				If Not extList.Contains(fi.Extension) Then
					extList.Add(fi.Extension)
				End If
			Next

                        Me.ComboBox1.Items.Add("*.*")
			Me.ComboBox1.Items.AddRange(extList.ToArray())

		End If

	End Sub


VB.NET:
	Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles ComboBox1.SelectedIndexChanged

		Dim di As New IO.DirectoryInfo(Me.FolderBrowserDialog1.SelectedPath)

		Me.ListBox1.Items.Clear()

		Me.ListBox1.Items.AddRange(di.GetFiles("*" & Me.ComboBox1.SelectedItem))

	End Sub
 
One thing I'm noticing here is that every time the ComboBox selection changes you go grab those files from the hard drive, why not store all of the file names in a List (Of String) right after the user picks a folder then display the files in the ListBox based on the ComboBox selection from your list and not re-reading the HDD over and over again.
 
On a large directory I can see that a being detrimental.

You can use the FindAll method of the List and a Predicate.

VB.NET:
Dim fileList As List(Of String)

VB.NET:
	Private Sub btnSelectFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	 Handles btnSelectFolder.Click

		If Me.FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

			Dim extList As New List(Of String)
			fileList = New List(Of String)

			Me.ComboBox1.Items.Clear()
			Me.ListBox1.Items.Clear()

			Dim di As New IO.DirectoryInfo(Me.FolderBrowserDialog1.SelectedPath)

			For Each fi As IO.FileInfo In di.GetFiles()
				fileList.Add(fi.Name())
				If Not extList.Contains(fi.Extension.ToLower()) Then
					extList.Add(fi.Extension.ToLower())
				End If
				extList.Sort()
			Next

			Me.ComboBox1.Items.Add("*.*")
			Me.ComboBox1.Items.AddRange(extList.ToArray())

			Me.ComboBox1.SelectedItem = 0

		End If

	End Sub

VB.NET:
	Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
	Handles ComboBox1.SelectedIndexChanged

		Me.ListBox1.Items.Clear()

		Dim filteredList As List(Of String) = fileList.FindAll(AddressOf SelectedExtension)

		If Me.ComboBox1.SelectedItem = "*.*" Then
			Me.ListBox1.Items.AddRange(fileList.ToArray())
		Else
			Me.ListBox1.Items.AddRange(filteredList.ToArray())
		End If

	End Sub

VB.NET:
	Private Function SelectedExtension(ByVal s As String) As Boolean

		Dim fi As New IO.FileInfo(s)
		Return fi.Extension = Me.ComboBox1.SelectedItem

	End Function
 
Last edited:
Back
Top