使用updown老是不能处理带小数的情况,根据网上流传很广的一段代码,我改成了能接受一位小数的spinedit控件.高人见笑了 ( 积分: 0 )

  • 主题发起人 主题发起人 xuegaoyili
  • 开始时间 开始时间
X

xuegaoyili

Unregistered / Unconfirmed
GUEST, unregistred user!
unit UpDownEdit;

interface

uses
Windows, SysUtils, Classes, Controls, StdCtrls, ComCtrls, Messages;

type
TUpDownEdit = class(TCustomControl)
private
{ Private declarations }
UpDown: TUpDown;
Edit: TEdit;
FMin: Double;
FMax: Double;
FOnChange: TNotifyEvent;
FPosition: Double;
procedure WMSize(var Msg: TWMSize); message wm_Size;
procedure SetMax(const Value: Double);
procedure SetMin(const Value: Double);
procedure EditChange(Sender: TObject);
procedure EditKeyPress(Sender: TObject; var Key: Char);
procedure UpDownClick(Sender: TObject; Button: TUDBtnType);
procedure SetPosition(const Value: Double);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Max: Double read FMax write SetMax;
property Min: Double read FMin write SetMin;
property Position: Double read FPosition write SetPosition;
property OnChange: TNotifyEvent read FOnChange write FOnChange;

end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TUpDownEdit]);
end;

{ TUpDownEdit }

constructor TUpDownEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SetBounds(0, 0, 57, 21);
Edit := TEdit.Create(Self);
Edit.Left := 0;
Edit.Top := 0;
Edit.Width := 40;
Edit.Align := alLeft;
Edit.Parent := self;
Edit.Text := '0.0';
// SetWindowLong(Edit.Handle, GWL_STYLE, GetWindowLong(Edit.Handle, GWL_STYLE) or ES_NUMBER);

UpDown := TUpDown.Create(self);
UpDown.Height := Height; //20;
UpDown.Width := 20;
UpDown.Left := Edit.Width + 1;
UpDown.Parent := self;
FMin := 0.0;
FMax := 1000.0;

Edit.OnChange := EditChange;
Edit.OnKeyPress := EditKeyPress;
UpDown.OnClick := UpDownClick;
end;

destructor TUpDownEdit.Destroy;
begin
Edit.Free;
UpDown.Free;
inherited;
end;

procedure TUpDownEdit.EditChange(Sender: TObject);
begin
UpDown.Position :=trunc(strtofloat(Edit.Text)*10.0);
FPosition := UpDown.Position*1.0;
if Assigned(FOnChange) then
FOnChange(Self);
end;

procedure TUpDownEdit.EditKeyPress(Sender: TObject; var Key: Char);
var
s: set of char;
i: integer;
Str, Text: string;
begin
s := [#8, '0'..'9','.'];
if Key = #8 then exit;

if not (Key in s) then
begin
Key := #0;
Exit;
end;

//控制输入数字的大小
if TEdit(Sender).SelLength > 0 then
begin
Text := TEdit(Sender).Text;
Str := Copy(Text, 1, TEdit(Sender).SelStart - 1)
+ Key +
Copy(Text, TEdit(Sender).SelStart + TEdit(Sender).SelLength + 1, Length(Text));
i := StrToInt(Str);
if i > FMax then
begin
Key := #0;
Exit;
end;
end
else
if strtofloat(TEdit(Sender).Text + Key) > FMax then
begin
Key := #0;
Exit;
end
else
if strtofloat(TEdit(Sender).Text + Key) < FMin then
begin
Key := #0;
Exit;
end;

end;

procedure TUpDownEdit.SetMax(const Value: Double);
begin
FMax := Value;
UpDown.Max := trunc(FMax*10);
if strtofloat(Edit.Text) > FMax then
begin
UpDown.Position := trunc(FMax*10);
Edit.Text := floattostr(FMax);
FPosition := UpDown.Position/10.0;
end;
end;

procedure TUpDownEdit.SetMin(const Value: Double);
begin
FMin := Value;
UpDown.Min := trunc(FMin*10);
if strtofloat(Edit.Text) < FMin then
begin
UpDown.Position := trunc(FMin*10);
Edit.Text := floatToStr(FMin);
FPosition := UpDown.Position/10.0;
if Assigned(FOnChange) then
FOnChange(Self);
end;
end;

procedure TUpDownEdit.SetPosition(const Value:double);
begin
if (Value >= FMin) or (Value <= FMax) then
begin
FPosition := Value;
UpDown.Position := trunc(FPosition*10);
Edit.Text := floatToStr(FPosition);
if Assigned(FOnChange) then
FOnChange(Self);
end;
end;

procedure TUpDownEdit.UpDownClick(Sender: TObject; Button: TUDBtnType);
begin
if Max = 0 then
begin
Max := 1000;
UpDown.Max := trunc(Max*10);
end;
UpDown.Min := trunc(Min*10);
Edit.Text := floatToStr(UpDown.Position/10.0);
Edit.SetFocus;
Edit.SelectAll;
if Assigned(FOnChange) then
FOnChange(Self);
FPosition := UpDown.Position/10.0;
end;

procedure TUpDownEdit.WMSize(var Msg: TWMSize);
begin

Edit.Width := Width - 15;
UpDown.Left := Edit.Width + 1;
UpDown.Height := Height;
inherited;

end;

end.
 
unit UpDownEdit;

interface

uses
Windows, SysUtils, Classes, Controls, StdCtrls, ComCtrls, Messages;

type
TUpDownEdit = class(TCustomControl)
private
{ Private declarations }
UpDown: TUpDown;
Edit: TEdit;
FMin: Double;
FMax: Double;
FOnChange: TNotifyEvent;
FPosition: Double;
procedure WMSize(var Msg: TWMSize); message wm_Size;
procedure SetMax(const Value: Double);
procedure SetMin(const Value: Double);
procedure EditChange(Sender: TObject);
procedure EditKeyPress(Sender: TObject; var Key: Char);
procedure UpDownClick(Sender: TObject; Button: TUDBtnType);
procedure SetPosition(const Value: Double);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Max: Double read FMax write SetMax;
property Min: Double read FMin write SetMin;
property Position: Double read FPosition write SetPosition;
property OnChange: TNotifyEvent read FOnChange write FOnChange;

end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TUpDownEdit]);
end;

{ TUpDownEdit }

constructor TUpDownEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SetBounds(0, 0, 57, 21);
Edit := TEdit.Create(Self);
Edit.Left := 0;
Edit.Top := 0;
Edit.Width := 40;
Edit.Align := alLeft;
Edit.Parent := self;
Edit.Text := '0.0';
// SetWindowLong(Edit.Handle, GWL_STYLE, GetWindowLong(Edit.Handle, GWL_STYLE) or ES_NUMBER);

UpDown := TUpDown.Create(self);
UpDown.Height := Height; //20;
UpDown.Width := 20;
UpDown.Left := Edit.Width + 1;
UpDown.Parent := self;
FMin := 0.0;
FMax := 1000.0;

Edit.OnChange := EditChange;
Edit.OnKeyPress := EditKeyPress;
UpDown.OnClick := UpDownClick;
end;

destructor TUpDownEdit.Destroy;
begin
Edit.Free;
UpDown.Free;
inherited;
end;

procedure TUpDownEdit.EditChange(Sender: TObject);
begin
UpDown.Position :=trunc(strtofloat(Edit.Text)*10.0);
FPosition := UpDown.Position*1.0;
if Assigned(FOnChange) then
FOnChange(Self);
end;

procedure TUpDownEdit.EditKeyPress(Sender: TObject; var Key: Char);
var
s: set of char;
i: integer;
Str, Text: string;
begin
s := [#8, '0'..'9','.'];
if Key = #8 then exit;

if not (Key in s) then
begin
Key := #0;
Exit;
end;

//控制输入数字的大小
if TEdit(Sender).SelLength > 0 then
begin
Text := TEdit(Sender).Text;
Str := Copy(Text, 1, TEdit(Sender).SelStart - 1)
+ Key +
Copy(Text, TEdit(Sender).SelStart + TEdit(Sender).SelLength + 1, Length(Text));
i := StrToInt(Str);
if i > FMax then
begin
Key := #0;
Exit;
end;
end
else
if strtofloat(TEdit(Sender).Text + Key) > FMax then
begin
Key := #0;
Exit;
end
else
if strtofloat(TEdit(Sender).Text + Key) < FMin then
begin
Key := #0;
Exit;
end;

end;

procedure TUpDownEdit.SetMax(const Value: Double);
begin
FMax := Value;
UpDown.Max := trunc(FMax*10);
if strtofloat(Edit.Text) > FMax then
begin
UpDown.Position := trunc(FMax*10);
Edit.Text := floattostr(FMax);
FPosition := UpDown.Position/10.0;
end;
end;

procedure TUpDownEdit.SetMin(const Value: Double);
begin
FMin := Value;
UpDown.Min := trunc(FMin*10);
if strtofloat(Edit.Text) < FMin then
begin
UpDown.Position := trunc(FMin*10);
Edit.Text := floatToStr(FMin);
FPosition := UpDown.Position/10.0;
if Assigned(FOnChange) then
FOnChange(Self);
end;
end;

procedure TUpDownEdit.SetPosition(const Value:double);
begin
if (Value >= FMin) or (Value <= FMax) then
begin
FPosition := Value;
UpDown.Position := trunc(FPosition*10);
Edit.Text := floatToStr(FPosition);
if Assigned(FOnChange) then
FOnChange(Self);
end;
end;

procedure TUpDownEdit.UpDownClick(Sender: TObject; Button: TUDBtnType);
begin
if Max = 0 then
begin
Max := 1000;
UpDown.Max := trunc(Max*10);
end;
UpDown.Min := trunc(Min*10);
Edit.Text := floatToStr(UpDown.Position/10.0);
Edit.SetFocus;
Edit.SelectAll;
if Assigned(FOnChange) then
FOnChange(Self);
FPosition := UpDown.Position/10.0;
end;

procedure TUpDownEdit.WMSize(var Msg: TWMSize);
begin

Edit.Width := Width - 15;
UpDown.Left := Edit.Width + 1;
UpDown.Height := Height;
inherited;

end;

end.
 
小数是非连续的啊,在机器中表示时,所以用Spin,不通吧
 
后退
顶部