{Functions: analog Nokia phones input controls, inherited from TCustomEdit Author: CoolSlob support: CoolSlob # 163.com Last updated: 2004/12/11} unit NokiaEdit;
Interface
Uses Sysutils, Classes, Controls, Stdctrls, Extctrls, Dialogs;
Type keyset = -1..9; // - 1 indicates an error
TNokiaEdit = class (TCustomEdit) private FInterval: Integer; FKeyset: TStrings; procedure SetKeyset (const Value: TStrings); function GetTimerEnabled: Boolean; procedure SetTimerEnabled (const Value: Boolean); function GetTextLength: Integer; {Private declarations} protected {Protected declarations} fTimer: TTimer; procedure OnTimer (Sender: TObject); virtual; property TimerEnabled: Boolean read GetTimerEnabled write SetTimerEnabled; procedure KeyPress (var Key: Char); override; function IndexByChar (const Key: Char): KeysetInt; function SearchNextChar ( const Index: KeysetInt; Key: Char): Char; function SearchFirstChar (const Index: KeysetInt): Char; function EndOfChar (const Value: string): Char; public {public declarations} constructor Create (AOwner: TComponent); override; destructor DESTROY; OVERRIDE;
property TextLength: Integer read GetTextLength; published {Published declarations} property MaxLength default 1; property Interval: Integer read FInterval write FInterval; property Keyset: TStrings read FKeyset write SetKeyset; property Font; end;
PROCEDURE register;
IMPLEMENTATION
Procedure Register; Begin RegisterComponents ('Coolslob', [TNOKIaEdit]); END;
{TnokiaEdit}
Constructor TnokiaEdit.create (Aowner: Tcomponent); begin inherited; fkeyset: = tstringlist.create; ftimer: = TTIMER.CREATE (Self); ftimer.ontimer: = Ontimer; END;
destructor TNokiaEdit.Destroy; begin FKeySet.Free; FKeySet: = nil; inherited; end; function TNokiaEdit.EndOfChar (const Value: string): Char; begin if Value = '' then Result: = # 0 else Result: = Value [ Length (value)];
Function TnokiaEdit.getTextLength: Integer; Begin Result: = Length (TRIM (TEX));
Function TnokiaEdit.getTimerenAding: Boolean; Begin Result: = ftimer.enabled;
Function TnokiaEdit.indexBychar (const key: char): keysetint; // var i: integer; begin result: = -1; if not assigned (fkeyset) THEN EXIT; for i: = 0 to fkeyset.count - 1 do begin if POS (key, fkeyset.strings [i])> 0 THEN BEGIN Result: = i; bug; end; end;
Procedure TnokiaEdit.KeyPress (var key: char); var curindex, newindex: keysetint; s: string; curchar: char; selected: boolean; begin inherited; if key in ['0' .. '9'] The begin ftimer. Enabled: = False; if TextLength = 0 then CurIndex: = -1 else CurIndex: = IndexByChar (Text [Length (Text)]); NewIndex: = Ord (Key) - 48; if CurIndex = NewIndex then begin CurChar: = EndOfChar (Text); s: = text; selected: = wordbool (sellength); delete (s, selstart 1, sellength; text: = s; if (not successd) or (curchar = ') THEN TEXT: = text SearchFirstchar (curIndex) else text: = text searchnextchar (curIndex, curchar); Else text: = text searchfirstchar (newIndex);
SELSTART: = Length (Text) - 1; Sellength: = 1; ftimer.enabled: = true; key: = # 0; end; end;
procedure TNokiaEdit.OnTimer (Sender: TObject); begin SelLength: = 0; SelStart: = Length (Text); FTimer.Enabled: = False; end; function TNokiaEdit.SearchFirstChar (const Index: KeysetInt): Char; var S: string Begin Result: = # 0; s: = fkeyset.strings [index]; if s <> '' Then Result: = s [1];
Function TnokiaEdit.SearchnextChar (const index: keyset): char; var i: integer; s: string; begin s: = fkeyset.strings [index]; i: = pos (key, s); if i = Length (s) THEN i: = 1 else i: = i 1; result: = s [i];
Procedure TnokiaEdit.setKeySet; Begin if Assigned (FKEYSET) THEN FKEYSET.Assign (Value) Else FKEYSET: = Value;
Procedure TnokiaEdit.SetTimerenabled (const value: boolean); begin if ftimer.enabled <> value; ftimer.enabled: = value;
End.