Results 1 to 14 of 14

Thread: free autocomplete combo code

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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:
    1. Imports System.Windows.Forms
    2. Public Class AutoCompleteCombo
    3.     Inherits ComboBox
    4.  
    5.     'underscore added in sub name to bypass VBForum filter
    6.     Protected Overrides Sub Refresh_Item(ByVal index As Integer)
    7.         MyBase.Refresh_Item(index)
    8.     End Sub
    9.  
    10.     Protected Overrides Sub SetItemsCore(ByVal items As System.Collections.IList)
    11.         MyBase.SetItemsCore(items)
    12.     End Sub
    13.  
    14.     Public Shadows Sub KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
    15.         Dim intIndex As Integer
    16.         Dim strEntry As String
    17.  
    18.         If Char.IsControl(e.KeyChar) Then
    19.             If MyBase.SelectionStart <= 1 Then
    20.                 MyBase.Text = String.Empty
    21.                 MyBase.SelectedIndex = -1
    22.                 e.Handled = True
    23.                 Exit Sub
    24.             End If
    25.             If MyBase.SelectionLength = 0 Then
    26.                 strEntry = MyBase.Text.Substring(0, MyBase.Text.Length - 1)
    27.             Else
    28.                 strEntry = MyBase.Text.Substring(0, MyBase.SelectionStart - 1)
    29.             End If
    30.         ElseIf (Not Char.IsLetterOrDigit(e.KeyChar)) And (Not Char.IsWhiteSpace(e.KeyChar)) Then  '< 32 Or KeyAscii > 127 Then
    31.             Exit Sub
    32.         Else
    33.             If MyBase.SelectionLength = 0 Then
    34.                 strEntry = UCase(MyBase.Text & e.KeyChar)
    35.             Else
    36.                 strEntry = MyBase.Text.Substring(0, MyBase.SelectionStart) & e.KeyChar
    37.             End If
    38.         End If
    39.  
    40.         intIndex = MyBase.FindString(strEntry)
    41.  
    42.         If intIndex <> -1 Then
    43.             MyBase.SelectedIndex = intIndex
    44.             MyBase.SelectionStart = strEntry.Length
    45.             MyBase.SelectionLength = MyBase.Text.Length - MyBase.SelectionStart
    46.         End If
    47.         e.Handled = True
    48.         Exit Sub
    49.     End Sub
    50. End Class

    hope it helps someone if they have been looking for something like this

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    That's great. Did you try the code in the link I posted on your original post?

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    yeah but it was kind of cumbersome

    it didnt limit to list, and it seemed like it was more work than needed because i found an article on the MSKB that shows how to do the same thing, but it looks a lot simpler. Neither of them had the full functionality i was looking for to limit to list so thats why i coded my own

    http://support.microsoft.com/default...&Product=vbNET

  4. #4
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437

  5. #5
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    if anyone's interested, to go along with this, i wrote a custom combobox that will list the states automatically.
    VB Code:
    1. Public Class StateComboBox
    2.     Inherits ComboBox
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6.     Public Sub New()
    7.         MyBase.New()
    8.  
    9.         'This call is required by the Windows Form Designer.
    10.         InitializeComponent()
    11.  
    12.         'Add any initialization after the InitializeComponent() call
    13.  
    14.     End Sub
    15.  
    16.     'UserControl1 overrides dispose to clean up the component list.
    17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    18.         If disposing Then
    19.             If Not (components Is Nothing) Then
    20.                 components.Dispose()
    21.             End If
    22.         End If
    23.         MyBase.Dispose(disposing)
    24.     End Sub
    25.  
    26.     'Required by the Windows Form Designer
    27.     Private components As System.ComponentModel.IContainer
    28.  
    29.     'NOTE: The following procedure is required by the Windows Form Designer
    30.     'It can be modified using the Windows Form Designer.  
    31.     'Do not modify it using the code editor.
    32.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    33.         components = New System.ComponentModel.Container
    34.     End Sub
    35.  
    36. #End Region
    37.  
    38.     Private EnterColor As Color = Color.LightYellow
    39.     Private LeaveColor As Color = SystemColors.Window
    40.  
    41.     Property EnterFocusColor() As Color
    42.         Get
    43.             Return EnterColor
    44.         End Get
    45.         Set(ByVal Value As Color)
    46.             EnterColor = Value
    47.         End Set
    48.     End Property
    49.     Property LeaveFocusColor() As Color
    50.         Get
    51.             Return LeaveColor
    52.         End Get
    53.         Set(ByVal Value As Color)
    54.             LeaveColor = Value
    55.         End Set
    56.     End Property
    57.  
    58.  
    59.     Private Sub StateComboBox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Enter
    60.         Me.BackColor = EnterFocusColor
    61.     End Sub
    62.     Private Sub StateComboBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Leave
    63.         Me.BackColor = LeaveFocusColor
    64.     End Sub
    65.  
    66.     Private Sub StateComboBox_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DropDown
    67.         'This checks first if the collection has been populated already.
    68.         If Me.Items.Contains("Alabama") Then
    69.             Exit Sub
    70.         End If
    71.  
    72.         Dim list As String = "Alabama|Alaska|Arizona|Arkansas|California|Colorado|Connecticut|Delaware|District of Columbia|" & _
    73.                                                     "Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Maryland|" & _
    74.                                                     "Massachusetts|Michigan|Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New Hampshire|" & _
    75.                                                     "New Jersey|New Mexico|New York|North Carolina|North Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|" & _
    76.                                                     "Rhode Island|South Carolina|South Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West Virginia|Wisconsin|Wyoming"
    77.  
    78.         Dim states As String()
    79.         states = list.Split("|"c)
    80.  
    81.         For Each s As String In states
    82.             Me.Items.Add(s)
    83.         Next s
    84.     End Sub
    85. End Class
    putting these together would be awesome.

  6. #6
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    One other thing, how do you get the nice little icon that's already used by the standard combobox? when I compile the above code, I get a cogwheel. I get that for ALL my custom controls. I would LIKE to have a custom icon to go along with all these controls I've made up.

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Specify that with the <ToolBoxBitMap> tag.....

  8. #8

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    this doesnt limit to list, and all they did for the autocomplete routine is use the same code as the MSKB article I linked above

  9. #9

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Andy, I put your state code in... but instead of adding it in to my code, I created a second class in the project and had it inherit from my original class... so it has all the functionality plus the states... that way you can use the autocomplete combo for something other than states, and the autocompletestatecombo for when you need it specifically for states..
    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Public Class AutoCompleteStateCombo
    5.     Inherits AutoCompleteCombo
    6.  
    7.     Public Sub New()
    8.         MyBase.New()
    9.         Dim strStateString As String = "Alabama|Alaska|Arizona|Arkansas|California|Colorado|Connecticut|Delaware|District of Columbia|" & _
    10.                                                     "Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Maryland|" & _
    11.                                                     "Massachusetts|Michigan|Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New Hampshire|" & _
    12.                                                     "New Jersey|New Mexico|New York|North Carolina|North Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|" & _
    13.                                                     "Rhode Island|South Carolina|South Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West Virginia|Wisconsin|Wyoming"
    14.  
    15.         Dim States() As String = strStateString.Split("|"c)
    16.  
    17.         MyBase.Items.AddRange(States)
    18.  
    19.     End Sub
    20.  
    21.  
    22. End Class

  10. #10

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    I added 2 new features to the state combo class, 1 is a readonly property to get the 2 letter abbreviation for the state currently selected, and the other is a method you can use to get the state abbreviation by passing in a state name

    here is the complete code for the state combo class
    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Public Class AutoCompleteStateCombo
    5.     Inherits AutoCompleteCombo
    6.  
    7.     Public Sub New()
    8.         MyBase.New()
    9.         Dim strStateString As String = "Alabama|Alaska|Arizona|Arkansas|California|Colorado|Connecticut|Delaware|District of Columbia|" & _
    10.                                                     "Florida|Georgia|Hawaii|Idaho|Illinois|Indiana|Iowa|Kansas|Kentucky|Louisiana|Maine|Maryland|" & _
    11.                                                     "Massachusetts|Michigan|Minnesota|Mississippi|Missouri|Montana|Nebraska|Nevada|New Hampshire|" & _
    12.                                                     "New Jersey|New Mexico|New York|North Carolina|North Dakota|Ohio|Oklahoma|Oregon|Pennsylvania|" & _
    13.                                                     "Rhode Island|South Carolina|South Dakota|Tennessee|Texas|Utah|Vermont|Virginia|Washington|West Virginia|Wisconsin|Wyoming"
    14.  
    15.         Dim States() As String = strStateString.Split("|"c)
    16.  
    17.         MyBase.Items.AddRange(States)
    18.  
    19.     End Sub
    20.     Public ReadOnly Property StateAbbr() As String
    21.         Get
    22.             Select Case Me.Text.ToUpper
    23.                 Case "Alabama" : StateAbbr = "AL"
    24.                 Case "Alaska" : StateAbbr = "AK"
    25.                 Case "Arizona" : StateAbbr = "AZ"
    26.                 Case "Arkansas" : StateAbbr = "AR"
    27.                 Case "California" : StateAbbr = "CA"
    28.                 Case "Colorado" : StateAbbr = "CO"
    29.                 Case "Connecticut" : StateAbbr = "CT"
    30.                 Case "Delaware" : StateAbbr = "DE"
    31.                 Case "District of Columbia" : StateAbbr = "DC"
    32.                 Case "Florida" : StateAbbr = "FL"
    33.                 Case "Georgia" : StateAbbr = "GA"
    34.                 Case "Hawaii" : StateAbbr = "HI"
    35.                 Case "Idaho" : StateAbbr = "ID"
    36.                 Case "Illinois" : StateAbbr = "IL"
    37.                 Case "Indiana" : StateAbbr = "IN"
    38.                 Case "Iowa" : StateAbbr = "IA"
    39.                 Case "Kansas" : StateAbbr = "KS"
    40.                 Case "Kentucky" : StateAbbr = "KY"
    41.                 Case "Louisiana" : StateAbbr = "LA"
    42.                 Case "Maine" : StateAbbr = "ME"
    43.                 Case "Maryland" : StateAbbr = "MD"
    44.                 Case "Massachusetts" : StateAbbr = "MA"
    45.                 Case "Michigan" : StateAbbr = "MI"
    46.                 Case "Minnesota" : StateAbbr = "MN"
    47.                 Case "Mississippi" : StateAbbr = "MS"
    48.                 Case "Missouri" : StateAbbr = "MO"
    49.                 Case "Montana" : StateAbbr = "MT"
    50.                 Case "Nebraska" : StateAbbr = "NE"
    51.                 Case "Nevada" : StateAbbr = "NV"
    52.                 Case "New Hampshire" : StateAbbr = "NH"
    53.                 Case "New Jersey" : StateAbbr = "NJ"
    54.                 Case "New Mexico" : StateAbbr = "NM"
    55.                 Case "New York" : StateAbbr = "NY"
    56.                 Case "None/Other" : StateAbbr = "XX"
    57.                 Case "North Carolina" : StateAbbr = "NC"
    58.                 Case "North Dakota" : StateAbbr = "ND"
    59.                 Case "Ohio" : StateAbbr = "OH"
    60.                 Case "Oklahoma" : StateAbbr = "OK"
    61.                 Case "Oregon" : StateAbbr = "OR"
    62.                 Case "Pennsylvania" : StateAbbr = "PA"
    63.                 Case "Rhode Island" : StateAbbr = "RI"
    64.                 Case "South Carolina" : StateAbbr = "SC"
    65.                 Case "South Dakota" : StateAbbr = "SD"
    66.                 Case "Tennessee" : StateAbbr = "TN"
    67.                 Case "Texas" : StateAbbr = "TX"
    68.                 Case "Utah" : StateAbbr = "UT"
    69.                 Case "Vermont" : StateAbbr = "VT"
    70.                 Case "Virginia" : StateAbbr = "VA"
    71.                 Case "Washington" : StateAbbr = "WA"
    72.                 Case "West Virginia" : StateAbbr = "WV"
    73.                 Case "Wisconsin" : StateAbbr = "WI"
    74.                 Case "Wyoming" : StateAbbr = "WY"
    75.                 Case Else : StateAbbr = String.Empty
    76.             End Select
    77.         End Get
    78.     End Property
    79.  
    80.     Public Function GetNameFromAbbr(ByVal strAbbr As String) As String
    81.         Select Case strAbbr.ToUpper
    82.             Case "AL" : Return "Alabama"
    83.             Case "AK" : Return "Alaska"
    84.             Case "AZ" : Return "Arizona"
    85.             Case "AR" : Return "Arkansas"
    86.             Case "CA" : Return "California"
    87.             Case "CO" : Return "Colorado"
    88.             Case "CT" : Return "Connecticut"
    89.             Case "DE" : Return "Delaware"
    90.             Case "DC" : Return "District of Columbia"
    91.             Case "FL" : Return "Florida"
    92.             Case "GA" : Return "Georgia"
    93.             Case "HI" : Return "Hawaii"
    94.             Case "ID" : Return "Idaho"
    95.             Case "IL" : Return "Illinois"
    96.             Case "IN" : Return "Indiana"
    97.             Case "IA" : Return "Iowa"
    98.             Case "KS" : Return "Kansas"
    99.             Case "KY" : Return "Kentucky"
    100.             Case "LA" : Return "Louisiana"
    101.             Case "ME" : Return "Maine"
    102.             Case "MD" : Return "Maryland"
    103.             Case "MA" : Return "Massachusetts"
    104.             Case "MI" : Return "Michigan"
    105.             Case "MN" : Return "Minnesota"
    106.             Case "MS" : Return "Mississippi"
    107.             Case "MO" : Return "Missouri"
    108.             Case "MT" : Return "Montana"
    109.             Case "NE" : Return "Nebraska"
    110.             Case "NV" : Return "Nevada"
    111.             Case "NH" : Return "New Hampshire"
    112.             Case "NJ" : Return "New Jersey"
    113.             Case "NM" : Return "New Mexico"
    114.             Case "NY" : Return "New York"
    115.             Case "XX" : Return "None/Other"
    116.             Case "NC" : Return "North Carolina"
    117.             Case "ND" : Return "North Dakota"
    118.             Case "OH" : Return "Ohio"
    119.             Case "OK" : Return "Oklahoma"
    120.             Case "OR" : Return "Oregon"
    121.             Case "PA" : Return "Pennsylvania"
    122.             Case "RI" : Return "Rhode Island"
    123.             Case "SC" : Return "South Carolina"
    124.             Case "SD" : Return "South Dakota"
    125.             Case "TN" : Return "Tennessee"
    126.             Case "TX" : Return "Texas"
    127.             Case "UT" : Return "Utah"
    128.             Case "VT" : Return "Vermont"
    129.             Case "VA" : Return "Virginia"
    130.             Case "WA" : Return "Washington"
    131.             Case "WV" : Return "West Virginia"
    132.             Case "WI" : Return "Wisconsin"
    133.             Case "WY" : Return "Wyoming"
    134.             Case Else : Return String.Empty
    135.         End Select
    136.     End Function
    137. End Class

  11. #11
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    nice!!!!!



    That's what I'm talking about fellas, developer's helping developers

  12. #12

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    I am going to make a whole set of controls that are used in standard business applications like phone number textboxes, email textboxes, date textboxes, etc... to go along with the autocomplete state combo and autocomplete combo

    then once I am done I will post all the source... these are the kinds of controls I use all the time, but I used to copy code from project to project in VB6 because you couldnt simply inherit and override events... and who says .net isnt all its cracked up to be

  13. #13
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    I've got a PhoneMaskBox I created too. as a matter of fact, I'll zip up the 3 i have complete in a second and post them i'd like to see what ideas or improvements you have for these:

    Phone mask: simply formats a phone number and accepts only numbers. it will format 7 or 10 digits and reject anything else. it also has an Enter and Leave color properties

    myLabel: nothing special except it is already sized to 100 x 23 (the same as a text box).

    myTextBox: simply has an Enter and Leave color properties

  14. #14
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    I altered the code for your AutoCompleteStateBox just a tad. You did what I did in the begining. If you add items in the sub New(), it will add those each time you add a box to your form. I noticed that when I had 10 Alabamas and 10 Wyomings lol! I ended up putting that in the dropDown event and it checks for the name alabama.

    I also took out the Designer Generated Code like you did (except for the label control) I hope they still function.. I'd like some feedback on what i have so I can improve on them.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width