In our support forums, users often asks us about how to set the input position to specific positions in the document. Even if this task sounds trivial, the question is not easy to answer. The reason is that there are many ways to set the caret to a character index position or to calculate the required index. It depends on which property values are known in your calculation to set the input position. Maybe you need to set the caret to the beginning of a TextField or at the end of a specific Page.

The following two properties can be used to manipulate the input position:

Selection.Start gets or sets the starting point of the selected text. The first possible value is 0 and the last is the length of the text. Each element in the document such as tables, images, lines or pages provide a property that can be used in combination with Selection.Start. The following code shows how to iterate through all tables and their cells in order to add text to each cell:

foreach (TXTextControl.Table table in textControl1.Tables)
{
    foreach (TXTextControl.TableCell cell in table.Cells)
    {
        textControl1.Selection.Start = cell.Start - 1;
        textControl1.Selection.Text = "Cell " +
            table.Cells.GetItem().Row.ToString() +
            ", " + table.Cells.GetItem().Column.ToString();
    }
}

The TableCell.Start property is used to set the Selection.Start value. As you can see in the code, the value 1 is subtracted from the Start value. The reason is that there is a difference between the input position and the character position in a document. The following illustration shows that the input position is in front of a character position. When passing the Start value from a TableCell or TextField to the Selection.Start property, you need to subtract 1 from it's value.

TX Text Control input positions

The InputPosition Constructor offers other possibilities to set the input position. This class can be used to set the input position when you know an X or Y location, or the specific page, column and row numbers. The following implementations of the constructor are available:

public InputPosition(int page, int line, int column);
public InputPosition(int textPosition);
public InputPosition(int textPosition, TextFieldPosition textFieldPosition);
public InputPosition(Point location);

The first constructor creates a new input position from a page, line and column number, the second and third from a text position, which is comparable to the Selection.Start approach, and the fourth from a geometric location. The following code snippet shows how to set the input position to the current mouse position using the MouseMove event.

private void textControl1_MouseMove(object sender, MouseEventArgs e)
{
    Graphics g = textControl1.CreateGraphics();
    int dpi = (int)(1440 / g.DpiX);

    Point newInputPosition = new Point(
        (e.X * dpi) +
        textControl1.ScrollLocation.X,
        (e.Y * dpi) +
        textControl1.ScrollLocation.Y);

    textControl1.InputPosition =
        new TXTextControl.InputPosition(newInputPosition);
}