Autocomplete Combo Boxes

Autocomplete Combo Boxes

When you develop an application in Microsoft Access, your combo boxes will, by default, incorporate autocomplete. However, Visual Basic does not include this intrinsic support. If you want autocomplete in VB, you’ve got to do it yourself. This tip shows you how.

First, you’ll need to add some methods to your form:

  • AutoCompleteKeyUp: This method accepts a combo box and KeyEventArgs objects as arguments. When you call this in the KeyUp event of your combo box, the method evaluates what the user has typed and selects the most appropriate match.
  • AutoCompleteLeave: Call this method when the Leave event of your combobox is fired. This method simply takes whatever you’ve finally chosen and cases it properly, according to the matching selection in the combo box.

Let’s look at those functions now:

Public Sub AutoCompleteKeyUp(ByVal Combo As ComboBox, _     ByVal e As KeyEventArgs)     Dim strTyped As String     Dim intFoundIndex As Integer     Dim objFoundItem As Object     Dim strFoundText As String     Dim strAppendText As String     ' Ignore basic selection keys     Select Case e.KeyCode         Case Keys.Back, Keys.Left, Keys.Right, Keys.Up, _             Keys.Delete, Keys.Down, Keys.CapsLock             Return     End Select     ' Find what user has typed in list     strTyped = Combo.Text     intFoundIndex = Combo.FindString(strTyped)     ' If found...     If intFoundIndex >= 0 Then         ' Get list item (actual type depends on whether data bound)         objFoundItem = Combo.Items(intFoundIndex)         ' Use control to resolve text - in case data bound         strFoundText = Combo.GetItemText(objFoundItem)         ' Append the typed text to rest of the found string         ' (text is set twice due to a combo box quirk:         '  on certain platforms, setting just once ignores casing!)         strAppendText = strFoundText.Substring(strTyped.Length)         Combo.Text = strTyped & strAppendText         Combo.Text = strTyped & strAppendText         ' Select the appended text         Combo.SelectionStart = strTyped.Length         Combo.SelectionLength = strAppendText.Length     End If End Sub Public Sub AutoCompleteLeave(ByVal Combo As ComboBox)     ' Correct casing when leaving combo     Dim intFoundIndex As Integer     intFoundIndex = Combo.FindStringExact(Combo.Text)     Combo.SelectedIndex = -1     Combo.SelectedIndex = intFoundIndex End Sub 

And here’s how to call these functions from your combo box:

Private Sub ComboBox1_KeyUp(ByVal sender As Object, _     ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp     AutoCompleteKeyUp(ComboBox1, e) End Sub Private Sub ComboBox1_Leave(ByVal sender As Object, _     ByVal e As System.EventArgs) Handles ComboBox1.Leave     AutoCompleteLeave(ComboBox1) End Sub 
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as