Inserting and Deleting Text Fields

Text fields can be inserted into a Text Control either by adding them to one of the field collections or with the TextControl.Load method as a part of a document. The following programming code creates a new text field, obtains the TextFieldCollection object and adds the field using the TextFieldCollection.Add method:

[Visual Basic]
Dim NewField As New TextField("new field")
NewField.ShowActivated = True
NewField.DoubledInputPosition = True
TextControl1.TextFields.Add(NewField)
[C#]
TextField newField = new TextField("new field");
newField.ShowActivated = true;
newField.DoubledInputPosition = true;
textControl1.TextFields.Add(newField);

This code inserts a text field that is shown with a gray background and with a doubled input position at the beginning and the end as explained in the chapter Editing Marked Text Fields.

A new field is always inserted at the current input position. If there is already a field an error occurs. To get the information whether a text field can be inserted at the current input position the CanAdd property can be used. To insert a field at a position other than the current one, first move the input position using the following code:

[Visual Basic]
Dim NewField As New TextField("new field")
TextControl1.InputPosition = New InputPosition(1, 3, 0)
TextControl1.TextFields.Add(NewField)
[C#]
TextField newField = new TextField("new field");
textControl1.InputPosition = new InputPosition(1, 3, 0);
textControl1.TextFields.Add(newField);

This code inserts the field on the first page, in the third line, at position zero. position zero is the position in front of the first character.

To remove a marked text field from the document use the collection's Remove method as shown with the following code:

[Visual Basic]
Dim tfc As TextFieldCollection = TextControl1.TextFields
Dim Field As TextField = tfc.GetItem(200)
If Not Field Is Nothing Then
    tfc.Remove(Field)
End If
[C#]
TextFieldCollection tfc = textControl1.TextFields;
TextField textField = tfc.GetItem(200);
if (textField != null) {
    tfc.Remove(textField);
}

This code removes the text field with the identifier 200. How to relate and use identifiers for text fields is described in the chapter Identifiers and Names.

If a text field is added during editing, a TextControl.TextFieldCreated event occurs. This can happen, if a piece of text containing a field is inserted from the clipboard, or if a field is restored through an undo/redo operation. This event does not occur, if a document containing fields is loaded, or if a field is added from programming code.

If a text field is deleted during editing, a TextControl.TextFieldDeleted event occurs. This can happen, if a selection of text containing a field is deleted, or if a field is deleted through an undo/redo operation. The event does not occur, if a field is deleted from program code.

The event handlers for these events also receive an argument of type TextFieldEventArgs. The TextField object that can be obtained through the TextField property identifies the field that has caused the event. If a field has been deleted, only the TextField.ID and the Textfield.Name properties provide valid values.