捐赠 | 广告 | 注册 | 发布 | 上传 | 关于我们    
  粤ICP备10103342号-1 DELPHI盒子 | 盒子文章 | 盒子问答悬赏 | 最新更新 | 盒子检索 | 下载中心 | 高级搜索    
  精品专区 | 繁體中文 | 奖励公告栏 | 直通车账号登陆 | 关闭GOOGLE广告 | 临时留言    
 
广告
评论:有关EIDT输入数字的控件
lavxcrkk 20326 2005/12/25 21:36:44
   这个控件还是有可取之处的,但是还有一些小问题,例如如果使用鼠标右键的粘贴后退出会报错,输入方式定的太死(例如只能在开始时输入负号)。还有一些符合习惯的方式,如按下Enter键时退出焦点,获得、退出焦点时框体颜色变化等。
   最近有人要我做一个类似的控件,主要是可以控制输入数字的范围,另外是否输入数字还是任意字符也可选。我把它粘上来,供大家参考,希望大家多提改进建议。
   另外感谢楼上的各位。
unit Cedit;

interface
uses
  SysUtils, Classes, StdCtrls, Windows,Messages,Graphics, Forms, Dialogs;

type
  TState=(StChar,StNumber);
  TCedit = class(TCustomEdit)
  private
    { Private declarations }
    FState:TState;
    FHintText:string;
    FMinInput:double;
    FMaxInput:double;
    function GetMinInput:double;
    function GetMaxInput:double;
    function checkformat(value:string):boolean;
    function IsPasteText(value:string):boolean;
    procedure SetMinInput(value:double);
    procedure SetMaxInput(value:double);
    procedure SetState(value:TState);
    procedure SetHintText(value:string);

  protected
    { Protected declarations }
    procedure KeyPress(var Key: Char); override;
    procedure doEnter;override;
    procedure doExit;override;
    procedure Click;override;

  public
    { Public declarations }
  published
    { Published declarations }
    constructor Create(AOwner:TComponent); override;
    property State:TState read FState write SetState default StChar;
    property HintText:string read FHintText write SetHintText;
    property MinInput:double read GetMinInput write SetMinInput;
    property MaxInput:double read GetMaxInput write SetMaxInput;
    property Text;
    property Color;
    property Enabled;
    property Font;
    property Hint;
    property ShowHint;
  end;


procedure Register;

implementation

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

constructor TCedit.Create(AOwner:TComponent);
begin
     {call the ancestors' concstructor }
     inherited Create(AOwner);
       {initialize value}
       State:=StChar;
       Text:='please input char';
       HintText:=Text;
       Hint:=Text;
       FMaxInput:=10000;
       FMinInput:=-10000;
       ShowHint:=True;
       Color:=clSilver;
       Font.Color:=clBlack;
end;

function TCedit.checkformat(value:string):boolean;
begin
  {check whether the text is digital format or not}
  if Pos('-',value)>1 then
     begin
        result:=false
     end
  else  if length(value)=0  then
          begin
          result:=true;
          end
  else if Pos('-',value)=1 then
          begin
          if Pos('.',value)=2 then
          begin
          result:=false;
          end
          else begin
          result:=true;
          end;
          end
  else  begin
        result:=true;
  end;
end;

function TCedit.IsPasteText(value:string):boolean;
var
i,j,k:integer;
begin
     result:=false;
     j:=0;
     k:=0;
     if text='' then
        begin
          result:=false;
        end
     else begin
          i:=1;
          while (i<=length(value))and(result=false) do
          begin
          if value[i]='.' then
          j:=j+1;
          if value[i]='-' then
          k:=k+1;
          if (not (value[i] in ['0'..'9','.','-'])) then
          begin
          result:=True;
          i:=i+1;
          end
          else begin
          i:=i+1;
          end;
          end;
      end;
      if (j>1) or (k>1) then
         result:=true;
end;

Function TCedit.GetMinInput:double;
begin
    result:=FMinInput;
end;

Function TCedit.GetMaxInput:double;
begin
   result:=FMaxInput;
end;

procedure TCedit.SetMinInput(value:double);
begin
     FMinInput:=value;
end;
procedure TCedit.SetMaxInput(value:double);
begin
     FMaxInput:=value;
end;


procedure TCedit.SetState(value:TState);
begin
   FState:= value;
end;

procedure TCedit.SetHintText(value:string);
begin
    {while set HintText,make Text and Hint have the same value}
    FHintText:=value;
    Text:=FHintText;
    Hint:=Text;
end;


// Procedure for working with KeyPress event
procedure TCedit.KeyPress(var key:char);

begin
  if Text=FHintText then
      begin
         Text:='';
      end;
  Color:=clWhite;
  Font.Color:=clBlack;
  if key=#13 then
     begin
        key:=#0;
        postmessage(self.Handle,WM_KEYDOWN,VK_TAB,0);
     end;
  {if select number format,you can only input digital symbal and some funtional key,'-','.'is allowed}
  if State=StNumber then
     begin
        if (not checkformat(text))or IsPasteText(text) then
          begin
          ShowMessage('输入格式错误,请重新输入');
          text:='';
          self.SetFocus;
          key:=#0;
          end
       else begin
          case key of
          '0'.. '9': if (Length(text)<>0) and (length(text)>pos('-',text))  then
          begin
          if ((StrToFloat(Text)-FMaxInput)>0 ) or ((StrToFloat(Text)-FMinInput)<0)  then
          begin
          ShowMessage('有效输入范围'+ FloatToStr(FMinInput )+'-'+ FloatToStr(FMaxInput)+',请重新输入');
          key:=#0;
          self.SetFocus;
          end;
          end;
          '-': if Pos('-',text)>0 then key:=#0;
          #8:;
          else
          if not ((key = DecimalSeparator) and (Pos(DecimalSeparator,text)=0)) then
          key:= #0;
          end;
          end;
     end;
   {one time input digital over}
   inherited KeyPress(key);

end;
procedure TCedit.doEnter;
begin
   Color:=clWhite;
   Font.Color:=clBlack;
   self.SelectAll;
   inherited doEnter;
end;

procedure TCedit.doExit;
begin
   if Text=FHintText then
      begin
         Text:='';
      end;
   if State=StNumber then
      begin
        if text='' then
          text:=FloatToStr(FMinInput );
        if (not checkformat(text))or IsPasteText(text) then
          begin
          ShowMessage('输入格式错误,请重新输入');
          text:='';
          self.SetFocus;
          end
        else begin
          if ((StrToFloat(Text)-FMaxInput)>0 ) or ((StrToFloat(Text)-FMinInput)<0)  then
          begin
          ShowMessage('有效输入范围'+ FloatToStr(FMinInput )+'-'+ FloatToStr(FMaxInput)+',请重新输入');
          self.SetFocus;
          end;
        end;
      end;
   Color:=clSilver;
   Font.Color:=clBlack;
   inherited doExit;
end;

procedure TCedit.Click;
begin
    if Text=FHintText then
       begin
          Text:='';
       end;
    inherited Click;
end;

end.
刚学Delphi 一个月:)
520god 19904 2005/12/12 0:42:17
谢谢楼主!正好用上了。
并结合 bios (阿贡) 的QMyedit改了下,使之能调整数字对齐方式。
为尊重楼主与BIOS (阿贡) 并向他们表示感谢,将改过的代码发上来给需要的人。:)
unit mynEdit;

interface

uses
  Windows, Messages, Graphics, Forms, Dialogs,
  SysUtils, Classes, Controls, StdCtrls;

type
  TnkEdit = class(TEdit)
  private
    { Private declarations }
    FAlignment:  TAlignment;
    FNumb: single;
    Fmydec:Integer;
    function GetNumb: single;
    procedure SetNumb(value:single);
    function Getmydec:integer;
    procedure Setmydec(value:integer);
  protected
    { Protected declarations }
    procedure KeyPress(var Key: Char); override;
    procedure doExit;override;
  public
    { Public declarations }
  published
    { Published declarations }
     procedure  CreateParams(var  Params:  TCreateParams);override;
     procedure  SetAlignment(Value:  TAlignment);
     property  Alignment:  TAlignment  read  FAlignment  write  SetAlignment  default  taLeftJustify;

    constructor Create(AOwner:TComponent); override;
    property Numb : single
          read GetNumb
          write SetNumb;
    property Mydec:integer read Getmydec write Setmydec;
  end;

procedure Register;

implementation

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

// Component constructor
constructor TnkEdit.Create(AOwner:TComponent);
begin
     // Don't forget to call the ancestors' concstructor
     inherited Create(AOwner);
     Fmydec:=2;
end;

Function Tnkedit.Getmydec:integer;
begin
result:=Fmydec;
end;

function TnkEdit.GetNumb:single;
begin
     try 
        Result:=StrToFloat(text);
        except
          on EConvertError do
          begin
          Result:=0;
          text:='';
          end;
     end;
end;

procedure Tnkedit.SetmyDEC(Value:integer);
begin
if value >0 then
Fmydec:=Value
else
Fmydec:=2;
end;

// Procedure to recording into FNumb
procedure TnkEdit.SetNumb(value:single);
begin
     FNumb:=Value;
     Text:=FloatToStr(value);
end;

// Procedure for working with KeyPress event
procedure TnkEdit.KeyPress(var key:char);
begin
    case key of
          '0'.. '9': if (text<>'0.00')and(Pos('.',text)<>0)and(Fmydec=Length(text)-Pos(DecimalSeparator,text)) then key:=#0 ;
          '-': if Length(text)<>0 then key:=#0;
          #8, #13:;
          else
          if not ((key = DecimalSeparator) and (Pos(DecimalSeparator,text)=0))
          then key:= #0;
    end;
    inherited KeyPress(key);
end;

procedure Tnkedit.doExit;
var i:integer;
begin
   //输入时没有输小数部分
   if pos('.',text)=0 then
    begin
      text:=text+'.';
    end;
    // 输入时末尾是.
    if pos('.',text)=length(text) then
    begin
       for i:=1 to Fmydec do
         begin
         text:=text+'0';
         end;
    end;
    //输入小数为数不够
    if Fmydec>(length(text)-pos('.',text)) then
    begin
       for i:=length(text)-pos('.',text) to Fmydec do
        begin
          text:=text+'0';
        end;
    end;
       inherited doExit;
end;

procedure  Tnkedit.SetAlignment(Value:  TAlignment);
begin
 if  FAlignment  <>  Value  then
 begin
 FAlignment  :=  Value;
 RecreateWnd;
 end;
end;

procedure  Tnkedit.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  Alignments[UseRightToLeftAlignment,  FAlignment]
 end;
end;
end.
bgh123 15925 2005/6/25 11:27:36
怎么安装这个控件,请详细说明一下好吗,我不懂
tang1 11677 2005/1/13 9:02:05
支持!至少是奉献精神值得学习。不过,建议参考Raize的相同控件进行一下修改。现在还很难用(在反复使用数字输入时)。
nbadjack 11576 2005/1/10 21:46:05
代码很简单,主要是这样控件比较实用。我贴代码,主要是为了刚入门做控件的朋友学习用的,没有什么其他用意!
zzgswh 11574 2005/1/10 19:36:25
不错,很好,学习。
hljmdjlxd 11561 2005/1/10 15:39:10
非常感谢!!
tim001 11524 2005/1/10 9:35:07
好,学习!!
jackalan 11498 2005/1/9 19:56:23
保存为.PAS,安装一下就好,安装后就会有个TnkEdit控件了。呵呵
cnhotel 11497 2005/1/9 19:33:44
呵呵,新手,感兴趣,但不知道这个代码保存后怎么使用,可否介绍一下~谢谢
第一页 上一页 下一页 最后页 有 10 条纪录 共1页 1 - 10
 用户名:
 密 码:
自动登陆(30天有效)
 
  DELPHI盒子版权所有 技术支持:深圳市麟瑞科技有限公司 1999-2024 V4.01 粤ICP备10103342号-1 更新RSS列表