文本框的自定义自动完成功能 [英] Custom autocomplete for textbox
问题描述
嗨
我希望为文本框提供一个自动完成功能,以便它显示包含文本框中键入的文本的值。
由于构建的自动完成功能仅建议StartsWith文本的值,我想建立一个用户控件
在网上搜索后,我得知我应该使用ToolStripDropDown ,ToolStripControlHost和一个ListBox来显示自定义列表。
下面是usercontrol的代码我正在使用的内容
问题是当显示ToolStripDropDown时,它会占用焦点而用户将无法继续输入。
要解决此问题,我已设置ToolStripDD的AutoClose为False,解决了这个问题。
但现在显示ToolStripDD并且用户最大化/分钟。或者移动或点击表格的标题栏,DropDown没有关闭。
Plz建议我解决这个问题。
问候
我尝试过:
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
推荐答案
检查:c# - 自定义TextBox自动完成 - 堆栈溢出 [ ^ ]
For有关详细信息,请参阅: TextBox.AutoCompleteCustomSource属性(System.Windows.Forms)| Microsoft Docs [ ^ ]
Check this: c# - Customize TextBox autocomplete - Stack Overflow[^]
For further details, please see: TextBox.AutoCompleteCustomSource Property (System.Windows.Forms) | Microsoft Docs[^]
这篇关于文本框的自定义自动完成功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!