文本框中的 Vb.net + 自动完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4152144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:08:05  来源:igfitidea点击:

Vb.net + AutoComplete in textboxes

vb.netautocompletetextbox

提问by Kenny Bones

So I was reading a bit on AutoComplete of textboxes in VB.NET, but I can't really understand where these are stored? Is it a fully built in feature, or do I have to write some code for it to work? I've found the AutoCompleteModeand AutoCompleteSourceproperties of textboxes. But I want to append whatever I've written in the textbox to the autocomplete source. Do I connect the source to My.Settingsor something? Wouldn't I have to use an array as well? Any help would be appreciated :)

所以我在 VB.NET 中阅读了一些关于自动完成文本框的内容,但我真的不明白它们的存储位置?它是完全内置的功能,还是我必须编写一些代码才能使其工作?我找到了文本框的AutoCompleteModeAutoCompleteSource属性。但我想将我在文本框中编写的任何内容附加到自动完成源。我是否将源连接到My.Settings或什么?我不是也必须使用数组吗?任何帮助,将不胜感激 :)

回答by stakx - no longer contributing

You would have to add new entries to your auto-complete data source manually... which makes sense, when you think about it: How is Windows Forms supposed to know when a new entry should be added to the list of suggestions and when the text entered is only something temporary?

您必须手动将新条目添加到您的自动完成数据源中...这是有道理的,当您考虑它时:Windows 窗体应该如何知道何时应将新条目添加到建议列表以及何时输入的文字只是暂时的?

You could add new values e.g. when validation of the input field happens, or when the user presses an OK / Apply button, or whatever fits your need best. But you will have to do this yourself.

您可以添加新值,例如,在验证输入字段时,或当用户按下“确定”/“应用”按钮时,或任何最适合您需要的值。但是你必须自己做这件事。

The properties you've already discovered are the right ones.

您已经发现的属性是正确的。

Dim suggestions As New List(Of String)
suggestions.Add("Abba")
suggestions.Add("Nirvana")
suggestions.Add("Rolling Stones")
...
textBox.AutoCompleteSource = suggestions

You can bind AutoCompleteSourceto almost anything; this is very similar to data-binding. One thing to keep in mind is that if you're adding new entries to the auto-complete data source, the UI control might not immediately notice if your data source doesn't implement the INotifyCollectionChangedinterface.

AutoCompleteSource几乎可以绑定任何东西;这与数据绑定非常相似。要记住的一件事是,如果您向自动完成数据源添加新条目,则 UI 控件可能不会立即注意到您的数据源未实现该INotifyCollectionChanged接口。

回答by Christhofer Natalius

first create the list to use as the custom source.

首先创建列表以用作自定义源。

Dim MySource As New AutoCompleteStringCollection()

and then set the property of the textbox

然后设置文本框的属性

With MyTextbox
   .AutoCompleteCustomSource = MySource
   .AutoCompleteMode = AutoCompleteMode.SuggestAppend
   .AutoCompleteSource = AutoCompleteSource.CustomSource
End With

put this code in eventlistener you use for validating the input field, e.g. btnOK.Click

将此代码放在用于验证输入字段的事件侦听器中,例如 btnOK.Click

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    MySource.Add(txtinput.text)
End Sub