franjo_tahi Franjo Tahi Zagreb
Član broj: 34712 Poruke: 399 *.adsl.net.htnet.hr
|
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.
|