any way to implement MaxLength property on RadRichTextBox?

5 posts, 0 answers
  1. li t
    li t avatar
    1 posts
    Member since:
    Apr 2010

    Posted 19 Jul 2011 Link to this post

    Is there any way to implement MaxLength functionality on RadRichTextBox?
    thanks.
  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 22 Jul 2011 Link to this post

    Hi Li T,

    RadRichTextBox does not have a MaxLength property, as it uses format providers to import and export its document and depending on the format you choose, the size of the exported document differs. Besides, it is not quite clear how tables, pictures, UI elements embedded in the document should be counted.
    We are considering implementing a TextBox-like control and it can have a property like that, however, the exact form is still not certain. Could you elaborate on your scenario? Does the text you are previewing/editing have any formatting and why do you need to limit the length of the content?

    Kind regards,
    Iva
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  3. Mike
    Mike avatar
    10 posts
    Member since:
    Jun 2011

    Posted 01 Feb 2012 Link to this post

    I have this situation too.  We basically need a dumbed down version of the RadRichTextBox.  We don't want tables, images, colored text, different fonts, bullet points.  All we want is text formatting (specifically bold, italics, underline), spell check, find/replace, cut, copy, and paste.  We are also sharing this control between wpf and silverlight.  We need a max length because the text here is only meant to be short, and we want to enforce that.  We previously did it by tying into the OnDocumentContentChanged event of the document with this method.
    private void OnDocumentContentChanged(object sender, EventArgs e)
                {
                    var noteText = new TxtFormatProvider().Export(Document);
                    var document = (sender as RadDocument);
                    if (noteText.Length > MaxLength && document != null)
                    {
                        document.DocumentContentChanged -= OnDocumentContentChanged;
                        document.CaretPosition.MoveToLastPositionInDocument();
                        document.Selection.SetSelectionStart(document.CaretPosition);
                        for (int i = 0; i < noteText.Length - MaxLength; ++i)
                        {
                            document.CaretPosition.MoveToPrevious();
                        }
                        document.Selection.AddSelectionEnd(document.CaretPosition);
                        _radRichTextBox.Delete(false);
                        document.DocumentContentChanged += OnDocumentContentChanged;
                    }
                }

    I originally started looking through the forums today because I noticed there is a DocumentChanging event in the RadRichTextBox, and I was curious if there were a way I could handle the content coming in before the document is changed.  Our current method leaves a flicker of text past what our max length.  Is there a way to get a hold of the new input on the DocumentChanging event?

    Aside from that, in your earlier post you mentioned a possible text box like control.  What would this entail?  Is it still a possibility? 

    Thanks for your help,
    Mike
  4. Mike
    Mike avatar
    10 posts
    Member since:
    Jun 2011

    Posted 03 Feb 2012 Link to this post

    So, it has recently come to my attention that the method I gave for how we are limiting the length of the text has a few drawbacks.  First, it places the delete on the undo stack, and if you try to undo it, it will simply redo this situation, so it effectively kills the undo.  Add that too the flicker I mentioned before, and this really isn't a very viable solution.
  5. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 06 Feb 2012 Link to this post

    Hello Mike,

    The DocumentChanging event is fired when you are assigning a new document to the Document property of RadRichTextBox or when you change the value of the property that RadRichTextBox's content is bound to (through a data provider). Therefore, it will not be applicable in your scenario.
    Unfortunately, currently there is no way to limit the content of RadRichTextBox at this point. We have logged this feature and will consider implementing it in one of the following releases.

    All the best,
    Iva Toteva
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top