Products Technologies Demo Docs Blog Support Company

Setting the Input Position in TX Text Control

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…

Setting the Input Position in TX Text Control

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);
}

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.