RadAutoCompleteBox - Conditionally format token depending on value

3 posts, 1 answers
  1. David
    David avatar
    57 posts
    Member since:
    Apr 2011

    Posted 29 Oct 2012 Link to this post

    Hi,

    I'd like to conditionally format each token in a RadAutoCompleteBox control depending on the value of the each token. For example, I'd like to format all items that can be matched in the AutoComplete collection as green and all unmatched items in red.

    There is an introduction on formatting tokens at:

    http://www.telerik.com/help/winforms/editors-autocompletetextbox-formatting-blocks.html

    however I can't find a property in the token variable that gives me access to the corresponding entry in the Items collection in the RadAutoCompleteBox control so that I can style the token conditionally. How can I get access to corresponding item being formatted?

    Kind regards,
    Dave.
  2. Answer
    Svett
    Admin
    Svett avatar
    728 posts

    Posted 01 Nov 2012 Link to this post

    Hi David,

    Thank you for writing.

    You should perform search operation in the Items collection of RadAutoCompleteBox. You can use the following code snippet as sample:
    private void radAutoCompleteBox1_TextBlockFormatting(object sender, TextBlockFormattingEventArgs e)
            {
                TokenizedTextBlockElement token = e.TextBlock as TokenizedTextBlockElement;
     
                if (token != null)
                {
                    ITextBlock block = token as ITextBlock;
                    object value = GetTokenValue(block.Text);
     
                    // YOU ACTION HERE
                }
            }
     
    private object GetTokenValue(string text)
            {
                foreach (RadTokenizedTextItem item in this.radAutoCompleteBox1.Items)
                {
                    if (item.Text == text)
                    {
                        return item.Value;
                    }
                }
     
                return null;
            }

    I hope that you find this information useful.

    Kind regards,
    Svett
    the Telerik team
    Q3’11 of RadControls for WinForms is available for download (see what's new). Get it today.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. David
    David avatar
    57 posts
    Member since:
    Apr 2011

    Posted 01 Nov 2012 Link to this post

    HI Svett,

    Thanks - your code snippet works as expected. For VB.net programmers who like LINQ:

    Private Function GetTokenValue(text As String) As Object
        Return radAutoCompleteBox1.Items.First(Function(item As RadTokenizedTextItem) item.Text = text).Value
    End Function

    Kind regards,
    Dave.
Back to Top