Click here to Skip to main content
15,894,405 members

Custom autocomplete for textbox

aashish_9601 asked:

Open original thread
Hi I wish to provide an Autocomplete feature for the textbox such that it suggests values that 'Contains' the text typed in the textbox.

Since the built AutoComplete feature suggests only values that StartsWith the text, i thought of building an usercontrol

After searching on the net, i learned that i should be using an ToolStripDropDown, ToolStripControlHost and an ListBox to display the custom list.


Below is the code of the usercontrol what i am using

The Problem is When the ToolStripDropDown is Shown , it takes the focus and the user wont be able to continue the typing.
To tackle this i have set the AutoClose of the ToolStripDD to False, which solved the problem.

But now when the ToolStripDD is Shown and if the user maximizes/min. or move or click on the title bar of the form, the DropDown is not been closed.

Plz suggest me any workaround for this.

Regards

What I have tried:

VB
Public Class MySearchBoxNew
 
    Dim PopUpControl As ToolStripDropDown
    Dim List_Box As ListBox
    Dim Control_Host As ToolStripControlHost
 
 
    Private Sub MySearchBoxNew_Enter(sender As Object, e As System.EventArgs) Handles Me.Enter
        PopUpControl = New ToolStripDropDown
        List_Box = New ListBox
        Control_Host = New ToolStripControlHost(List_Box)
 
 
        List_Box.BorderStyle = BorderStyle.None
        List_Box.SelectionMode = SelectionMode.One
        List_Box.BindingContext = New BindingContext
        List_Box.IntegralHeight = True
 
        List_Box.Items.Clear()
        List_Box.Items.Add("A")
        List_Box.Items.Add("B")
        List_Box.Items.Add("c")
        List_Box.Items.Add("D")
 
        Control_Host.Padding = New Padding(0)
        Control_Host.Margin = New Padding(0)
        Control_Host.AutoSize = False
 
 
        PopUpControl.Padding = New Padding(0)
        PopUpControl.Margin = New Padding(0)
        PopUpControl.Width = Me.TextBox1.Width
        PopUpControl.AutoSize = True
        PopUpControl.AutoClose = False  ' **** **** **** ****
        PopUpControl.Items.Add(Control_Host)
 
    End Sub
 
    Private Sub MySearchBoxNew_Leave(sender As Object, e As System.EventArgs) Handles Me.Leave
        Me.DiscardControls()
    End Sub
 
    Private Sub MySearchBoxNew_LostFocus(sender As Object, e As System.EventArgs) Handles Me.LostFocus
        Me.DiscardControls()
    End Sub
 
    Private Sub ShowDropDown()
        Dim pnt As Point = New Point(Me.TextBox1.Location.X, Me.TextBox1.Location.Y + Me.TextBox1.Height)
        Dim PointToShowMenu As Point = Me.PointToScreen(pnt)
 
        If PopUpControl Is Nothing Then PopUpControl = New ToolStripDropDown
        If List_Box Is Nothing Then List_Box = New ListBox
        If Control_Host Is Nothing Then Control_Host = New ToolStripControlHost(List_Box)
        PopUpControl.Show(PointToShowMenu)
    End Sub
 
    Private Sub CloseDropDown()
        PopUpControl.Hide()
    End Sub
    Private Sub DiscardControls()
        If PopUpControl.Visible Then PopUpControl.Close()
        If PopUpControl IsNot Nothing Then PopUpControl.Close()
        Control_Host = Nothing
        PopUpControl = Nothing
        List_Box = Nothing
    End Sub
  
End Class
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900