AutoCompleteBox: Different color boxes

4 posts, 0 answers
  1. John
    John avatar
    14 posts
    Member since:
    Apr 2013

    Posted 11 Apr 2013 Link to this post

    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
  2. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 16 Apr 2013 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. John
    John avatar
    14 posts
    Member since:
    Apr 2013

    Posted 16 Apr 2013 Link to this post

    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
  5. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 19 Apr 2013 Link to this post

    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.
Back to Top