This is a migrated thread and some comments may be shown as answers.

RadAutoCompleteBox - Conditionally format token depending on value

2 Answers 161 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 2
David asked on 29 Oct 2012, 08:47 AM
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 Answers, 1 is accepted

Sort by
0
Accepted
Svett
Telerik team
answered on 01 Nov 2012, 07:35 AM
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.
0
David
Top achievements
Rank 2
answered on 01 Nov 2012, 09:48 AM
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.
Tags
AutoCompleteBox
Asked by
David
Top achievements
Rank 2
Answers by
Svett
Telerik team
David
Top achievements
Rank 2
Share this question
or