Delete entire text in rtb programmatically

5 posts, 1 answers
  1. Karl Golling
    Karl Golling avatar
    226 posts
    Member since:
    Jun 2005

    Posted 08 Aug 2011 Link to this post

    Hi,

    How do you simply delete the entire content of a richtextbox programmatically?

    Thanks,
    Karl
  2. Answer
    Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 10 Aug 2011 Link to this post

    Hello Karl,

    Thank you for writing.

    RadRichTextBox holds its content in RadDocument. Setting the Document property of RadRichTextBox to a new instance of RadDocument will clear the previous document will its data:
    radRichTextBox1.Document = new RadDocument();

    I hope that the provided information addresses your question. Should you have any other questions, do not hesitate to contact us.

    Kind regards,
    Stefan
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Wayne
    Wayne avatar
    36 posts
    Member since:
    May 2013

    Posted 18 Oct 2013 Link to this post

    I started with that, but then began to wonder if I was introducing any additional memory overhead by creating a new document instead of simply using a "Clear" method of some type.

    In my situation, I'm creating an on-the-fly preview of text, and the document will be re-created several times as the user goes from place to place in another part of the screen.  Will I incur unnecessary memory overhead from creating a new document repeatably?

    Would it be advisable to create several documents that I'll swap in and out of view as I need them?

    Thanks!

    Wayne
  5. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 22 Oct 2013 Link to this post

    Hello Wayne,

    In general, the old instance of the Document will not be referenced anywhere and it will be collected by the Garbage Collector when needed, so this should not incur any memory overhead. 

    Alternative method to clear a document will be to simply select its content and delete it:
    this.radRichTextBox1.Document.Selection.SelectAll();
    this.radRichTextBox1.Document.Delete(false);

    You can use this one if you prefer.

    I hope this helps.

    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 >>
  6. Wayne
    Wayne avatar
    36 posts
    Member since:
    May 2013

    Posted 22 Oct 2013 Link to this post

    Wonderful!  Thanks!
Back to Top
UI for WinForms is Visual Studio 2017 Ready