Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
Syndicate this content

How to add additional functionality to a text field by inheriting from the TextField class

This source code snippet requires TX Text Control .NET
Author:TX Text Control Support Department
Language:C#
Version:1.0
Released:November 29, 2006
Last modified:January 11, 2008
Requirements:TX Text Control .NET with Visual Studio 2003
Download code:tx_sample_field_inherit.zip Download [7.57 KB, ZIP]

How to add additional functionality to a text field by inheriting from the TextField class

TX Text Control .NET offers a powerful set of text field functions in it's base class, for example to protect a field from changes (TextField.Editable) or from deletion (TextField.Deleteable).

In some applications, the field functionality must be enhanced. Consider a currency text field in an invoice or quotation that checks it's own validity. Or a field that allows only digits instead of letters.

This sample illustrates how to create a new text field class that offers a field with a fixed length. Missing characters in the field are filled up with a fill character like a dot ('.') or an 'x'.

The new class SpecialTextField inherits all properties and methods from the base class TextField.

  1. public class SpecialTextField : TXTextControl.TextField

The constructor of this class expects four parameters:

  1. public SpecialTextField(TXTextControl.TextControl TextControl, string Text, _
  2. int MaxLength, char FillChar)
  1. TextControl: The parent TextControl object. We need the handle to TextControl to attach some events to it automatically.
  2. Text: The initial string of the field.
  3. MaxLength: The maximum length of the field.
  4. FillChar: A character that is used to fill up the missing characters.

In the constructor, two events are attached to the parent TextControl instance:

  1. m_tx.TextFieldLeft += new TXTextControl.TextFieldEventHandler(m_tx_TextFieldLeft);
  2. m_tx.TextFieldChanged += new TXTextControl.TextFieldEventHandler(m_tx_TextFieldChanged);

These events are fired when the user changes the content of the field or leaves the field. On these events, a function is called that deals with the logic behind the special field.

  1. private void CleanTextField(TXTextControl.TextField TextField)
  2. {
  3. if(TextField.Length < m_maxlength)
  4. {
  5. TextField.Text += new String(m_fillChar, m_maxlength - TextField.Text.Length);
  6. }
  7. else if(TextField.Length > m_maxlength)
  8. {
  9. TextField.Text = TextField.Text.Remove(TextField.Text.Length - 1,1);
  10. }
  11. }

If the field is smaller than the maximum size, it is filled up with the specified fill character. If the field is longer, the field's text is truncated at the end.

There is an unlimited number of possibilities to adapt the field's functionality to the current application or task.

The sample requires Visual Studio 2003 and at least a trial version of TX Text Control .NET 13.0.

External verification page for ISO 9000:2000 certificate
ISO 9001:2000
certified