# Extending the Table Object: AutoSize

> A C# extension method adds AutoSize functionality to the TX Text Control Table class. The method iterates through each cell, measures line widths using the TextBounds property, and resizes each column to fit its content while properly accounting for cell padding and border widths.

- **Author:** Bjoern Meyer
- **Published:** 2014-05-08
- **Modified:** 2026-07-17
- **Description:** A C# extension method adds AutoSize functionality to the TX Text Control Table class. The method iterates through each cell, measures line widths using the TextBounds property, and resizes each column to fit its content while properly accounting for cell padding and border widths.
- **2 min read** (209 words)
- **Tags:**
  - Tutorial
- **Web URL:** https://www.textcontrol.com/blog/2014/05/08/extending-the-table-object-autosize/
- **LLMs URL:** https://www.textcontrol.com/blog/2014/05/08/extending-the-table-object-autosize/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2014/05/08/extending-the-table-object-autosize/llms-full.txt

---

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](https://s1-www.textcontrol.com/assets/dist/blog/2014/05/08/a/assets/tx_table_autosize.webp "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](https://docs.textcontrol.com/textcontrol/windows-forms/ref.txtextcontrol.table.class.htm) 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](https://s1-www.textcontrol.com/assets/dist/blog/2014/05/08/a/assets/tx_table_autofit.zip) and test it on your own. At least, a [TX Text Control .NET for Windows Forms trial version](https://www.textcontrol.com/product/tx-text-control-dotnet-winforms/download/) is required.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Windows Forms Tutorial: Create Your First Windows Forms C# Application](https://www.textcontrol.com/blog/2024/08/26/windows-forms-tutorial-create-your-first-windows-forms-csharp-application/llms.txt)
- [How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#](https://www.textcontrol.com/blog/2023/10/16/how-to-mail-merge-ms-word-docx-documents-in-aspnet-core-csharp/llms.txt)
- [Creating an Angular Document Editor Application with a Node.js WebSocket Server](https://www.textcontrol.com/blog/2023/08/24/creating-an-angular-document-editor-application-with-a-nodejs-websocket-server/llms.txt)
- [Adding SVG Watermarks to Documents](https://www.textcontrol.com/blog/2022/01/28/adding-svg-watermarks-to-documents/llms.txt)
- [Using MailMerge in ASP.NET Core 6 Web Applications](https://www.textcontrol.com/blog/2022/01/27/using-mailmerge-in-aspnet-core-6-web-applications/llms.txt)
- [DocumentViewer for React Prerelease](https://www.textcontrol.com/blog/2020/10/27/document-viewer-for-react-prerelease/llms.txt)
- [New DocumentViewer Signature Tutorial Sample](https://www.textcontrol.com/blog/2020/08/18/new-documentviewer-signature-tutorial-sample/llms.txt)
- [Creating an ASP.NET MVC DocumentViewer Application With Razor](https://www.textcontrol.com/blog/2020/01/01/creating-an-aspnet-mvc-documentviewer-application-with-razor/llms.txt)
- [Creating Your First Windows Forms Application with C#](https://www.textcontrol.com/blog/2020/01/01/creating-your-first-windows-forms-application-with-csharp/llms.txt)
- [Creating Your First WPF Application](https://www.textcontrol.com/blog/2020/01/01/creating-your-first-wpf-application/llms.txt)
- [Creating a WPF Ribbon Application](https://www.textcontrol.com/blog/2020/01/01/creating-a-wpf-ribbon-application/llms.txt)
- [Integrate Document Editing into any HTML Client using the HTML Widget](https://www.textcontrol.com/blog/2020/01/01/integrate-document-editing/llms.txt)
- [Creating an ASP.NET MVC Application With Razor](https://www.textcontrol.com/blog/2020/01/01/creating-an-aspnet-mvc-application-with-razor/llms.txt)
- [Creating A Windows Forms Ribbon Application](https://www.textcontrol.com/blog/2020/01/01/creating-a-windows-forms-ribbon-application/llms.txt)
- [Creating Your First ASP.NET Reporting Application](https://www.textcontrol.com/blog/2020/01/01/creating-your-first-aspnet-reporting-application/llms.txt)
- [Creating an ASP.NET Web Forms AJAX Application](https://www.textcontrol.com/blog/2020/01/01/creating-an-aspnet-web-forms-ajax-application/llms.txt)
- [Creating a WebSocket Server Project with Node.js](https://www.textcontrol.com/blog/2020/01/01/creating-a-websocket-server-project-with-nodejs/llms.txt)
- [Creating an Angular Document Editor Application](https://www.textcontrol.com/blog/2020/01/01/creating-an-angular-document-editor-application/llms.txt)
- [ReportingCloud .NET Core Quickstart Tutorial](https://www.textcontrol.com/blog/2019/07/24/reportingcloud-dotnet-core-quickstart-tutorial/llms.txt)
- [Document Permissions and Password Encryption](https://www.textcontrol.com/blog/2019/07/05/document-permissions-and-password-encryption/llms.txt)
- [New Online Sample: Build your First Report](https://www.textcontrol.com/blog/2019/07/03/build-your-first-report/llms.txt)
- [Create your First Document with ReportingCloud](https://www.textcontrol.com/blog/2019/02/19/create-your-first-document-with-reportingcloud/llms.txt)
- [MailMerge: Starting Each Merge Block on a New Page](https://www.textcontrol.com/blog/2016/09/09/mailmerge-starting-each-merge-block-on-a-new-page/llms.txt)
- [Windows Forms and WPF: End a List on Return when Line is Empty](https://www.textcontrol.com/blog/2016/08/26/windows-forms-and-wpf-end-a-list-on-return-when-line-is-empty/llms.txt)
- [Using IFormattedText Objects to Access Elements Across All TextParts in a Document](https://www.textcontrol.com/blog/2016/08/25/using-iformattedtext-objects-to-access-elements-across-all-textparts-in-a-document/llms.txt)
