| Skype: | TextControlSupport | |
| Orders: | 877-462-4772 |

| 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 |
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:
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.
int selStart = textControl1.Selection.Start; int selEnd = textControl1.Selection.Start + textControl1.Selection.Length; 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.
textControl1.Selection.Start = selStart; textControl1.Selection.Length = 0; Point firstCell = new Point(textControl1.Tables.GetItem().Cells.GetItem().Row, _ textControl1.Tables.GetItem().Cells.GetItem().Column); textControl1.Selection.Start = selEnd - 1; textControl1.Selection.Length = 0; Point lastCell = new Point(textControl1.Tables.GetItem().Cells.GetItem().Row, _ 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.
for (int row = firstCell.X; row <= lastCell.X; row++) { for (int col = firstCell.Y; col <= lastCell.Y; col++) { if (row == firstCell.X) curTable.Cells.GetItem(row, col).CellFormat.TopBorderWidth = 1; if (row == lastCell.X) curTable.Cells.GetItem(row, col).CellFormat.BottomBorderWidth = 1; if (col == firstCell.Y) curTable.Cells.GetItem(row, col).CellFormat.LeftBorderWidth = 1; if (col == lastCell.Y) curTable.Cells.GetItem(row, col).CellFormat.RightBorderWidth = 1; } }
The minimum requirements for this sample application are TX Text Control .NET trial version and C# 2005.