[ Dwiz @ 19.09.2004. 19:32 ] @
Kako u delhiju napraviti da se u polje edit ne može unijeti tekst, samo brojevi. Da se ništa ne ispisuje kada tipkam slova, nego samo kada tipkam brojeve. |
[ Dwiz @ 19.09.2004. 19:32 ] @
[ bancika @ 19.09.2004. 19:41 ] @
pa mozes malo na silu, na OnChange stavi
Code: i := 1; while i <= Length(Edit1.Text) do if not (Edit1.Text[i] in ['0'..'9']) then Delete(Edit1.Text, i, 1) else Inc(i); moguce da Edit1.Text ne moze da se stavi kao parametar u Delete, al mozes da koristis pomocni string. Bolje resenje je da presretnes poruku i da promenis, samo sto to nisam najsigurniji. Stavi ApplicationEvents komponentu i pod OnMessage stavi Code: if (Msg.message = WM_KEYDOWN) and (Edit1.HasFocus) and not (Msg.wParam in [Ord('0')..Ord('9')]) then Msg.wParam := 0; nemoj da me drzis za rec, ovo ti je samo ideja, ovako iz glave :)\ pozdrav [ Almedin @ 19.09.2004. 19:43 ] @
Obrađuj događaj OnKeyPress. Ako nije pritisnut broj uradi Key := Chr(0).
[ kefalo @ 20.09.2004. 16:33 ] @
a zasto ne bi koristio maskedit?
[ Dwiz @ 20.09.2004. 17:23 ] @
Ja bih u to polje trebao nekada unesiti npr. 3,00 a nekada 30,00, kako to napraviti s maskeditom?
[ franjo_tahi @ 21.09.2004. 08:18 ] @
Radio sam kontrolicu koja bi trebala biti to sto ti treba. Nije skroz zgotovljena, ali vidis bit. Sad sam na poslu, doma imam noviju verziju koja bolje radi. ako ti treba, posaljem ti poslije 20h.
unit NumEdit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, mask; type TNumEdit = class(TMaskEdit) private { Private declarations } FDecimals : Integer; // Number of decimals for rounding FKeyPressed : Boolean; // Keypressed FAlignment : TAlignment; Function RoundedText(aText : String) : String; // Perform the rounding procedure KeyPress(var Key: Char); override; procedure DoExit; override; procedure DoEnter; override; procedure Change; override; procedure SetDecimal(Br:integer); Function GetNumber:Extended; procedure SetNumber(Br:Extended); property EditMask; public { Public declarations } Constructor Create (AOwner : TComponent); override; procedure CreateParams(var Params: TCreateParams); override; procedure SetAlignment(Value: TAlignment); property KeyPressed : boolean read FKeyPressed write FKeyPressed default False; property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify; published { Published declarations } //new properties property Decimals : Integer read FDecimals write SetDecimal; property Number:Extended read GetNumber write SetNumber; //publishing properties we want property Anchors; property AutoSelect; property AutoSize; property BiDiMode; property BorderStyle; property CharCase; property Color; property Constraints; property Ctl3D; property DragCursor; property DragKind; property DragMode; property Enabled; property Font; property HideSelection; property ImeMode; property ImeName; property MaxLength; property OEMConvert; property ParentBiDiMode; property ParentColor; property ParentCtl3D; property ParentFont; property ParentShowHint; property PasswordChar; property PopupMenu; property ReadOnly; property ShowHint; property TabOrder; property TabStop; property Text; property Visible; property OnChange; property OnClick; property OnDblClick; property OnDragDrop; property OnDragOver; property OnEndDock; property OnEndDrag; property OnEnter; property OnExit; property OnKeyDown; property OnKeyPress; property OnKeyUp; property OnMouseDown; property OnMouseMove; property OnMouseUp; property OnStartDock; property OnStartDrag; end; procedure Register; implementation procedure TNumEdit.SetNumber(Br:Extended); begin text:=RoundedText(floattostr(br)); end; function TNumEdit.GetNumber:Extended; begin try result := strtofloat(text); except result := 0; end; end; procedure TNumEdit.SetDecimal(Br:integer); var s:string; //i:integer; begin FDecimals := Br; s:=RoundedText(Text); //i:=pos(DecimalSeparator, s); self.EditMask := '!'+StringOfChar('9', MaxLength - Br -1)+DecimalSeparator+StringOfChar('9', Br); self.Text := s; end; Constructor TNumEdit.Create (AOwner : TComponent); begin Inherited Create(AOwner); SetAlignment(taRightJustify); MaxLength := 10; EditMask := '!9999999'+DecimalSeparator+'99;1;_'; FDecimals := 2; self.Text := '0.00'; end; procedure TNumEdit.CreateParams(var Params: TCreateParams); const Alignments: array[Boolean, TAlignment] of DWORD = ((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER)); begin inherited CreateParams(Params); with Params do begin Style := Style or ES_MULTILINE or Alignments[UseRightToLeftAlignment, FAlignment]; end; end; procedure TNumEdit.SetAlignment(Value: TAlignment); begin if FAlignment <> Value then begin FAlignment := Value; RecreateWnd; end; end; Function TNumEdit.RoundedText(aText : String) : String; //Perform rounding the text var x:integer; S1,S2:STRING; begin S1:=''; S2:=''; X:=1; WHILE (X <= LENGTH(aTEXT)) AND (ATEXT[X] <> DecimalSeparator) do begin if aText[x] in ['0'..'9'] then s1:=s1+aText[x]; x:=x+1; end; if s1 = '' then s1:='0'; while x <= length(aText) do begin if aText[x] in ['0'..'9'] then s2:=s2+aText[x]; x:=x+1; end; if length(s2) < FDecimals then s2:=s2+StringOfChar('0', FDecimals-length(s2)); //FmtStr(Result,'%0.'+ IntToStr(RoundingDecimals) +'f',[StrToFLoat(Text)]); result := s1+DecimalSeparator+s2; KeyPressed := false; end; procedure TNumEdit.Change; begin //if not KeyPressed then //begin // if (Text <> '') then // Text := RoundedText(Text); //end end; procedure TNumEdit.DoEnter; // Perform rounding when entering var i:integer; begin if (Text <> '') then begin Text := RoundedText(Text); i:=pos(DecimalSeparator, Text)-1; if i < 1 then i:=length(text); SelStart := 0; SelLength := i; end; end; procedure TNumEdit.DoExit; // Perform rounding when exiting begin if (Text <> '') then Text := RoundedText(Text); end; procedure TNumEdit.KeyPress(var Key: Char); begin if key in [',', '.'] then key:=DecimalSeparator; KeyPressed := true; if key = DecimalSeparator then text:=RoundedText(Text); Inherited KeyPress(Key); // For user events //Check for Numeric and backspaces {if not (Key in [#8, '0'..'9', '-', DecimalSeparator]) then begin KeyPressed := False; key:=#0; beep; end else //Check for existing ofdecimalsepatator and '-' if ((Key = DecimalSeparator) or (key = '-')) and (Pos(Key, Text) > 0) then begin If Key = DecimalSeparator then begin SelStart := Pos(Key, Text); SelLength := Length(Text); end; key:=#0; end else // If '-' then first check position at front if (Key = '-') and (Selstart <> 0) then begin key:=#0; beep; end;} end; procedure Register; begin RegisterComponents('Moje', [TNumEdit]); end; end. [ Anarki @ 22.09.2004. 21:04 ] @
obradi OnChange event i izbrishi svaki uneti znak koji nije cifra i eventualno zarez, ako radish sa realnim brojevima
POz [ Dwiz @ 03.10.2004. 18:42 ] @
Skužio sam uz pomoć Delphi helpa. Ovo je kod:
Code: procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin case key of '0': ; '1': ; '2': ; '3': ; '4': ; '5': ; '6': ; '7': ; '8': ; '9': ; ',': ; '.': ; else abort; end; Jednostavno, zar ne!!!! [ Milos D @ 04.10.2004. 13:05 ] @
Ili, jednostavnije:
if not (Key in ['0'..'9', '.', ',']) then Key := #0; Samo, to nece spreciti nadahnutog korisnika da ukuca npr. "21..,,,.,.,.,.1.21..21.21.2.1" ;) [ Nemanja Avramović @ 04.10.2004. 13:40 ] @
A sta od ovoga reaguje na copy|paste? Jel ste razmisljali o tome. Mrzi me da probam, samo cisto informativno pitam...
[ Dwiz @ 04.10.2004. 20:06 ] @
Ovo je samo osnovna ideja koju treba još doraditi!
Copyright (C) 2001-2025 by www.elitesecurity.org. All rights reserved.
|