有没有办法从 vb.net 中的文本框 - AutoCompleteCustomSource 检索集合项

Is there a way to retreive the collection items from Textbox - AutoCompleteCustomSource in vb.net

执行下面的代码后,我成功地将数组值插入到文本框 AutoCompleteCustomSource 中。

但我也需要能够从 AutoCompleteCustomSource 读回所有数据,并将其放入数组中。

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
    TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource

    Dim Arr1 As String() = {"Hello", "How", "Are", "You"}

    'Below line of code puts all the array values into TxtBox.AutoCompleteCustomSource
    TextBox1.AutoCompleteCustomSource.AddRange(Arr1)


    '-------------Need Help on Below-------
    'How to read all data from TextBox1.AutoCompleteCustomSource and bring it into an array
    Dim MyArr1
    MyArr1 = TextBox1.AutoCompleteCustomSource??????????????????????????????????????

End Sub

您可以使用一些 LINQ:

Dim items = TextBox1.AutoCompleteCustomSource.Cast(Of String)().ToArray()

AutoCompleteCustomSourceAutoCompleteStringCollection 类型,它实现 IEnumerable 而不是 IEnumerable(Of T),尽管每个项目都保证是 String。这意味着您可以调用 Cast(Of String) 扩展方法来获取 IEnumerable(Of String),然后对其调用 ToArray 来获取 String 数组。

其他选项包括:

Dim source = TextBox1.AutoCompleteCustomSource
Dim items(source.Count - 1) As String

source.CopyTo(items, 0)

或者你可以用这个来真正老派:

Dim source = TextBox1.AutoCompleteCustomSource
Dim upperBound = source.Count - 1
Dim items(upperBound) As String

For i = 0 To upperBound
    items(i) = source(i)
Next