开发者

autocomplete text box for .net with support for delimiter

I am developing an application which requires a text box with auto-complete/suggestions (drop down) for multiple words (separated by a delimiter like space) like the tags text box here in StackOverflow.

For example, I start typing 'app' and开发者_运维知识库 it should show all words in the suggestions list starting with app, and when I enter a word and press space and start typing a new word it should show all suggestions for that partial word.

Is there an example I can have a look at?


I Hope this helps.. I'm using developer-express tools, but the same can be used with the regular .net components.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜