unit QcurrEdit;
interface
uses
SysUtils, Classes, QControls, QStdCtrls,Qdialogs,strUtils;
type
TcurrEdit =
class(TEdit)
private
FDecimalDigits: Integer;
FIntDigits: Integer;
FPrefix:
string;
fSufix:
string;
FValue:real;
procedure UptText;
procedure SetDecimalDigits(
const Value: Integer);
procedure SetintDigits(
const Value: Integer);
procedure SetValue(newvalue:real);
{ Private declarations }
protected
procedure Loaded;
override;
procedure KeyPress(
var Key: Char);
override;
procedure doEnter;
override;
procedure doExit;
override;
procedure Click;
override;
{ Protected declarations }
public
constructor create(Aowner:tcomponent);
override;
procedure Clear;
override;
{ Public declarations }
published
property IntDigits:integer
read FintDigits
write SetIntDigits;
property DecimalDigits: Integer
read FDecimalDigits
write SetDecimalDigits;
{ Published declarations }
property Value : real
read Fvalue
write SetValue;
property Prefix:
string read fPrefix
write fPrefix;
property Sufix:
string read fSufix
write fSufix;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('
Byosoft', [TcurrEdit]);
end;
constructor tcurredit.create(Aowner:tcomponent);
begin
inherited Create(Aowner);
Alignment:=taRightJustify;
Prefix:='
';
Sufix:='
';
end;
procedure TcurrEdit.Loaded;
begin
inherited;
upttext;
end;
procedure TcurrEdit.clear;
var ch:char;
begin
ch:=chr(8);
keypress(ch);
end;
procedure TCurrEdit.SetValue(newvalue:real);
begin
fValue:=newvalue;
text:=floattostr(fvalue);
doexit;
end;
procedure TCurrEdit.uptText;
begin
Text:=prefix+'
0'+DecimalSeparator;
while length(text)-2-length(prefix)<DecimalDigits
do
Text:=Text+'
0';
Text:=Text+Sufix;
end;
procedure TcurrEdit.SetDecimalDigits(
const Value: Integer);
begin
FDecimalDigits:=Value;
UptText;
end;
procedure TcurrEdit.SetIntDigits(
const Value: Integer);
begin
FintDigits := Value;
UptText;
end;
procedure TCurrEdit.click;
begin
//Doenter;
end;
procedure TcurrEdit.doEnter;
begin
if Alignment =taLeftJustify
then exit;
{avoid double entering!! }
Alignment:=taLeftJustify;
Text:=rightstr(leftstr(Text,length(text)-length(sufix)),length(text)-length(prefix));
SelStart:=0;
SelLength:=length(text)-1;
end;
procedure TcurrEdit.doExit;
begin
Alignment:=taRightJustify;
SelLength:=0;
if (text='
')
or (text=DecimalSeparator)
then text:='
0';
text:=format('
%'+inttostr(IntDigits)+'
.'+inttostr(DecimalDigits)+'
f',[strtofloat(text)]);
Fvalue:=strtofloat(text);
text:=prefix+text+sufix;
end;
procedure TCurrEdit.KeyPress(
var Key: Char);
begin
if not (ord(key)
in [8,ord(DecimalSeparator),44,46,48..57])
then
begin
key:=chr(0);
exit;
end;
if ord(key)
in [ord(DecimalSeparator),44,46]
then
begin
if (pos(key,Text)<>0)
and (pos(key,SelText)=0)
then key:=chr(0)
else key:=DecimalSeparator;
end;
inherited;
end;
end.