Extending the Table Object: AutoSize
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. The following code shows the extender method AutoSize. It loops through all cells of the given table,…

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.

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.
Related Posts
Windows FormsGetting StartedTutorial
Windows Forms Tutorial: Create Your First Windows Forms C# Application
This tutorial shows how to create your first Windows Forms application with C# using TX Text Control .NET for Windows Forms in Visual Studio 2022.
How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#
Mail merge is the process of merging data, such as Json or IEnumerable objects, into a template document, such as a DOC or DOCX file. This tutorial is a walkthrough of the steps necessary to…
Creating an Angular Document Editor Application with a Node.js WebSocket Server
This tutorial shows how to create an Angular application that uses the Document Editor with a Node.js WebSocket server.
Adding SVG Watermarks to Documents
This article shows how to add SVG images to document section headers that repeat automatically on each page. This watermark will be inserted vertically and horizontally centered on each section page.
Using MailMerge in ASP.NET Core 6 Web Applications
This article shows how to use the TX Text Control ASP.NET MailMerge class to merge templates with JSON data within a .NET 6 application in Visual Studio 2022.