Can anyone give me an idea as to how to make a Combo box auto-complete? The combo boxes are being filled from a SQL table, which works fine. I am not sure how to make them auto-complete when a letter is typed.
Also, in the db, there are some records with corresponding characters.
For example, one record might be F110 and another record might be S110. The end users want to be able to use 10-key to select items, so rather than typing F110 to find the correct db record, is there a way to make each one distinguished?
Can anyone give me an idea as to how to make a Combo box auto-complete? The combo boxes are being filled from a SQL table, which works fine. I am not sure how to make them auto-complete when a letter is typed.
Also, in the db, there are some records with corresponding characters.
For example, one record might be F110 and another record might be S110. The end users want to be able to use 10-key to select items, so rather than typing F110 to find the correct db record, is there a way to make each one distinguished?
Thanks.
After filling your combobox, you just need to set these properties
VB Code:
With ComboBox1
.AutoCompleteSource = AutoCompleteSource.ListItems 'The source is the items in combobox
.AutoCompleteMode = AutoCompleteMode.SuggestAppend 'Change the mode such that it best suits your need
Thanks guys. Stan, your solution worked exactly how I wanted it to. I appreciate it.
Is there a way to distinguish between the different letters of records?
So if F110 and S110 exist, the end user doesn't want to type the letters, but use the 10-key? There are roughly 500 different entries into the table, so there might be 100 instances of the same number used but a different preceding letter...ie. F110, S110, F111, S111, etc.
So what was so different my example from stanav? Instead of coping and pasting codes you need to understand them. If you check the properties on the design time you would see all the values you could set for “AutoCompleteMode” and choose one that suits you. Also I don’t understand why you want to make your code bigger, more complicated that will be difficult to read, where you could simply set the properties on design time since you will set it only ones.
Rating is a way of saying thank you. Don't forget to rate always!
I didn't just copy and paste the code as you implied. Actually the first thing I did when I read the examples, was looked up information on each item and foudn this:
The AutoCompleteSource Enumeration has following members:
* AllSystemResources - Specifies the equivalent of FileSystem and AllUrl as the source. This is the default value when AutoCompleteMode has been set to a value other than the default.
* AllUrl - Specifies the equivalent of HistoryList and RecentlyUsedList as the source.
* CustomSource - Specifies strings from a built-in AutoCompleteStringCollection as the source.
* FileSystem - Specifies the file system as the source.
* FileSystemDirectories - Specifies that only directory names and not file names will be automatically completed.
* HistoryList - Includes the Uniform Resource Locators (URLs) in the history list.
* ListItems - Specifies that the items of the ComboBox represent the source.
* None - Specifies that no AutoCompleteSource is currently in use. This is the default value of AutoCompleteSource.
* RecentlyUsedList - Includes the Uniform Resource Locators (URLs) in the list of those URLs most recently used.
The AutoCompleteMode enumeration has following members:
* Append - Appends the remainder of the most likely candidate string to the existing characters, highlighting the appended characters.
* None - Disables the automatic completion feature for the ComboBox and TextBox controls.
* Suggest - Displays the auxiliary drop-down list associated with the edit control. This drop-down is populated with one or more suggested completion strings.
* SuggestAppend - Applies both Suggest and Append options.
I realize that both do the same thing, but just selecting an option in the properties window does not really do any more than copy/pasting code. You said "try to set these properties of the combo box and try again" with no explanation. With Stan's code samples, I was able to search and see what each part did.
I am not a college student needing a grade, I am an IT professional trying my best to learn VB.net on my own time. Just because YOU think someone is copying and pasting code to get the quick fix, you could be wrong. That does me absolutely no good in determining my solution. If you want to see what I had done previously to auto complete my combo box, here it is....I ran into a problem with the code though as if the values were entered too quick, the auto complete wouldn't respond quick enough.
Code:
'AUTO COMPLETE CODE TO FILL IN FINISHERLOC COMBO BOX
Private Sub cboFinisherLoc_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboFinisherLoc.KeyUp
Dim iIndex As Integer
Dim sActual As String
Dim sFound As String
Dim bMatchFound As Boolean
If Not cboFinisherLoc.Text = "" Then 'if the text is not blank then only proceed
If e.KeyCode = Keys.Back Then
cboFinisherLoc.Text = Mid(cboFinisherLoc.Text, 1, Len(cboFinisherLoc.Text) - 1)
End If
If ((e.KeyCode = Keys.Left) Or _
(e.KeyCode = Keys.Right) Or _
(e.KeyCode = Keys.Up) Or _
(e.KeyCode = Keys.Down) Or _
(e.KeyCode = Keys.PageUp) Or _
(e.KeyCode = Keys.PageDown) Or _
(e.KeyCode = Keys.Home) Or _
(e.KeyCode = Keys.End)) Then
Return
End If
Do
sActual = cboFinisherLoc.Text
iIndex = cboFinisherLoc.FindString(sActual)
If (iIndex > -1) Then
sFound = cboFinisherLoc.Items(iIndex).ToString()
cboFinisherLoc.SelectedIndex = iIndex
cboFinisherLoc.SelectionStart = sActual.Length
cboFinisherLoc.SelectionLength = sFound.Length
bMatchFound = True
Else
If sActual.Length = 1 Or sActual.Length = 0 Then
cboFinisherLoc.SelectedIndex = 0
cboFinisherLoc.SelectionStart = 0
cboFinisherLoc.SelectionLength = Len(cboFinisherLoc.Text)
bMatchFound = True
Else
cboFinisherLoc.SelectionStart = sActual.Length - 1
cboFinisherLoc.SelectionLength = sActual.Length - 1
cboFinisherLoc.Text = Mid(cboFinisherLoc.Text, 1, Len(cboFinisherLoc.Text) - 1)
End If
End If
Loop Until bMatchFound
End If
End Sub
My explanation was “try to set these properties of the combo box and try again” because if you did and found out it works then it would be natural to ask the question WHY? Then you will look for it on dynamic help which will give you more info then anyone of us here. Anyway I am glad that it worked. Peace!
Rating is a way of saying thank you. Don't forget to rate always!