Products Technologies Demo Docs Blog Support Company

Sample Code: How to Format Selected Table Cells

With the introduction of version 17.0, TX Text Control users can now select table columns with one click on the down arrow in the first table row. Or you can simply click and drag to select some cells. Additionally, you no longer have to select cells row-wise. Multiple cells can be selected in the middle of a table. The shipped dialog box can be used to modify the cell formatting of selected table cells. The following code shows how to format selected cells using the API instead of the…

Sample Code: How to Format Selected Table Cells

With the introduction of version 17.0, TX Text Control users can now select table columns with one click on the down arrow in the first table row. Or you can simply click and drag to select some cells. Additionally, you no longer have to select cells row-wise. Multiple cells can be selected in the middle of a table.

The shipped dialog box can be used to modify the cell formatting of selected table cells. The following code shows how to format selected cells using the API instead of the dialog box.

private void SetCellFormat(TableCellFormat CellFormat)
{
    Table table = textControl1.Tables.GetItem();

    if (table == null)
        return;

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

    textControl1.Selection.Length = 0;

    // start row and col
    int iTableStartRow = table.Cells.GetItem().Row;
    int iTableStartCol = table.Cells.GetItem().Column;

    textControl1.Selection.Start =
        curSelection.Start + curSelection.Length - 1;

    // end row and col
    int iTableEndRow = table.Cells.GetItem().Row;
    int iTableEndCol = table.Cells.GetItem().Column;

    // loop through all cells and check whether they are in the
    // range of selected cells
    foreach (TableCell cell in textControl1.Tables.GetItem().Cells)
    {
        if (cell.Row >= iTableStartRow &&
            cell.Row <= iTableEndRow &&
            cell.Column <= iTableEndCol &&
            cell.Column >= iTableStartCol)

            cell.CellFormat = CellFormat;
    }
}

The code pretty straightforward: Using the TableCellCollection.GetItem method, the table row and column at the beginning and the end of the selection is stored in a variable. Then, all cells are iterated using the TableCellCollection. Each cell's row and column property is checked whether it is in the range of the selected cells.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

Windows FormsWPF.NET

Create a Table of Contents in Windows Forms using C#

This article explains how to create a table of contents in Windows Forms using the ribbon or programmatically. Creating a table of contents is required to organize large documents.


ASP.NETWindows FormsWPF

Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub

This article gives a quick overview of the new repositories, their structure and our plans for the future.


ASP.NETJavaScriptDocument Editor

Detect Toggle Button Changes Using a MutationObserver

This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…


Windows FormsList.NET

Two Ways to Restart Numbered Lists in TX Text Control

In TX Text Control, numbered lists are continued by default and need to be reset when required. There is more than one way if you want to restart numbered lists in a document. In this article, two…


Windows FormsSampleShortcuts

Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More

This article shows how to disable CTRL + MOUSE WHEEL, implement zooming with keyboard and reset the zoom factor to its default value.