This is a migrated thread and some comments may be shown as answers.

any way to implement MaxLength property on RadRichTextBox?

4 Answers 181 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
li t
Top achievements
Rank 1
li t asked on 19 Jul 2011, 06:55 AM
Is there any way to implement MaxLength functionality on RadRichTextBox?
thanks.

4 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 22 Jul 2011, 10:09 AM
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!

0
Mike
Top achievements
Rank 1
answered on 01 Feb 2012, 03:42 PM
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
0
Mike
Top achievements
Rank 1
answered on 03 Feb 2012, 02:46 PM
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.
0
Iva Toteva
Telerik team
answered on 06 Feb 2012, 04:47 PM
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 >>
Tags
RichTextBox
Asked by
li t
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Mike
Top achievements
Rank 1
Share this question
or