TX Text Control Source Code Library

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

TX Text Control .NET for Windows Forms 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.

public class SpecialTextField : TXTextControl.TextField

The constructor of this class expects four parameters:

public SpecialTextField(TXTextControl.TextControl TextControl, string Text, 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:

m_tx.TextFieldLeft += new TXTextControl.TextFieldEventHandler(m_tx_TextFieldLeft); 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.

private void CleanTextField(TXTextControl.TextField TextField) { if(TextField.Length < m_maxlength) { TextField.Text += new String(m_fillChar, m_maxlength - TextField.Text.Length); } else if(TextField.Length > m_maxlength) { TextField.Text = TextField.Text.Remove(TextField.Text.Length - 1,1); } }

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 for Windows Forms 13.0.

 
 

Products

Support

Downloads

Corporate

Buy Now