Extension methods have been added to the .Net Framework in version 3.0 and enable developers to add additional functionality to existing classes. I personally like this way more than creating an inherited class. Based on this concept, the TX Text Control class can be extended with typical tasks such as an AutoSize functionality that adjusts a table to fit the cell content automatically.

Extending the Table object: AutoSize

The following code shows the extender method AutoSize. It loops through all cells of the given table, checks the length of all lines and resizes each columns based on the longest detected lines:

public static class TableExtender
{
        public static void AutoSize(this Table table, TextControl TextControl)
        {
                int[] iColWidths = new int[table.Columns.Count];

                foreach (TXTextControl.TableCell tc in table.Cells)
                {
                        int iTextBounds = 0;

                        // check the width of every line in a cell
                        for (int i = tc.Start; i <= tc.Start + tc.Length - 1; i++)
                        {
                                // pick width, if the current one is the longest
                                if (TextControl.Lines.GetItem(i).TextBounds.Width > iTextBounds)
                                {
                                        iTextBounds = TextControl.Lines.GetItem(i).TextBounds.Width;
                                }
                        }

                        // pick the width, if it is the longest in the whole column
                        if (iTextBounds > (int)iColWidths.GetValue(tc.Column - 1))
                                iColWidths.SetValue(iTextBounds, tc.Column - 1);
                }

                // resize the table according to the filled array
                for (int iColCount = 1; iColCount <= table.Columns.Count; iColCount++)
                {
                        if ((int)iColWidths.GetValue(iColCount - 1) != 0)
                        {
                                TableColumn tcColumn = table.Columns.GetItem(iColCount);

                                tcColumn.Width =
                                        (int)iColWidths.GetValue(iColCount - 1) +
                                        tcColumn.CellFormat.RightTextDistance +
                                        tcColumn.CellFormat.LeftTextDistance +
                                        tcColumn.CellFormat.RightBorderWidth +
                                        tcColumn.CellFormat.LeftBorderWidth;
                        }
                }
        }
}

This method can be called directly on a TXTextControl.Table object. The following code resizes the table at the current input position:

textControl1.Tables.GetItem().AutoSize(textControl1);

The TableExtender class in this sample also contains a method to spread the table columns to fit the page width. The combination of both enables you to maximize the number of characters in a single column.

You can download the sample project and test it on your own. At least, a TX Text Control .NET for Windows Forms trial version is required.