DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Lookup ComboBox

  1. #1
    Join Date
    Jan 2004

    Lookup ComboBox

    Does anyone have a good routine out there that I can use to enhance a .NET combobox to scroll down the items list matching items as I type in the text?

  2. #2
    Join Date
    Jan 2004

    Found a routine

    I found the following VB.NET routine that can be run in the TextChanged event of a combobox:

    Dim boxIndex As Integer, lExst As Boolean
    Dim box As ComboBox = sender
    Dim txt As String = box.Text
    Dim posCursor As Integer = box.SelectionStart
    ' If Cursor does not stay on the beginning of text box.
    If posCursor <> 0 Then
    lExst = False
    ' Go in cycle through the combo box list to
    ' find the appropriate entry in the list
    For boxIndex = 0 To box.Items.Count - 1
    If UCase(Mid(box.Items(boxIndex), 1, posCursor)) = UCase(Mid(txt, 1, posCursor)) Then
    box.Text = box.Items(boxIndex)
    box.SelectionStart = posCursor
    lExst = True
    Exit For
    End If
    ' We didn't find appropriate entry and return previous value to text box
    If Not lExst Then
    box.Text = Mid(txt, 1, posCursor - 1) + Mid(txt, posCursor + 1)
    box.SelectionStart = posCursor - 1
    End If
    End If

  3. #3
    Join Date
    Feb 2006
    Although this is a bit long and has no comments try this. Create a new user control in your project called ComboBoxEx and they replace the entire contents of the vb file with the following:

    'Drop this control on a form, add some items and try typing into the combo box.
    Public Class ComboBoxEx
    Inherits System.Windows.Forms.ComboBox

    Private Sub SelectValidItem()
    Dim mCharacterPosition As Integer = Me.SelectionStart

    If mCharacterPosition > 0 Then
    Dim EnteredText As String = Strings.Mid(Me.Text, 1, mCharacterPosition)
    Dim index As Integer = Me.FindString(EnteredText)

    If index > -1 Then
    Me.SelectedIndex = index
    Me.SelectionStart = mCharacterPosition
    Me.SelectionLength = Me.Text.Length - mCharacterPosition
    End If
    End If
    Catch ex As Exception
    End Try
    End Sub
    Private Sub ComboBoxEx_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    If (e.KeyCode = Windows.Forms.Keys.Back) AndAlso (Me.SelectionStart = Me.Text.Length) AndAlso (Me.SelectionLength = 0) Then
    Dim c As String = Strings.Mid(Me.Text, Me.SelectionStart, 1)
    Me.Text &= c
    Me.SelectionStart = Me.Text.Length
    End If
    Catch ex As Exception
    End Try
    End Sub
    Private Sub ComboBoxEx_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
    If e.KeyCode = Windows.Forms.Keys.Back Then
    If Me.SelectionStart > 0 Then
    Me.SelectionStart -= 1
    Me.SelectionLength += 1
    End If
    End If
    Catch ex As Exception
    End Try
    End Sub

    End Class

  4. #4
    Join Date
    Feb 2006
    Wow I just noticed that all the tabbing was removed. That makes the above code pretty hard to read, my advice is to drop it into VB and allow it to tabify the code.

    --I'll try to learn how to better post code on this site.

Similar Threads

  1. Replies: 6
    Last Post: 03-27-2010, 01:49 AM
  2. To Lookup or not to Lookup, that is the issue.
    By Otis in forum Architecture and Design
    Replies: 7
    Last Post: 09-07-2003, 02:33 PM
  3. Lookup list required for entry of a ROW of data
    By Mike Turner in forum VB Classic
    Replies: 0
    Last Post: 04-07-2002, 11:24 AM
  4. comboBox question
    By Stan Shankman in forum .NET
    Replies: 2
    Last Post: 09-07-2001, 03:06 AM
  5. ComboBox Value Disappeared Automatically
    By xiao_john@yahoo.com in forum VB Classic
    Replies: 0
    Last Post: 02-20-2001, 02:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center