加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 以编程方式显示Balloon提示仅用于无效的文本编辑输入

发布时间:2020-12-15 04:14:02 所属栏目:大数据 来源:网络整理
导读:我正在关注这个例子: http://lawrencebarsanti.wordpress.com/2009/12/16/display-error-messages-with-tballoonhint/ 我正在尝试仅在编辑框中的当前值不可接受时显示气球提示. OnExit时会触发检查.仍应允许显示气球,直到确定该值为止.我还尝试在用户离开编
我正在关注这个例子:
http://lawrencebarsanti.wordpress.com/2009/12/16/display-error-messages-with-tballoonhint/

我正在尝试仅在编辑框中的当前值不可接受时显示气球提示. OnExit时会触发检查.仍应允许显示气球,直到确定该值为止.我还尝试在用户离开编辑时以编程方式显示气球以显示初始错误.

代码有效,但不是第一次.我必须使用无效值离开一次,更改为可接受的值,然后再次使用无效值.我想这是因为我在尝试显示气球之前无法启用或禁用ShowHint属性.

这是我的代码:

procedure TForm1.Edit1Exit(Sender: TObject);
var
  R: TRect;
  Bad : Boolean;
begin
  //Check if edit has only numbers
  if StrIsReal(Edit1.Text) then
  begin
    if(StrToFloat(Edit1.Text) >= 0.5) then
    begin
      //Value is ok
      SpeedButton1.Visible := false;
      Edit1.ShowHint := false;
      BalloonHint1.HideHint;
      Edit1.Text := FloatToStrF(StrToFloat(Edit1.Text),ffFixed,8,2);
    end
    else
    begin
      //Is decimal,but not at least 0.5
      Bad := true;
    end;
  end
  else
  begin
    Bad := true;
  end;

  if Bad then
  begin
    //Invalid number
    Edit1.ShowHint := true;

    Edit1.Text := '0.00';
    SpeedButton1.Visible := true;

    R := Edit1.BoundsRect;
    R.TopLeft := ClientToScreen(R.TopLeft);
    R.BottomRight := ClientToScreen(R.BottomRight);
    BalloonHint1.ShowHint(R);   //!!! Issue: No hint the first time around
  end;
end;

当我检查有效值(离开编辑)时,如何有条件地显示气球?

解决方法

实际上,奇怪的是你第二次得到任何东西,而不是你第一次得到任何东西.

您的代码适用于我(在XE4和XE6中)第一次(和第二次)进行此更改:

R := Edit1.BoundsRect;
  R.TopLeft := ClientToScreen(R.TopLeft);
  R.BottomRight := ClientToScreen(R.BottomRight);
  BalloonHint1.Description := 'bad input';   <---- this was missing
  BalloonHint1.ShowHint(R);   //!!! Issue: No hint the first time around

所以,如果第二次它确实适合你,我认为这是因为你没有展示的代码,同样你没有展示的东西可能就是为什么它不起作用的第一次.所以你可以“发现差异”,我的项目代码如下.我找不到你的StrIsReal函数,所以我自己动手了.

顺便说一句,我在我的表单中添加了第二个TEdit,因此还有其他东西可以失去焦点,并注释掉了你的两个Edit1.ShowHint作业,因为它们没有任何区别,但无论如何都不需要.

TForm1 = Class(TForm)
  [...]
    FBalloonHint : TBalloonHint;
    procedure WMActivateApp(var AMessage: TMessage); message WM_ActivateApp;
    procedure WMWindowPosChanged(var AMessage: TMessage);  message WM_WindowPosChanged;
  [...]
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FBalloonHint := TBalloonHint.Create(Self);
  FBalloonHint.HideAfter := 5000;
  FBalloonHint.Delay := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  R: TRect;
begin
  FBalloonHint.Description := 'You pressed ' + Button1.Caption;
  R := Button1.BoundsRect;
  R.TopLeft := ClientToScreen(R.TopLeft);
  R.BottomRight := ClientToScreen(R.BottomRight);
  FBalloonHint.ShowHint(R);
end;

procedure TForm1.Edit1Exit(Sender: TObject);
var
  R: TRect;
  Bad : Boolean;
  F : Double;

  function StrIsReal(S : String) : Boolean;
  begin
    Result := True;
  end;

begin
  //Check if edit has only numbers
  if StrIsReal(Edit1.Text) then
  begin
    if(StrToFloat(Edit1.Text) >= 0.5) then
    begin
      //Value is ok
      SpeedButton1.Visible := false;
      //Edit1.ShowHint := false;
      FBalloonHint.HideHint;
      Edit1.Text := FloatToStrF(StrToFloat(Edit1.Text),but not at least 0.5
      Bad := true;
    end;
  end
  else
  begin
    Bad := true;
  end;

  if Bad then
  begin
    //Invalid number
    //Edit1.ShowHint := true;

    Edit1.Text := '0.00';
    SpeedButton1.Visible := true;

    R := Edit1.BoundsRect;
    R.TopLeft := ClientToScreen(R.TopLeft);
    R.BottomRight := ClientToScreen(R.BottomRight);
    FBalloonHint.Description := 'bad input';
    FBalloonHint.ShowHint(R);   //!!! Issue: No hint the first time around
  end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  Edit1Exit(Sender);
end;

procedure TForm1.WMActivateApp(var AMessage: TMessage);
begin
  if Assigned(FBalloonHint) then FBalloonHint.HideHint;
  inherited;
end;

procedure TForm1.WMWindowPosChanged(var AMessage: TMessage);
begin
  if Assigned(FBalloonHint) then FBalloonHint.HideHint;
  inherited;
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读