|
-
Jul 30th, 2004, 11:32 AM
#1
free autocomplete combo code
Ok I wasnt able to find it so i gave up the search and coded it myself. Now as far as I can tell it works fine... but I would like to have some of you guys check it out and make any suggestions on potential problems or bad coding
Now what I did was create a class library for this so I could compile it and add it to my windows app, what I use the autocomplete combo is USUALLY for the state field in an address entry, but I use it for pretty much every combo that you dont want the user to enter their own value.
When I inherited the combobox in my class it automatically added 2 protected subs ("Refresh Item" <- no space but spells $hit when together and VBForums doesnt like that, and "SetItemsCore"). I am not sure why (maybe someone can tell me) so I just passed them up to the base class.
NOTE: while the code could be altered to do differently, this code is LIMIT TO LIST only, which means the user cant put in their own text if its not in the drop down list (perfect for a state combo for example)
VB Code:
Imports System.Windows.Forms
Public Class AutoCompleteCombo
Inherits ComboBox
'underscore added in sub name to bypass VBForum filter
Protected Overrides Sub Refresh_Item(ByVal index As Integer)
MyBase.Refresh_Item(index)
End Sub
Protected Overrides Sub SetItemsCore(ByVal items As System.Collections.IList)
MyBase.SetItemsCore(items)
End Sub
Public Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
Dim intIndex As Integer
Dim strEntry As String
If Char.IsControl(e.KeyChar) Then
If MyBase.SelectionStart <= 1 Then
MyBase.Text = String.Empty
MyBase.SelectedIndex = -1
e.Handled = True
Exit Sub
End If
If MyBase.SelectionLength = 0 Then
strEntry = MyBase.Text.Substring(0, MyBase.Text.Length - 1)
Else
strEntry = MyBase.Text.Substring(0, MyBase.SelectionStart - 1)
End If
ElseIf (Not Char.IsLetterOrDigit(e.KeyChar)) And (Not Char.IsWhiteSpace(e.KeyChar)) Then '< 32 Or KeyAscii > 127 Then
Exit Sub
Else
If MyBase.SelectionLength = 0 Then
strEntry = UCase(MyBase.Text & e.KeyChar)
Else
strEntry = MyBase.Text.Substring(0, MyBase.SelectionStart) & e.KeyChar
End If
End If
intIndex = MyBase.FindString(strEntry)
If intIndex <> -1 Then
MyBase.SelectedIndex = intIndex
MyBase.SelectionStart = strEntry.Length
MyBase.SelectionLength = MyBase.Text.Length - MyBase.SelectionStart
End If
e.Handled = True
Exit Sub
End Sub
End Class
hope it helps someone if they have been looking for something like this
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|