Thursday, May 24, 2012

Adding a Table to a RichTextBox Control in C#

As I have searched, there are not many tutorials, examples, or projects specifically to C# for how to going about adding a user defined table to a RichTextBox control. One of last stands for a programmer would be to program a custom control for them self that would perform what they could not already find. What everybody's goal in development is going the best affordable rout and as 'this' example is so simple, it would be kind of pointless to shell out +$250 on a library that does the same thing.

Note: for developers programming in VB.Net, try this link; it basically does just what the code in C# does - in fact, I used that link to construct this code by example. WPF, Java, ASP.Net, and Silverlight devs, there are plenty of tutorials and examples already... kind of annoying when your trying to search for only pure Windows Forms - C#!

To the code! I have tested this method bellow and it works... there was a side effect though that I couldn't fix so I went the long rout, but still looks the same. Greater success to those who can fix that.

public void InsertTable(RichTextBox Rtb, int Rows, 
        int Cols, int BrdrW, int TableWidth)
{
    String A; int i = 1, j = 1, a = 1, b = 1;
    A = @"{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033" +
        @"{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}" +      
        @{\f1\fnil\fcharset0 Microsoft Sans Serif;}}" +
        @"\viewkind4\uc1\trowd";
    for (; j <= Rows; j++) // for every row
    {
        for (; i <= Cols; i++) // for every column
        {
            A += @"\clbrdrt\brdrw" + BrdrW + "\brdrs" +
                 @"\clbrdrl\brdrw" + BrdrW + "\brdrs" +
                 @"\clbrdrb\brdrw" + BrdrW + "\brdrs" +
                 @"\clbrdrr\brdrw" + BrdrW + "\brdrs" +
                 @"\cellx" + ((TableWidth / Cols) * 10 * i);
        }
        A += @"\pard\intbl\ltrpar\lang1023\f0\fs24";
        for (; a <= Rows; a++)
        {
            A += @"\intbl\clmrg";
            for (; b <= Cols; b++) A += @"\cell";
            A += @"\row";
        }
    }
    A += "}";

    Rtb.SelectedRtf = A; // send table to RichTextBox 
    Rtb.Rtf = Rtb.Rtf.Replace("\\\'08rdrs", ""); 
    // (\'08rdrs seems to be the side effect here)

    // center table in RichTextBox (and everything else XP )
    Rtb.SelectAll();
    Rtb.SelectionAlignment = HorizontalAlignment.Center;
}

If you would also like to combine this method with this project, you'll be one step closer to creating your own Microsoft Word Program.
I also came across something interesting relating to RichTextBox's, maybe it may be of use to any of you viewing. Check it out!

5 comments:

  1. Replies
    1. That is something paid for. My blog is about doing these king of things for FREE as in Mono/Linux, not NET/Windows.

      Sorry.

      Delete
  2. \cellx is your column width measured in twips. 15 twips = 1 pixel. So if you want a cell 100px wide, include the line "\cellx"+(100*15);

    ReplyDelete
  3. This link explains how to add table in richtextbox control using C#
    http://www.dotnetstuffs.com/create-table-in-richtextbox-using-c-sharp/

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete