Auto Complete Folder Combobox

  • Thread starter Thread starter John
  • Start date Start date
J

John

Does anyone have some sample code for building a combobox that will
automatically list the remaining folders while you type? I'm looking for
functionality similar to the "Look in:" combobox in the Search Companion
panel in Windows Explorer. If you type "C:\" it will open the drop down and
fill it in with all of the directories on C:\. Then as you type letters it
will re-fill the drop down with only the folders that match your typing.

Thanks,
John
 
Since nobody has answered this, I'm going to post a response.
This example is in VB2005 (hey, it's better than nothing).


On the combobox:
Set AutoCompleteMode to SuggestAppend.
Set AutoCompleteSource to CustomSource.

Private Sub LoadManagerComboBox()
If MgrList IsNot Nothing AndAlso MgrList.Count > 0 Then
MgrList = Nothing 'blank out the table
End If
MgrList = PTBO.ManagerList.Create("ManagerName")
'bind the combo box to the list
ManagerComboBox.DataSource = MgrList
ManagerComboBox.DisplayMember = "FullName"
ManagerComboBox.ValueMember = "UserID"
ManagerComboBox.SelectedIndex = -1
Call BuildAutoCompleteStrings()
End Sub

Private Sub BuildAutoCompleteStrings()
'If the manager list doesn't have any entries, return.
If MgrList.Count <= 0 OrElse ManagerComboBox.Items.Count <= 0 Then
Return
End If
' Clear what is in there now
ManagerComboBox.AutoCompleteCustomSource.Clear()
'Set the column name.
Dim filterField As String = "FullName"
' Build the list of filter values.
Dim filterVals As AutoCompleteStringCollection = _
New AutoCompleteStringCollection()
For Each dataItem As Object In MgrList
Dim props As PropertyDescriptorCollection = _
TypeDescriptor.GetProperties(dataItem)
Dim propDesc As PropertyDescriptor = _
props.Find(filterField, True)
Dim fieldVal As String = _
propDesc.GetValue(dataItem).ToString()
filterVals.Add(fieldVal)
Next
' Set the list on the collection.
ManagerComboBox.AutoCompleteCustomSource = filterVals
End Sub

Robin S.
 
Thanks Robin. I'll check this out tonight. I had sort of given up hope for
an example. I'll post this in C# once I get it working.

John
 
You're welcome.

The irony is, I first saw it in Brian Noyes' Data Binding book, where all
the examples are in C#. It's a great book, if you're doing much data
binding, and it's responsible for the C# I *do* know, because I had to
figure it out to read the book. (I'm sure it made me a better person).

Good luck.
Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top