AutoCompleteBox: Cancel creation of new tag

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

    Posted 02 May 2013 Link to this post

    Telerik,

    If my program detects that new tag has been created, then I would like to have a message box prompting the user if they want to create the new tag.  If they choose 'No' then the tag will not be created.

    How would I cancel the creation of a new tag?

    Thanks
  2. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 07 May 2013 Link to this post

    Hello John,

    Thank you for writing.

    You can do that by using the TokenValidating event of RadAutoCompleteBox:
    void radAutoCompleteBox1_TokenValidating(object sender, TokenValidatingEventArgs e)
    {
        if (RadMessageBox.Show("Should I create the following item: " + e.Text, "Action needed", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
        {
            e.IsValidToken = false;
        }
    }

    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. Jason Parrish
    Jason Parrish avatar
    87 posts
    Member since:
    Sep 2009

    Posted 07 Oct 2013 Link to this post

    This only cancels the creation of the token.  However, it does not clear the invalid text.  It does disappear while the dialog is opened, but then shows back up in the textbox.

    If my user enters a duplicate token, I want it to be removed.  How can I do this?
  5. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 10 Oct 2013 Link to this post

    Hi Jason,

    One solution that comes to my mind is to override the method inserting this text in the AutoCompleteBoxViewElement. To do that you need to create a descendant of the control, the RadAutoCompleteBoxElement and the AutoCompleteBoxViewElement. Here is the code for this:
    class MyAutoCompleteBox : RadAutoCompleteBox
    {
        protected override RadTextBoxControlElement CreateTextBoxElement()
        {
            return new MyRadAutoCompleteBoxElement();
        }
     
        public override string ThemeClassName
        {
            get
            {
                return typeof(RadAutoCompleteBox).FullName;
            }
        }
    }
     
    class MyRadAutoCompleteBoxElement : RadAutoCompleteBoxElement
    {
        protected override TextBoxViewElement CreateViewElement()
        {
            return new MyAutoCompleteBoxViewElement();
        }
     
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(RadAutoCompleteBoxElement);
            }
        }
    }
     
    class MyAutoCompleteBoxViewElement : AutoCompleteBoxViewElement
    {
        protected override int InsertTokenizedTextBlocks(int index, string text, bool performInvalidation)
        {
            if (this.OnTokenValidating(text) == false)
            {
                return -1;
            }
     
            StringBuilder textBuilder = new StringBuilder();
     
            for (int i = 0; i < text.Length; i++)
            {
                char symbol = text[i];
                bool isDelimiter = symbol == this.Delimiter;
     
                if (isDelimiter)
                {
                    if (textBuilder.Length > 0)
                    {
                        string blockText = textBuilder.ToString();
                        bool isValid = true;
     
                        if (isValid)
                        {
                            ITextBlock textBlock = this.CreateBlock(blockText, TokenizedTextBlockElementType);
                            textBlock.Index = index;
                            this.Children.Insert(index, textBlock as RadElement);
                        }
                        else
                        {
                            textBuilder.Append(symbol);
                            blockText = textBuilder.ToString();
                            index = base.InsertTextBlocks(index, blockText, TextBlockElementType);
                        }
     
                        index++;
                        textBuilder = new StringBuilder();
                    }
     
                    continue;
                }
                else if (symbol == TextBoxViewElement.LineFeedSymbol ||
                         symbol == TextBoxViewElement.CarriageReturnSymbol)
                {
                    if (textBuilder.Length >= 0)
                    {
                        textBuilder.Append(symbol);
                        index = base.InsertTextBlocks(index, textBuilder.ToString(), TextBlockElementType) + 1;
                        textBuilder = new StringBuilder();
                        continue;
                    }
                }
     
                textBuilder.Append(symbol);
            }
     
            if (textBuilder.Length > 0)
            {
                index = base.InsertTextBlocks(index, textBuilder.ToString(), TextBlockElementType) + 1;
            }
     
            return index - 1;
        }
    }

    Let me know how this works for you.

    Regards,
    Stefan
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top