Auto Complete Combo Box

Does anyone know of some good example source online of how to do this in vb.net? Or have a good idea on how to do it? I need something with functionality similar to the IE address bar. If possibly I would also like to be able to set it to match list values with the typed value existing at any position in it instead of just the beginning. I appologize if this is a repeat post or I am totally missing something like that this is included in the framework ;).

Thanks in advance,

Paul Tyng

[492 byte] By [codefund.com] at [2007-12-16]
# 1
Here is the code:
1- Inherits combobox class as follow. (i named it clsComboBox).
2- add HandleKeyPress method to the inherited class as follow.
3- if you want backspace functionality then overrides OnKeyDown as follow.
4- you must make your combobox editable in initialization.
5- instantiate clsComboBox instead of normal ComboBox.

Public Class clsComboBox
Inherits ComboBox

Public Sub New()
AddHandler Me.KeyPress, AddressOf HandleKeyPress
End Sub

Private Sub HandleKeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim lngItemNum As Long
Dim lngSelectedLength As Long
Dim lngMatchLength As Long
Dim strCurrentText As String
Dim strSearchText As String
Dim sTypedText As String
Const CB_LOCKED = &H255

Try
With (Me)
If .Items.Count > 0 Then
If .Text = Nothing Then
.Tag = Nothing
Exit Sub
End If
If .Text.Length = 1 Then
.Tag = Nothing
End If
Me.BeginUpdate()
If ((InStr(1, .Text, .Tag, vbTextCompare) <> 0 And Len(.Tag) = Len(.Text) - 1) Or (InStr(.Text, 1) <> InStr(.Tag, 1) And .Tag <> Nothing)) And .Tag <> CStr(CB_LOCKED) Then
strSearchText = .Text
lngSelectedLength = Len(strSearchText)
lngItemNum = .FindString(strSearchText)
If Not (lngItemNum = -1) Then
.Tag = CB_LOCKED
.SelectedIndex = lngItemNum
lngMatchLength = Len(.Text) - lngSelectedLength
sTypedText = strSearchText
.SelectionStart = lngSelectedLength
Dim Temp As Integer
Temp = lngMatchLength
.SelectionLength = Temp
.Tag = sTypedText
End If

ElseIf .Tag <> CStr(CB_LOCKED) Then
.Tag = .Text
End If
Me.EndUpdate()
End If
End With

Catch err As Exception
MsgBox(err.Message & err.StackTrace)
End Try

End Sub

Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = 8 Then
Me.SelectedIndex = -1
Me.SelectedValue = ""
Me.Text = ""
End If
End Sub
End Class

codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
I could swear I saw a writeup on an example of this recently. I'd love to dig in and write it, but I think there's one already out there. I'm pretty sure I saw it in the weekly newsletter from <a href="http://www.DotnetWire.com">Dotnetwire.com</a>. You might check their site, and see what you come up with. If you still don't get anywhere, I'm sure someone here will be happy to cruft one up...<g>
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
OK. I'm embarrassed. While I was posting a response in this thread, another poster provided working code. When I attempted to delete my response, I hit delete twice (on a very very slow connection here on the road in steamy southern FL), I lost the good response. I'm hoping that someone who picked it up, or the original poster, wouldn't mind reposting. Sorry....
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
So forget it. It didn't get deleted, and I've got three posts here that SHOULD be deleted. Argh.
Thanks, and sorry for the "noise"
codefund.com at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...