.net的自动完成文本框,支持分隔符

时间:2011-04-06 01:52:05

标签: vb.net text autocomplete

我正在开发一个应用程序,它需要一个带有自动完成/建议(下拉)的文本框,用于多个单词(用空格分隔符分隔),就像StackOverflow中的标签文本框一样。

例如,我开始输入'app',它应该在 app 开头的建议列表中显示所有单词,当我输入单词并按空格键并开始输入新单词时,它应该显示该部分单词的所有建议。

我可以查看一个例子吗?

1 个答案:

答案 0 :(得分:1)

我希望这有帮助..我正在使用开发人员快速工具,但同样可以与常规.net组件一起使用。

Private Sub txtToEmail_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtToEmail.EditValueChanged
    Try
        Dim Emails As New List(Of String)
        Emails.Add("123@abc.com")
        Emails.Add("456@dfg.com")
        Emails.Add("abc@123.com")
        Emails.Add("dfg@456.com")

        Dim Txt = Trim(CStr(txtToEmail.EditValue))

        Dim Suggestions As IEnumerable(Of String)

        If Txt <> "" Then
            If Txt.IndexOf(",") = -1 Then
                Suggestions = From c In Emails Where c.StartsWith(Txt) Select c
            Else
                Dim lastIndex = Txt.LastIndexOf(",")
                Dim lastWord = Trim(Txt.Substring(lastIndex + 1))
                Suggestions = From c In Emails Where c.StartsWith(lastWord) Select c
            End If

            EmailList.Items.Clear()

            For Each r In Suggestions
                EmailList.Items.Add(r)
            Next

        End If

        If EmailList.ItemCount > 0 Then
            EmailList.Visible = True
        End If

    Catch ex As Exception
        ShowErrorBox(ex)
    End Try

End Sub

Private Sub EmailList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmailList.Click
    Try
        If EmailList.SelectedValue = Nothing OrElse EmailList.SelectedValue = "" Then Return

        Dim Txt = CStr(txtToEmail.EditValue)

        If Txt.IndexOf(",") = -1 Then
            txtToEmail.EditValue = EmailList.SelectedValue
        Else
            Dim lastIndex = Txt.LastIndexOf(",")
            txtToEmail.EditValue = Txt.Substring(0, lastIndex + 1) & EmailList.SelectedValue
        End If

        txtToEmail.Focus()
        txtToEmail.SelectionStart = CStr(txtToEmail.EditValue).Length

        EmailList.Visible = False
    Catch ex As Exception
        ShowErrorBox(ex)
    End Try

End Sub