none
Using seletedRTF in rich text box to build tables.

    Question

  • I am using a rich text box to populate sections of a document from a database.  I am also dynamically building tables with rows and cells with in the rich text box.  Everything seems to be working well except for sentences that are bigger than the cells.  Instead of the cells expanding to fit all of text, it allows the text to run out of the cell.  My question is, is there a way to have the rtf cell expand to fit all of the text?  I opened the rtf code in WordPad and it worked fine, but it still doesn't work in my rich text box project.  Any ideas of what I need to do to make it work in my Visual C# project?

    Here is an example of my rtf code:

    {\rtf1\ansi \par \trowd \trql\trgaph108\trrh280\trleft36\clbrdrt\brdrth \clbrdrl\brdrdb \clbrdrb\brdrdb \clbrdrr\brdrdb \cellx2636 \clbrdrt\brdrth \clbrdrl\brdrdb \clbrdrb\brdrdb \clbrdrr\brdrdb \cellx5236 \pard \intbl SC2116810, Rev. C\cell \pard \intbl Interface Control Document, Miniature Air Data Computer for AC-130H
    Aircraft (1 February 1990)\cell\row\pard\par}

     

    Wednesday, January 25, 2006 5:48 PM

Answers

  • I just wrote a simple application that converts between the RTF on a RichTextBox, and the RTF code, and back again. I think the issue is not that there is a bug in the RTF controls, but moreso that applications such as Word, WordPad etc. automatically do the table formatting for you. If you are only looking to output text into your table, then this shouldn't be an issue - there are font functions that allow you to calculate length of character text for a given font at different sizes, so you just need to work out how much space you need, and create the table borders yourself in code.

    See the following example for the parameters that I've changed to achieve a longer cell width in the second cell:

    RTF:

    {\rtf1\ansi \par \trowd \trql\trgaph108\trrh280\trleft36\clbrdrt\brdrth \clbrdrl\brdrdb \clbrdrb\brdrdb \clbrdrr\brdrdb \cellx2636 \clbrdrt\brdrth \clbrdrl\brdrdb \clbrdrb\brdrdb \clbrdrr\brdrdb \cellx11236 \pard \intbl SC2116810, Rev. C\cell \pard \intbl Interface Control Document, Miniature Air Data Computer for AC-130H
    Aircraft (1 February 1990)\cell\row\pard\par}

     

    Note that this same functionality precludes your text from wrapping when the width of your display pane decreses as well (ie: word-wrap doesn't occur either).

    Friday, January 27, 2006 4:40 PM

All replies

  • I was able to narrow down my rtf code for building a table in my richTextBox to this sample below.  But, it still doesn't take care of my problem of having the text stay within the cell.

    {\rtf1\ansi \par \trowd \ltrrow \cellx2636 \cellx5236 \pard \intbl SC2116810, Rev. C\cell Interface Control Document, Miniature Air Data Computer for AC-130H
    Aircraft (1 February 1990)\cell \row }

    Wednesday, January 25, 2006 9:52 PM
  • You might consider trying working backwards - get some RTF code that does expand automatically in your program (copy & paste from word/excel for example), and ensure that it expands as you expect when you type in the cells.

    Then, have a look at the Rtf property and use the code that's produced.

    Thursday, January 26, 2006 4:27 AM
  • Yes, I've tryed that.  I took code from a Word and WordPad document where I created a table and added lines of text in each cell.  When I copied the code into a C# richTextBox project it still over flows the cells.  Is this a known bug?
    Thursday, January 26, 2006 12:47 PM
  • A bug? I don't know. I know that sometimes when I try to copy & paste from Word/Excel into wordpad (write.exe), the table borders don't properly accomodate their contents.

    Try the following:

    • Create a table in Excel, copy it, and paste it into your C# Rich Text box
    • Create a table using HTML, and paste it into your C# Rich Text box
    •  Create a table using HTML, paste it into WordPad, then copy and paste it from there into your C# Rich Text box

    Just some ideas! Here's a test HTML table:

    test test
    test test


    Friday, January 27, 2006 5:21 AM
  • Thank you Christopher for your helpful suggestions, but non of them seem to work.  If I cut and paste the example below into my richTextBox I still have the same results. the larger paragraphs flow out of the cells.  Try pasting the table below into a Visual richTextBox and you'll see what I'm talking about.

    test

    test

    test to see if

    test now to see if this will work

    • Proposed as answer by bahushekh Wednesday, June 16, 2010 10:41 AM
    • Proposed as answer by bahushekh Wednesday, June 16, 2010 10:41 AM
    • Proposed as answer by bahushekh Wednesday, June 16, 2010 10:41 AM
    • Proposed as answer by bahushekh Wednesday, June 16, 2010 10:41 AM
    Friday, January 27, 2006 1:03 PM
  • I just wrote a simple application that converts between the RTF on a RichTextBox, and the RTF code, and back again. I think the issue is not that there is a bug in the RTF controls, but moreso that applications such as Word, WordPad etc. automatically do the table formatting for you. If you are only looking to output text into your table, then this shouldn't be an issue - there are font functions that allow you to calculate length of character text for a given font at different sizes, so you just need to work out how much space you need, and create the table borders yourself in code.

    See the following example for the parameters that I've changed to achieve a longer cell width in the second cell:

    RTF:

    {\rtf1\ansi \par \trowd \trql\trgaph108\trrh280\trleft36\clbrdrt\brdrth \clbrdrl\brdrdb \clbrdrb\brdrdb \clbrdrr\brdrdb \cellx2636 \clbrdrt\brdrth \clbrdrl\brdrdb \clbrdrb\brdrdb \clbrdrr\brdrdb \cellx11236 \pard \intbl SC2116810, Rev. C\cell \pard \intbl Interface Control Document, Miniature Air Data Computer for AC-130H
    Aircraft (1 February 1990)\cell\row\pard\par}

     

    Note that this same functionality precludes your text from wrapping when the width of your display pane decreses as well (ie: word-wrap doesn't occur either).

    Friday, January 27, 2006 4:40 PM
  • Halidon  - any more thoughts on this one?
    Wednesday, February 01, 2006 4:19 PM
  • Hi,

    Have you tried any other solution. I am also having a same problem on both VB.NET and VB 6.0. Exactly same problem as you.

    Please let me know, how you solved this.

    Many thanks

    Monday, July 17, 2006 4:01 PM
  • I am trying to display nested tables (2 levels) in RichTextBox control in .NET 2.0 C# windows application. It comes up right in Word/Wordpad but does not work in the control. I created one table (netsted and static) in word, saved it as RTF, but even that doesn't come up right. Any ideas?

    Thank you,
    Malay
    Tuesday, August 08, 2006 3:06 AM
  • I think you'll need to do all the calculations of the table widths etc. manually in code. There are font functions that return the number of pixels required to draw a string of text using a given font. You can use this to determine the width required for your tables.

    have a look at http://www.codeproject.com/cs/media/measurestring.asp for details...
    Tuesday, August 08, 2006 9:34 AM
  • I solved the problem of how to expand cell columns to fit text as follows (inVB6):

    1) Make a very small (invisible) rich text box control with horizontal and vertical scrollbars (remember to set right margin to a high number otherwise the horizontal scroll bar does not appear)
    2) Massage the rich text to replace cells with tabs
    3) Put the richtext that now contains tabs into the very small rich text box control
    4) In the very small rich text box control, set selstart to each successive tab position
    5) At each tab position, you can get the position of the tab using sendmessage + EM_GETSCROLLPOS
    6) Convert this position to twips
    7) Massage the cell positions in the original rich text using the positions just obtained
    8) The text now fits in the cells

    Hope you get the idea.  The basic trick is to use the positions of the scroll bars in the very small rich text box to give you the required positions of the cell/tab boundaries.  It is probably even unnecessary to replace cells with tabs, but I had to do it anyway in my code, so that's how I did it.

    Monday, May 05, 2008 12:33 AM
  • I use RTF Control in vb.net 2008,  how can I insert rtf code into RTF document to create the table.  Previously I only use rtf1,text = rtf.text & "..." to add text to the docuemnt.

     

    Thanks.

    Thursday, April 08, 2010 1:41 PM
  • Now I am able to create a table with multirows and several columns inthe RTF at a selected location.   But my cell background color and border lines are still not shown up, any ideas?

    Thanks.

     

    Friday, April 16, 2010 5:50 PM
  • Hi Peter,

    Can you plz let me know how you solved the issue of overflowing text in tables in rich text box.

    I will give you the brief of what i am doing..

    I have a application Created in VB.Net 2005 Express (Framework 2.0).

    There is an interface having richtextbox where i populate some existing text (as template), which has a table in it.

    The user has to fill it like a form and then i will store it and print it as well.

    When ever the text in any cell is bigger the cell dose not get re-sized, i realized that may be there RTFTextBox can not change the table tags of native RTF language.

    To resolve the situation i export the template and open a external application and ask the user to edit in that (Like wordpad or some thing).

    Then i get all the rtf data back but still is is overflowing.

    I even tried copy and past from word. It still overflows..

    Any idea what is going wrong?

    Regards

    Nishkarsh

    Tuesday, July 13, 2010 11:07 AM