Context menu on RadAutoCompleteBox items

3 posts, 1 answers
  1. Aleksander
    Aleksander avatar
    2 posts
    Member since:
    Aug 2016

    Posted 12 Aug Link to this post

    Hi!

    I'm trying to add a context menu for the RadTokenizedTextItem items in the RadAutoCompleteBox.Items collection. Something like the mockup-image i attached to this post.

    The RadAutoCompleteBox itself has both a ContextMenu and a RadContextMenu property, but the RadTokenizedTextItem have none of these.To be clear: I want to be able to right-click an individual token in the RadAutoCompleteBox, click an option in the menu, and then catch some kind of event that includes information about that token in the eventargs. Is this even possible? If so, how would I go about doing this?

    I'm using UI for WinForms.

     

    Thanks,

    Aleksander

  2. Answer
    Dess
    Admin
    Dess avatar
    1608 posts

    Posted 15 Aug Link to this post

    Hello Aleksander,

    Thank you for writing. 

    In order to customize the context menu, you can handle the ContextMenuOpening event. It is necessary to create a custom AutoCompleteBoxInputHandler in order to identify the clicked tokenized element. Here is a sample code snippet:
    public Form1()
    {
        InitializeComponent();
     
        this.radAutoCompleteBox1.ContextMenuOpening += radAutoCompleteBox1_ContextMenuOpening;
        this.radAutoCompleteBox1.TextBoxElement.InputHandler =
            new CustomAutoCompleteBoxInputHandler(this.radAutoCompleteBox1.TextBoxElement as RadAutoCompleteBoxElement);
    }
     
    private void radAutoCompleteBox1_ContextMenuOpening(object sender, TreeBoxContextMenuOpeningEventArgs e)
    {
        TokenizedTextBlockElement token = this.radAutoCompleteBox1.TextBoxElement.Tag as TokenizedTextBlockElement;
        if (token != null)
        {
            //modify the context menu according to the clicked token
        }
    }
     
    public class CustomAutoCompleteBoxInputHandler : AutoCompleteBoxInputHandler
    {
        public CustomAutoCompleteBoxInputHandler(RadAutoCompleteBoxElement textBoxElement) : base(textBoxElement)
        {
        }
     
        protected override bool ProcessContextMenu(Point location)
        {
            TokenizedTextBlockElement token = this.TextBox.ElementTree.GetElementAtPoint(location) as TokenizedTextBlockElement;
            this.TextBox.Tag = token;
            return base.ProcessContextMenu(location);
        }
    }

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Aleksander
    Aleksander avatar
    2 posts
    Member since:
    Aug 2016

    Posted 16 Aug in reply to Dess Link to this post

    Thanks Dess, this solution works perfectly!
Back to Top