Two TextField Tricks with TX Text Control .NET for Windows Forms
I just fished two little TextField code snippets out of our source code pool that is being filled during the daily support work. 1. Navigating to the next TextField This snippet shows how to set the input position to the next TextField, if the current input position is inside of another TextField and the user presses the TAB key. private void textControl1_KeyPress(object sender, KeyPressEventArgs e) { TXTextControl.TextField curField = textControl1.TextFields.GetItem(); if (curField == null…

I just fished two little TextField code snippets out of our source code pool that is being filled during the daily support work.
1. Navigating to the next TextField
This snippet shows how to set the input position to the next TextField, if the current input position is inside of another TextField and the user presses the TAB key.
private void textControl1_KeyPress(object sender, KeyPressEventArgs e)
{
TXTextControl.TextField curField = textControl1.TextFields.GetItem();
if (curField == null || e.KeyChar != 9) return;
bool bIsFieldMatch = false;
foreach (TXTextControl.TextField field in textControl1.TextFields)
{
if (bIsFieldMatch == true)
{
textControl1.InputPosition = new TXTextControl.InputPosition(
field.Start - 1,
TXTextControl.TextFieldPosition.InsideTextField);
break;
}
if (field.Start == curField.Start)
bIsFieldMatch = true;
}
e.Handled = true;
}
2. Ensure that the input position is always inside a TextField
You might know the doubled input position of TextFields that enables an input position inside and outside of a TextField. From our documentation:
If a marked text field is fully editable, the two text input positions at the beginning and the end of a marked text field have a special meaning. If an end-user inserts text at one of these positions, it is not unique whether or not the newly inserted text belongs to the field. To solve this problem, the TextField class offers the DoubledInputPosition property. This implements a second input position, which the end-user can reach with the Left Arrow and Right Arrow keyboard keys.
If the user is not used to this doubled input position, it might be confusing, if the user is setting the caret to the beginning of a TextField, but the text he is typing is not inside the field. The following snippet ensures that the input position is always inside of the TextField, if the user sets the caret to the character position before the field:
private void textControl1_InputPositionChanged(object sender, EventArgs e)
{
foreach (TXTextControl.TextField field in textControl1.TextFields)
{
if (textControl1.Selection.Start == field.Start - 1)
{
textControl1.InputPosition = new TXTextControl.InputPosition(
textControl1.Selection.Start,
TXTextControl.TextFieldPosition.InsideTextField);
break;
}
}
}
Do you have similar requirements and you are not sure how to solve it? Request such snippets by opening a support case here:
Related Posts
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.
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…
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…
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.