The supplied Table Properties dialog box can be used to apply a TableCellFormat TX Text Control .NET for Windows Forms
TXTextControl Namespace
TableCellFormat Class
An instance of the TableCellFormat class represents the formatting attributes of a table cell.
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;
}
}
}
view raw test.cs hosted with ❤ by GitHub

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);
view raw test.cs hosted with ❤ by GitHub

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,
});
view raw test.cs hosted with ❤ by GitHub

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.