Products Technologies Demo Docs Blog Support Company

Selecting and Formatting TableCells in TX Text Control

This article shows how to select and format table cells in TX Text Control .NET for Windows Forms and TX Text Control .NET Server. The sample code shows how to select a table cell range and how to apply formatting to the selected cells.

Selecting and Formatting TableCells in TX Text Control

The supplied Table Properties dialog box can be used to apply a TableCellFormat to selected table cells. It is possible to apply a TableCellFormat to a single cell, a range of cells, or to the entire table. The dialog box can be opened by clicking the Properties button in the Table Layout ribbon tab.

Tables in TX Text Control

However, using a custom interface or formatting a table programmatically requires applying table formatting to selected cells.

Applying TableCellFormat to Selected Cells

The following code snippet shows how to apply a TableCellFormat to selected cells in a table. The TableCellFormat is created with a specific background color and a border style. The TableCellFormat is then applied to the selected cells in the table.

private void FormatSelectedTableCells(TableCellFormat cellFormat)
{
        Table table = textControl1.Tables.GetItem();

        if (table == null)
                return;

        Selection curSelection = new Selection(
                textControl1.Selection.Start,
                textControl1.Selection.Length);

        // Clear the current selection
        textControl1.Selection.Length = 0;

        // Determine the starting row and column
        TableCell tableCellStart = table.Cells.GetItem();
        int startRow = tableCellStart.Row;
        int startCol = tableCellStart.Column;

        // Set the selection to the last cell in the selected range
        textControl1.Selection.Start = curSelection.Start + curSelection.Length - 1;

        // Determine the ending row and column
        TableCell tableCellEnd = table.Cells.GetItem();
        int endRow = tableCellEnd.Row;
        int endCol = tableCellEnd.Column;

        // Loop through the selected range and apply formatting
        for (int row = startRow; row <= endRow; row++)
        {
                for (int col = startCol; col <= endCol; col++)
                {
                        TableCell cell = table.Cells.GetItem(row, col);
                        cell.CellFormat = cellFormat;
                }
        }
}

This function gets the starting row and column and the ending row and column values to loop through the selected range of table cells. Finally, all cells in the selection will be formatted by applying the specified format to the cells.

Selecting TableCells

Before applying a TableCellFormat to selected cells, the cells must be selected. The following code snippet shows how to select a range of cells in a table.

textControl1.Tables.Add(9, 9, 10);

Table table = textControl1.Tables.GetItem(10);
table.Select(2, 2, 4, 6);

This code inserts a table with nine rows and nine columns and selects the range of cells from row 2, column 2 to row 4, column 6.

Selecting Table Cells

After selecting the range of cells, the TableCellFormat can be applied to the selected cells as described in the previous section.

textControl1.Tables.Add(9, 9, 10);

Table table = textControl1.Tables.GetItem(10);
table.Select(2, 2, 4, 6);

FormatSelectedTableCells(new TableCellFormat()
{
        BackColor = Color.LightGray,
        BottomBorderWidth = 1,
});

Selecting Table Cells

After executing the code, the selected cells are formatted with a specific background color and a border style.

Conclusion

This article showed how to apply a TableCellFormat to selected cells in a table. The TableCellFormat can be applied to a single cell, a range of cells, or to the entire table. The TableCellFormat can be used to format the background color, the border style, and other properties of the selected cells.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

ASP.NETWindows FormsImages

Various Ways of Inserting Images into TX Text Control

TX Text Control provides various ways of inserting images into the document. This article shows different approaches to add images to a document from memory, from a file or from a URL.


ASP.NETWindows FormsWPF

Different Ways to Insert Images

Images can be added to a document from various sources: Physical files, streams or System.Drawing.Images. This article explains how to create images and to add them to a document.


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format

Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…