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

AutoCompleteBox: Different color boxes

3 Answers 118 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 11 Apr 2013, 09:04 PM
Hey Telerik!

How would I customize the boxes in a AutoCompleteBox to be different colors?  A simple idea would be to have a list of objects with a text property and color property.  The boxes could be a certain color based off the color property.

Thanks

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 16 Apr 2013, 12:26 PM
Hello John,

Thank you for writing.

To customize the tokens in the AutoCompleteBox, you should use the TextBoxFormatting event. The following article describes how to do that: http://www.telerik.com/help/winforms/editors-autocompletetextbox-formatting-blocks.html.

I hope that you find this information useful.
 

Greetings,
Stefan
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
0
John
Top achievements
Rank 1
answered on 16 Apr 2013, 02:33 PM
Stefan,

Thanks for the link.  I was hoping to use an object class to be able to add the text blocks.  How would I add the text blocks as a custom class.  The only way I see to add blocks is with  RadAutoCompleteBox1.Text = "foo;bar;"

Thanks
0
Stefan
Telerik team
answered on 19 Apr 2013, 10:34 AM
Hello John,

There is no way to add tokens to the control. If you want to use custom object, you have to create a custom token with the desired information, and use the CreateTextBlock event, to replace the standard token with yours. This approach is described here: http://www.telerik.com/help/winforms/editors-autocompletetextbox-creating-custom-blocks.html.

Alternatively, you can just store your item's text and desired color in a simple dictionary, and use the formatting event to pull the value from the dictionary and color the box as specified:
protected override void OnLoad(EventArgs e)
{
 
    base.OnLoad(e);
 
    items.Add("text1", Color.Red);
    items.Add("text2", Color.Green);
    items.Add("text3", Color.Blue);
    items.Add("text4", Color.Yellow);
}
 
Dictionary<string, Color> items = new Dictionary<string, Color>();
 
void radAutoCompleteBox1_TextBlockFormatting(object sender, TextBlockFormattingEventArgs e)
{
    TokenizedTextBlockElement token = e.TextBlock as TokenizedTextBlockElement;
    Color c;
    if (token != null && items.TryGetValue(e.TextBlock.Text.Trim(), out c))
    {
        token.GradientStyle = Telerik.WinControls.GradientStyles.Solid;
        token.BackColor = c;
    }
}

I hope this helps.

All the best,
Stefan
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
Tags
AutoCompleteBox
Asked by
John
Top achievements
Rank 1
Answers by
Stefan
Telerik team
John
Top achievements
Rank 1
Share this question
or