This is a migrated thread and some comments may be shown as answers.
AutoCompleteBox: Cancel creation of new tag
3 Answers 29 Views
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 02 May 2013, 07:15 PM
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

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 07 May 2013, 08:29 AM
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.
0
Jason Parrish
Top achievements
Rank 1
answered on 07 Oct 2013, 03:14 PM
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?
0
Stefan
Telerik team
answered on 10 Oct 2013, 04:49 PM
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 >>
Asked by
John
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Jason Parrish
Top achievements
Rank 1
Share this question
or