Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 15th, 2004, 11:02 AM
Friend of Wrox
Join Date: Jul 2003
Location: York, , United Kingdom.
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
Default Combobox Autofill

I'm trying to implement a combobox that does the following:

- allows the user to type a value, but only of values that exist in the combobox's items collection

- 'autocompletes' values - so if a user types '6' it will complete the rest of the box with the first value in the collection beginning with 6, then if '3' is the next character the rest of the box is completed with the first value in the collection beginning with 63 - and not allow a key that filters down to no values

- when the drop-down arrow is clicked, shows all values belonging to the given filter - so in the above example, gives all values beginning with '63'

Has anyone ever implemented this sort of control? I've seen them in action, and they're a lot better than your standard combo box when you have a lot of values to select from (as my combo box will have!)


Reply With Quote
  #2 (permalink)  
Old March 15th, 2004, 12:29 PM
Friend of Wrox
Join Date: Jul 2003
Location: York, , United Kingdom.
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts

Don't worry, I've sorted this. However, I've created a new control to do it; wack the following into a new class

Public Class AutoCompleteComboBox
    Inherits System.Windows.Forms.ComboBox

    Protected _AutoCompleteEnabled As Boolean = True

    <System.ComponentModel.Category("Behavior"), System.ComponentModel.Description("Enables or disables the autocompletion functionality")> _
    Public Property AutoCompleteEnabled() As Boolean
            Return _AutoCompleteEnabled
        End Get
        Set(ByVal Value As Boolean)
            _AutoCompleteEnabled = Value
        End Set
    End Property

    Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
        ' only do something if autocomplete is enabled...
        If _AutoCompleteEnabled = True Then AutoComplete(e)
    End Sub

    Private Sub AutoComplete(ByVal e As Windows.Forms.KeyEventArgs)
        Dim sTypedText As String
        Dim iFoundIndex As Integer
        Dim oFoundItem As Object
        Dim sFoundText As String
        Dim sAppendText As String

        'Allow select keys without Autocompleting
        Select Case e.KeyCode
            Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, Keys.Delete, Keys.Down
        End Select

        'Get the Typed Text and Find it in the list
        sTypedText = Text
        iFoundIndex = FindString(sTypedText)

        'If we found the Typed Text in the list then Autocomplete
        If iFoundIndex >= 0 Then

            'Get the Item from the list (Return Type depends if Datasource was bound 
            ' or List Created)
            oFoundItem = Items(iFoundIndex)

            'Use the ListControl.GetItemText to resolve the Name in case the Combo 
            ' was Data bound
            sFoundText = GetItemText(oFoundItem)

            'Append then found text to the typed text to preserve case
            sAppendText = sFoundText.Substring(sTypedText.Length)
            Text = sTypedText & sAppendText

            'Select the Appended Text
            SelectionStart = sTypedText.Length
            SelectionLength = sAppendText.Length
        End If
    End Sub
End Class
I'd just like to say none of it is my own work apart from a tiny amount of combining of code -the autocomplete procedure is from http://www.thecodeproject.com/vb/net...e_combobox.asp by Daryl, the 'autocompleteenabled' idea from http://www.dotnetforums.net/t81380.html by Plausibly Damp.

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to Autofill headings sarahb Excel VBA 4 March 18th, 2008 03:35 PM
Filling Combobox with value from other Combobox ayazhoda Access VBA 6 June 5th, 2007 04:58 AM
Macro autofill EricB123 Excel VBA 3 February 23rd, 2007 03:55 PM
Autofill text from an Email Link zanna5910 Access 1 September 28th, 2005 07:13 PM
Autofill in unknown number of rows ashu_gupta75 Excel VBA 2 August 9th, 2004 06:09 AM

All times are GMT -4. The time now is 02:13 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.