Can only enter a number of controls in D7 (like the maskedit of PB)

zhaozj2021-02-12  134

I found that there is no use of only the number that can only be used in Delphi. So I wrote one (D7).

One of the rookies, I hope everyone will point out. Ha ha. . .

UNIT COMERMASKEDIT;

Interface

Uses Windows, Messages, Graphics, Forms, Sysutils, Classes, Controls, Stdctrls

type TComerMaskEdit = class (TEdit) private FMdNumber: string; FIpo: Integer; FLen: Integer; procedure SetMdNumber (const Value: string); procedure WMPaste (var Message: TMessage); message WM_PASTE; {Private declarations} protected procedure CreateWnd; override ; {Protected declarations} public constructor Create (AOwner: TComponent); override; procedure DoEnter (); override; procedure DoExit (); override; procedure KeyPress (var Key: Char); override; procedure KeyDown (var Key: Word; Shift : Tshiftstate); OVERRIDE; {public declarations} Published Property MDNumber: String Read FMDNumber Write setmdNumber; {Published Declarations} END;

PROCEDURE register;

IMPLEMentation

Procedure Register; Begin RegisterComponents ('Standard', [TComermasKedit]); END;

{TcomermasKedit}

Constructor tcombermaskedit.create (Aowner: Tcomponent); begin inherited create (aowner); font.size: = 12; font.name:= 'Song body'; font.charset: = GB2312_Charset; fmdnumber: = '; end;

Procedure TcomberMasked; Var i, k: integer; s, str: string; begin inherited; if not enabled dam, font.color: = clnavy; font.size: = 12; font.name: = 'Song'; font. Charset: = GB2312_Charset; if fmdNumber <> '' Then Begin i: = POS (',', fmdnumber); if i> 0 THEN BEGIN S: = COPY (FMDNumber, 1, I-1); FIPO: = STRTOINT COPY (FMDNumber, i 1, Length)); for K: = 1 to FIPO Do Str: = Str '0'; Text: = '.' Str; Else Begin S: = fMDNumber FIPO: = 0; Text: = '; End; Maxlength: = STRTOINT (S); Flen: = MaxLength; IMEMODE: = Imclose; End; End; Procedure TcomberMaskedit.SETMDNUMBER (const value: string); var s: String; begin if value <> '' Then Begin s: = stringreplace (value, ',', '', [rfignorecase]); try strt (s); Except Application.MessageBox ('attribute value setting is not right!' , 'Error', MB_OK MB_ICONERROR); FMDNumber: = ''; exit; end; end; fmdnumber: = value;

Procedure tcomermasKedit.doenter (); begin inherited; selstart: = 0;

Procedure tcomermaskedit.doExit (); begin if (fipo> 0) and (POS ('.', text) = 0) THEN TEXT: = Text '.' stringofchar ('0', FIPO) else inherited; end;

procedure TComerMaskEdit.KeyPress (var Key: Char); var I, k: Integer; AfterDot, BeforSelStart: string; //, AfterSelStart, Str: string; TmpText: string; iSelStart: Integer; // BeforComma, AfterComma: String; begin // If there is a number of characters selected if selLength> 0 Then SELSTART: = 0 else Begin if fmdnumber <> '' Then Begin IF (length (text) = 0) And (FIPO> 0) THEN BEGIN for K: = 1 TO FIPO Do Str: = STR '0'; Text: = '.' Str; End; Case Key Of # 13: Inherited; '-': Begin IF (SELSTART <> 0) OR (POS ('-' , TEXT)> 0) The key: = # 0 else // maxlength: = maxlength 1; inherited; end; # 8: Begin i: = POS ('.', Text); if (i> 0) and (i> 0) SELSTART> I) THEN Begin Key: = # 0; ISELSTART: = SELSTART ;TMPTEXT: = text; beforselstart: = Copy (TmpText, 1, ISELSTART-1); Text: = BeforStart Copy (TMPText, Iselstart 1, Length (TMPText) -ielstart) '0'; SELSTART: = ISELSTART - 1; END ELSE IF (i> 0) and (selstart = i) THEN Begin Key: = # 0; ISELSTART: = SELSTART;

SELSTART: = ISELSTART - 1; END ELSE INHERITED; END; '0' .. '9': begin i: = POS ('.', Text); // Limited Bit IF i> 0 Then Begin if Selstart = Length (Text) The key: = # 0 else begin afterdot: = COPY (Text, i 1, length (text) -fipo); if length (afterdot)> FIPO THEN key: = # 0 else if selstart> = I Then Begin ISELSTART: = SELSTART ;TMPTEXT: = text; beforselstart: = COPY (TmpText, 1, ISELSTART); Text: = BeforStart Copy (TMPText, ISELSTART 2, Length (TmpText) -iselstart-1); SELSTART: = ISELSTART; END ELSE INHERITED; END; END ELSE IF (i <= 0) And (FIPO> 0) THEN / / This situation does not exist begin if longth (text)> = flen-fipo-1 Then Key : = # 0 else inherited; END ELSE / / FIPO = 0 inherited; end; '.'

: Begin key: = # 0; if fipo> 0 THEN SELSTART: = POS ('.', Text); end; Else Key: = # 0; End; Else // key: = # 0; inherited; END;

procedure TComerMaskEdit.KeyDown (var Key: Word; Shift: TShiftState); var I: Integer; BeforSelStart: string; TmpText: string; iSelStart: Integer; begin // if there are more characters to be selected if SelLength> 0 then begin Key: = 0; SELSTART: = 0; END ELSE BEGIN IF FMDNUMBER <> 'TEN BEGIN IFIN I: = POS ('. ', Text); if (i> 0) and (SELSTART> = i) then begin key: = 0; iSelStart: = SelStart; TmpText: = Text; BeforSelStart: = Copy (TmpText, 1, iSelStart); if (SelStart = Length (Text)) then Text: = BeforSelStart Copy (TmpText, iSelStart 2, Length (TMPText) -iselstart-1) else text: = BeforStart Copy (TmpText, ISELSTART 2, Length (TmpText) -iselstart-1) '0'; SELSTART: = ISELSTART; END Else if (i> 0) and (selstart = i-1) Then Begin key: = 0; ISELSTART: = SELSTART; SELSTART: = ISELSTART 1; END; ELSE IF (Key = VK_TAB) OR (key = vk_left) OR (key = vk_right) or (key = vk_nd) or (key = vk_end) or (key = vk_home) THENERITED ELSE KEY: = 0; ELSE INHERITED; END;

Procedure TcombermasKedit.wmpaste (Var Message: TMESSAGE); begin if fmdnumber = '' THEN inherited; end; end;

转载请注明原文地址:https://www.9cbs.com/read-6627.html

New Post(0)