Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

Apply a boxed frame style to selected table cells

This source code snippet requires TX Text Control .NET
Author:TX Text Control Support Department
Language:C#
Version:1.0
Released:June 12, 2007
Last modified:January 11, 2008
Requirements:TX Text Control .NET with C#
Download code:tx_table_border.zip Download [14 KB, ZIP]

The dialog box that ships with TX Text Control allows end-users to adjust table borders. Amongst others, the following pre-defined styles can be set:

  • None
  • Box
  • All
  • Grid

Internally, the settings are adjusted for each cell. The following function illustrates how to programmatically set a boxed border around all selected table cells.

First of all, we need to store the character index start and end positions of the current selection, in order to get the selected cells.

  1. int selStart = textControl1.Selection.Start;
  2. int selEnd = textControl1.Selection.Start + textControl1.Selection.Length;
  3. TXTextControl.Table curTable = textControl1.Tables.GetItem();

In the next step, we need to find out which is the first cell and the last cell in the range of the currently selected characters. A System.Drawing.Point is used as a container to hold the values for the specific cell's row and column.

  1. textControl1.Selection.Start = selStart;
  2. textControl1.Selection.Length = 0;
  3. Point firstCell = new Point(textControl1.Tables.GetItem().Cells.GetItem().Row, _
  4. textControl1.Tables.GetItem().Cells.GetItem().Column);
  5.  
  6. textControl1.Selection.Start = selEnd - 1;
  7. textControl1.Selection.Length = 0;
  8. Point lastCell = new Point(textControl1.Tables.GetItem().Cells.GetItem().Row, _
  9. textControl1.Tables.GetItem().Cells.GetItem().Column);

Finally, we have to iterate through all selected cells, in order to adjust the border. In this sample, two nested for loops are used to iterate through the rows and each column of the selected cells. The left border of the first cell in a row and the right border of the last cell is set. The first and last row are slightly different, in that the cells in the former have a top border set, while those in the latter have a bottom border set.

  1. for (int row = firstCell.X; row <= lastCell.X; row++)
  2. {
  3. for (int col = firstCell.Y; col <= lastCell.Y; col++)
  4. {
  5. if (row == firstCell.X)
  6. curTable.Cells.GetItem(row, col).CellFormat.TopBorderWidth = 1;
  7.  
  8. if (row == lastCell.X)
  9. curTable.Cells.GetItem(row, col).CellFormat.BottomBorderWidth = 1;
  10.  
  11. if (col == firstCell.Y)
  12. curTable.Cells.GetItem(row, col).CellFormat.LeftBorderWidth = 1;
  13.  
  14. if (col == lastCell.Y)
  15. curTable.Cells.GetItem(row, col).CellFormat.RightBorderWidth = 1;
  16. }
  17. }

The minimum requirements for this sample application are TX Text Control .NET trial version and C# 2005.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place