Search code examples
c#winforms.net-3.5rtf

How do I use the RichTextBox control?


I want my users to be able to enter text in a Rich Text box and the RTF output be saved to the database.

Currently I have the RichTextBox bound to the field in the database, but it strips formatting when it's saved. How can I prevent this?

Also, how can I attach a formatting toolbox, (like what's in Wordpad) so my users can change the formatting?

And, last, I can't right click in the box or use Ctrl+C, Ctrl+V or anything like that, why is that? How can I fix it?


Solution

  • Ok, there are alot of questions there, I'll try to answer what I can.

    1) Databinding is great for displaying data, but I personally NEVER use DataBinding for saving stuff back to the database. I've always found it to be "too much magic happening" and that I needed more control. Therefore, I would strongly suggest that you handle the insertion yourself (via LINQ to Sql or ADO.Net whatever). The RichTextBox control has an RTF property which you can get to do the insert manually into the database.

    2) As for getting formatting buttons on top, it's not as simple as changing a property and be done with it. You'll have to implement that custom control yourself. Have a look at this CodeProject article. IT's in VB.NET, but maybe it'll give you some ideas: http://www.vbdotnetheaven.com/UploadFile/scottlysle/WordProcessor09122006234320PM/WordProcessor.aspx

    3) Not quite sure...