SoFunction
Updated on 2025-03-08

Intelligent perception application example of TextBox

This article introduces the intelligent perception implementation method of TextBox in the example form. The functions are very practical, as follows:

This example mainly implements: typing characters in a TextBox can intelligently sense the list, and automatically display "Not Found" for non-existent words (without intelligent sense).

The first thing that comes to mind about this function is to utilize TextBox's AutoComplete feature. This feature allows you to set up different forms of AutoComplete intelligent perception, such as:

1)AutoCompleteSource:Set the aware source type (here is CustomSource).

2)AutoCompleteMode:Set the perceptual mode (the input does not have characters to append, does not append or at the same time, obviously not append here).

3)AutoCompleteCustomSource:Set the source data (AutoCompleteSource must be CustomSource).

Next, think about how to determine whether it is perceived when entering the first character, and if not, display the text.

Drag a Label onto the form, and then judge the data source in the KeyUp event of TextBox (for convenience, you can directly convert the data source data into Array form and then use the extension method Any to judge). At the same time, in order to prevent the interface from being stuck, use asynchronous.

The specific implementation code is as follows:

Public Class Form1
  Dim collection As New AutoCompleteStringCollection
  Private ReadOnly arrayCollection() As String = {"a"}

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles 

  End Sub

  Public Sub New()

    InitializeComponent()
    (New String() {"apple", "aero", "banana"})
     = collection
    ReDim arrayCollection( - 1)
    (arrayCollection, 0)
  End Sub
  ''' <summary>
  ''' When release the keys, plz start a background thread to handle the problem
  ''' </summary>
  Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles 
    Dim act As New Action(Sub()
                 'Check whether there are any values inside the collection or not
                 If ( = "") OrElse ((Function(s)
                                             Return ()
                                           End Function)) Then

                   (New MethodInvoker(Sub()
                                         = 
                                      End Sub))
                 Else
                   (New MethodInvoker(Sub()
                                         = "Not found"
                                      End Sub))
                 End If

               End Sub)
    (Nothing, Nothing)
  End Sub
End Class

Here are some notes:

1)Asynchronous exceptions will not be thrown (because the essence of asynchronous is the thread inside the CLR), and can only be seen during debugging. Therefore, you must be extremely careful when writing asynchronous programs.

2)Define an array (for example, an array that defines String(5) has a length of 6 (from 0~5) that contains "5" itself. Therefore, when copying the array (Redim redefining the size), you must Count-1, otherwise one more redefined array will be available, and the default is Nothing, which will cause an exception to the asynchronous thread).