Inserting a Table with Text Fields

Tables are often used to display data and TX Text Control's ApplicationField type text fields are the perfect tool to merge data into tables. The whole procedure can be divided into three simple steps:

The following code inserts a 6 x 3 table and adds the header texts to the first table row.

To format the first table row's text bold, the cursor is set to the first text position in the first table cell with

textControl1.Selection.Start = table.Cells.GetItem(1, 1).Start - 1;.

The lenght of the selection is calculated with

textControl1.Selection.Length = table.Cells.GetItem(1, 3).Start - 1 + table.Cells.GetItem(1, 3).Length;.

As the Selection class is zero-based and the TableCell text index one-based, it is necessary to subtract one from the TableCell text index.

[C#]
int tableID = 10;
textControl1.Tables.Add(6, 3, tableID);
TXTextControl.Table table = textControl1.Tables.GetItem(tableID);
table.Cells.GetItem(1, 1).Text = "Date / Time";
table.Cells.GetItem(1, 2).Text = "Customer data";
table.Cells.GetItem(1, 3).Text = "Result";
textControl1.Selection.Start = table.Cells.GetItem(1, 1).Start - 1;
textControl1.Selection.Length = table.Cells.GetItem(1, 3).Start - 1
                               + table.Cells.GetItem(1, 3).Length;
textControl1.Selection.Bold = true;
textControl1.Selection.ParagraphFormat.Alignment = TXTextControl.HorizontalAlignment.Center;
textControl1.Selection.Length = 0;
[Visual Basic]
Dim TableID As Integer = 10
TextControl1.Tables.Add(6, 3, tableID)
Dim Table As TXTextControl.Table = TextControl1.Tables.GetItem(TableID)
Table.Cells.GetItem(1, 1).Text = "Date / Time"
Table.Cells.GetItem(1, 2).Text = "Customer data"
Table.Cells.GetItem(1, 3).Text = "Result"
TextControl1.Selection.Start = Table.Cells.GetItem(1, 1).Start - 1
TextControl1.Selection.Length = Table.Cells.GetItem(1, 3).Start - 1 _
                               + Table.Cells.GetItem(1, 3).Length
TextControl1.Selection.Bold = True
TextControl1.Selection.ParagraphFormat.Alignment = TXTextControl.HorizontalAlignment.Center
TextControl1.Selection.Length = 0

Next, the TableCellCollection is looped to insert variable data, like date, customer number and result. The customer number will be inserted in an ApplicationField text field.

[C#]
foreach (TXTextControl.TableCell cell in table.Cells)
{
  if (cell.Row == 1)
    continue;
  if (cell.Column == 1)
    cell.Text = DateTime.UtcNow.ToString();
  if (cell.Column == 2)
  {
    textControl1.Selection.Start = cell.Start - 1;
    TXTextControl.ApplicationField appField = new TXTextControl.ApplicationField(
                                             TXTextControl.ApplicationFieldFormat.MSWord,
                                             "MERGEFIELD",
                                             cell.Row.ToString(),
                                             new string[] { "FieldName" });
    appField.DoubledInputPosition = true;
    appField.ShowActivated = true;
    textControl1.ApplicationFields.Add(appField);
    textControl1.Selection.ParagraphFormat.Alignment = TXTextControl.HorizontalAlignment.Center;
  }
  if (cell.Column == 3)
    cell.Text = Guid.NewGuid().ToString().Substring(1, 10);
}
[Visual Basic]
For Each cell As TXTextControl.TableCell In table.Cells
  If cell.Row = 1 Then
    Continue For
  End If
  If cell.Column = 1 Then
    cell.Text = DateTime.UtcNow.ToString()
  End If
  If cell.Column = 2 Then
    textControl1.Selection.Start = cell.Start - 1
    Dim appField As New TXTextControl.ApplicationField(
            TXTextControl.ApplicationFieldFormat.MSWord, _
            "MERGEFIELD", _
            cell.Row.ToString(), _
            New String() {"FieldName"})
    appField.DoubledInputPosition = True
    appField.ShowActivated = True
    textControl1.ApplicationFields.Add(appField)
    textControl1.Selection.ParagraphFormat.Alignment = TXTextControl.HorizontalAlignment.Center
  End If
  If cell.Column = 3 Then
    cell.Text = Guid.NewGuid().ToString().Substring(1, 10)
  End If
Next

<< Back