none
How do I set a length limit for the RICH TEXT of a RichTextBox?

    Question

  • WHAT I HAVE:

    Visual Basic 2015, WinForms, Entity Framework 6.1.3

    MY PROBLEM:

    I'm assigning the rich-text (Rtf property) to an entity property, and I want to limit the size of the property field's length. But the MaxLength property only sets the maximum size of the plain-text (Text property)--whereas the rich text generally is longer than the plain text by an undetermined length. Is there a way so set a maximum size for the total rich-text? 


    Robert Gustafson

    Friday, March 24, 2017 5:36 AM

Answers

  • Okay, then how does one reverse (prevent) the text-change, when in the TextChanged event? Simply checking for the length isn't useful unless one can readily enforce it.

    If you use the textchanged event you can't actually reverse the change.  You could assume that the most recent change is at the end of the text, and chop it off.  But a better approach would be to alert the user and tell them to reduce their text.  Better still is to show the text length in a label alongside the RTB and alert the user as they approach the maximum. Think about how you use the on-line comments box when filling in a form - the usual practice is for the user to go back and shorten earlier parts of the message while watching the length indicator until the maximum is no longer exceeded.

    You don't have to prevent extra text entry - you just have to be sure that it is adjusted before it is saved.  For instance, the validating event could be used to check the length and send the user back to make adjustments.  You can use both - textchanged to report the length, and validating to confirm that it is within the required size. It all depends on what sort of user experience you want to provide, but simply preventing additional entry when the maximum is hit is probably not the best.

    Saturday, March 25, 2017 9:13 PM

All replies

  • I'm assigning the rich-text (Rtf property) to an entity property, and I want to limit the size of the property field's length.

    You can use RTF.Length to get the current length of the RTF encoded text, and implement your own check for maximum length. That code would be in the TextChanged event handler for the control.

    Friday, March 24, 2017 5:59 AM
  • I think that if the [MaxLength] attribute is applied to the property that holds the RTF content, then it will count the length of the RTF code, not the length of the plain text.

    Or show the details about the usage of [MaxLength].

    Friday, March 24, 2017 6:41 AM
  • Viorel_ says:

    "I think that if the [MaxLength] attribute is applied to the property that holds the RTF content, then it will count the length of the RTF code, not the length of the plain text.

    "Or show the details about the usage of [MaxLength]."

    How does one do that? (And also, can I do it at run-time?)


    Robert Gustafson

    Saturday, March 25, 2017 6:30 PM
  • Acamar says:

    "You can use RTF.Length to get the current length of the RTF encoded text, and implement your own check for maximum length. That code would be in the TextChanged event handler for the control."

    Okay, then how does one reverse (prevent) the text-change, when in the TextChanged event? Simply checking for the length isn't useful unless one can readily enforce it.


    Robert Gustafson

    Saturday, March 25, 2017 6:34 PM
  • Okay, then how does one reverse (prevent) the text-change, when in the TextChanged event? Simply checking for the length isn't useful unless one can readily enforce it.

    If you use the textchanged event you can't actually reverse the change.  You could assume that the most recent change is at the end of the text, and chop it off.  But a better approach would be to alert the user and tell them to reduce their text.  Better still is to show the text length in a label alongside the RTB and alert the user as they approach the maximum. Think about how you use the on-line comments box when filling in a form - the usual practice is for the user to go back and shorten earlier parts of the message while watching the length indicator until the maximum is no longer exceeded.

    You don't have to prevent extra text entry - you just have to be sure that it is adjusted before it is saved.  For instance, the validating event could be used to check the length and send the user back to make adjustments.  You can use both - textchanged to report the length, and validating to confirm that it is within the required size. It all depends on what sort of user experience you want to provide, but simply preventing additional entry when the maximum is hit is probably not the best.

    Saturday, March 25, 2017 9:13 PM