PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2005 Custom Autocomplete for TextBox-VBForums
Results 1 to 2 of 2

Thread: Custom Autocomplete for TextBox

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Custom Autocomplete for TextBox

    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.


    Here is the code of the usercontrol what i am using.
    vb Code:
    1. Public Class MySearchBoxNew
    2.  
    3.     Dim PopUpControl As ToolStripDropDown
    4.     Dim List_Box As ListBox
    5.     Dim Control_Host As ToolStripControlHost
    6.  
    7.  
    8.     Private Sub MySearchBoxNew_Enter(sender As Object, e As System.EventArgs) Handles Me.Enter
    9.         PopUpControl = New ToolStripDropDown
    10.         List_Box = New ListBox
    11.         Control_Host = New ToolStripControlHost(List_Box)
    12.  
    13.  
    14.         List_Box.BorderStyle = BorderStyle.None
    15.         List_Box.SelectionMode = SelectionMode.One
    16.         List_Box.BindingContext = New BindingContext
    17.         List_Box.IntegralHeight = True
    18.  
    19.         List_Box.Items.Clear()
    20.         List_Box.Items.Add("A")
    21.         List_Box.Items.Add("B")
    22.         List_Box.Items.Add("c")
    23.         List_Box.Items.Add("D")
    24.  
    25.         Control_Host.Padding = New Padding(0)
    26.         Control_Host.Margin = New Padding(0)
    27.         Control_Host.AutoSize = False
    28.  
    29.  
    30.         PopUpControl.Padding = New Padding(0)
    31.         PopUpControl.Margin = New Padding(0)
    32.         PopUpControl.Width = Me.TextBox1.Width
    33.         PopUpControl.AutoSize = True
    34.         PopUpControl.AutoClose = False  ' **** **** **** ****
    35.         PopUpControl.Items.Add(Control_Host)
    36.  
    37.     End Sub
    38.  
    39.     Private Sub MySearchBoxNew_Leave(sender As Object, e As System.EventArgs) Handles Me.Leave
    40.         Me.DiscardControls()
    41.     End Sub
    42.  
    43.     Private Sub MySearchBoxNew_LostFocus(sender As Object, e As System.EventArgs) Handles Me.LostFocus
    44.         Me.DiscardControls()
    45.     End Sub
    46.  
    47.     Private Sub ShowDropDown()
    48.         Dim pnt As Point = New Point(Me.TextBox1.Location.X, Me.TextBox1.Location.Y + Me.TextBox1.Height)
    49.         Dim PointToShowMenu As Point = Me.PointToScreen(pnt)
    50.  
    51.         If PopUpControl Is Nothing Then PopUpControl = New ToolStripDropDown
    52.         If List_Box Is Nothing Then List_Box = New ListBox
    53.         If Control_Host Is Nothing Then Control_Host = New ToolStripControlHost(List_Box)
    54.         PopUpControl.Show(PointToShowMenu)
    55.     End Sub
    56.  
    57.     Private Sub CloseDropDown()
    58.         PopUpControl.Hide()
    59.     End Sub
    60.     Private Sub DiscardControls()
    61.         If PopUpControl.Visible Then PopUpControl.Close()
    62.         If PopUpControl IsNot Nothing Then PopUpControl.Close()
    63.         Control_Host = Nothing
    64.         PopUpControl = Nothing
    65.         List_Box = Nothing
    66.     End Sub
    67.  
    68. End Class

    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

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,251

    Re: Custom Autocomplete for TextBox

    Quote Originally Posted by aashish_9601 View Post
    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
    Maybe add handlers to the UC to detect when certain Form events occur, something like...
    Code:
    Public Sub ClosePopUp(sender As Object, e As EventArgs)
        If PopUpControl IsNot Nothing Then PopUpControl.Close()
    End Sub
    
    Private Sub MySearchBoxNew_HandleCreated(sender As Object, e As EventArgs) Handles Me.HandleCreated
        AddHandler Me.ParentForm.SizeChanged, AddressOf ClosePopUp
        AddHandler Me.ParentForm.Move, AddressOf ClosePopUp
        AddHandler Me.ParentForm.Deactivate, AddressOf ClosePopUp
    End Sub
    
    Private Sub MySearchBoxNew_HandleDestroyed(sender As Object, e As EventArgs) Handles Me.HandleDestroyed
        RemoveHandler Me.ParentForm.SizeChanged, AddressOf ClosePopUp
        RemoveHandler Me.ParentForm.Move, AddressOf ClosePopUp
        RemoveHandler Me.ParentForm.Deactivate, AddressOf ClosePopUp
    End Sub
    To detect if mouse clicked titlebar, maybe check form messages for a mouse non-client msg, then call the public Sub in UC to close popup...
    Code:
    Private Const WM_NCLBUTTONDOWN As Integer = &HA1
    
    Protected Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)
        If m.Msg = WM_NCLBUTTONDOWN Then ' call public sub in UC
            MySearchBoxNew1.ClosePopUp(Nothing, Nothing)
        End If
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width