Search code examples
.netvb.netformswindows-forms-designerrtf

Adding resizable tables to a Richtextbox (.net or C#)


Our Capstone group is designing a stripped-down text editor for designing syllabus templates, one of the requirements is being able to add and remove resizable tables that function similarily to Microsoft Word (rtf) tables, they must be resizable by "dragging" as well as customizable fonts etc. They must also be placed in the RichTextBox, or at least in a way that can transition easily between regular text and a cell with text.

We have tried using different methods including tablelayoutpanel (doesn't work as it cannot be placed directly in the richtextbox), the closest we have found is using Stringbuilder, but this still does not work exactly as the cells cannot be resized, and the string itself is very difficult to manipulate. We also need the ability to "merge cells".

We have tried designing in both .net and C#, however the results are similar. If anyone has any suggestions as how to work this program, I would appreciate it. This is the code for the table creation:

  Private Sub tsbTwoRows_Click(sender As Object, e As EventArgs) Handles tsbTwoRows.Click

    Dim rtbTemp As New RichTextBox
    Dim sbTaRtf As New System.Text.StringBuilder

    'These strings are necessary so that it will be visible in MS Word
    sbTaRtf.Append("{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0 Calibri;}{\f1\fnil\fcharset0 Microsoft Sans Serif;}}")
    sbTaRtf.Append("\viewkind4\uc1\trowd\trgaph108\trleft5\trbrdrt\brdrs\brdrw10 \trbrdrl\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 ")

    sbTaRtf.Append("\clbrdrt\brdrw15\brdrs\clbrdrl\brdrw15\brdrs\clbrdrb\brdrw15\brdrs\clbrdrr\brdrw15\brdrs")
    sbTaRtf.Append("\cellx4678") 'set the width of the first cell
    sbTaRtf.Append("\clbrdrt\brdrw15\brdrs\clbrdrl\brdrw15\brdrs\clbrdrb\brdrw15\brdrs\clbrdrr\brdrw15\brdrs")
    sbTaRtf.Append("\cellx9355") 'set the width of the second cell

    sbTaRtf.Append("\pard\intbl\ltrpar\sl252\slmult1\lang3082\f0\fs22\cell\cell\cell\row")

    sbTaRtf.Append("\pard\ltrpar\lang1033\f1\fs17\par")
    sbTaRtf.Append("}")

    rtbTemp.Rtf = sbTaRtf.ToString()

    'This prevents the new table from deleting the text
    rtbContent.SelectedRtf = rtbTemp.Rtf

    rtbTemp.Undo()

    Me.rtbContent.Focus()
    Me.rtbContent.SelectionStart = Me.rtbContent.SelectionStart - 1
    Me.rtbContent.SelectionLength = 0

End Sub

enter image description here


Solution

  • Okay, after lots of trial and error we found that stringbuilder works rather erratically on .net, we tried the following code on c# and it worked much better:

    StringBuilder tableRtf = new StringBuilder();
            tableRtf.Append(@"{\rtf1");
            tableRtf.Append(@"\trowd");
            tableRtf.Append(@"\clbrdrt\brdrw15\brdrs\clbrdrl\brdrw15\brdrs\clbrdrb\brdrw15\brdrs\clbrdrr\brdrw15\brdrs");
            tableRtf.Append(@"\cellx1000");
    tableRtf.Append(@"\trrh3000");
            tableRtf.Append(@"\clbrdrt\brdrw15\brdrs\clbrdrl\brdrw15\brdrs\clbrdrb\brdrw15\brdrs\clbrdrr\brdrw15\brdrs");
            tableRtf.Append(@"\cellx3000");
    
            tableRtf.Append(@"\intbl \cell \row");
    
            tableRtf.Append(@"\pard");
            tableRtf.Append(@"}");
    
            string combined2 = tableRtf.ToString();
            string combined1 = this.TextEditor.Rtf.ToString();
    
    
            tableRtf.AppendLine(tableRtf.ToString());
    
            TextEditor.Select(TextEditor.TextLength, 0);
            TextEditor.SelectedRtf = tableRtf.ToString();
    

    This creates a string in the form of a cell that surrounds text you enter in it, you can change the font inside it easily, program the cell's height using \trrh and it can be resized both in the windows form program as well as in Word when it is exported. Initially, it wasn't visible when exported to Word so I added the tableRtf.Append(@"\clbrdrt\brdrw15\brdrs\clbrdrl\brdrw15\brdrs\clbrdrb\brdrw15\brdrs\clbrdrr\brdrw15\brdrs"); line and it was given a font recognized by Word. Most importantly, when typing inside the table and pressing enter to create a new line, the cell automatically resizes itself to fit the contents.