Click here to Skip to main content
15,440,109 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


What I have tried:

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
        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  ' **** **** **** ****
    End Sub
    Private Sub MySearchBoxNew_Leave(sender As Object, e As System.EventArgs) Handles Me.Leave
    End Sub
    Private Sub MySearchBoxNew_LostFocus(sender As Object, e As System.EventArgs) Handles Me.LostFocus
    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)
    End Sub
    Private Sub CloseDropDown()
    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
Updated 19-May-19 1:21am
Gerry Schmitz 18-May-19 12:47pm    
You're asking for "opinions". Or for someone to do work for you. How many words will match when I type an "e"? eh? hey? eek? eel? lee. eke. tee. me.....

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

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