Click here to Skip to main content
14,391,726 members
Rate this:
Please Sign up or sign in to vote.
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.

Regards

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
 
        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
Posted
Updated 19-May-19 2:21am
v3
Comments
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100