【问题标题】:use progressbar do display text limit of a memo使用进度条显示备忘录的文本限制
【发布时间】:2016-12-17 11:13:10
【问题描述】:

我的 cxmemo 限制为 150 个字符(最大长度)。 我想使用 cxProgressBar 来直观地表示这个限制。 我怎样才能做到这一点 ? 我试过了:

procedure TMain_Form.cxMemo1PropertiesChange(Sender: TObject);
begin
if not  (trim(cxmemo1.lines.Text) = '')  then begin
cxProgressBar1.Position := cxProgressBar1.Position +cxmemo1.Properties.MaxLength - Length(cxmemo1.Text);
end;
end;

但它不起作用...... 我希望在用户键入时进度条上升,如果他正在删除备忘录中的字符,我希望进度条缩小。 我怎样才能使这项工作?

【问题讨论】:

  • progressbar.max 设置为 maxlength 并使用 Length(cxmemo1.Text); 更新位置
  • 为什么进度条永远不会回到 0% ?即使备忘录是空的,它也保持在 1%。 - 非常好的答案,谢谢。
  • 进度条的最小值是多少?当备忘录为空时,您是否还有不更新进度条的检查?
  • Min = 0 不,我没有做检查...
  • 请随时发布您自己的答案,在 SO 上鼓励这样做。过两天左右就可以接受了。

标签: delphi


【解决方案1】:
procedure TMain_Form.cxMemo1PropertiesChange(Sender: TObject);
begin
  if Trim(cxMemo1.Lines.Text) <> '' then begin
    cxProgressBar1.Visible := True;
    cxProgressBar1.Position := Length(cxMemo1.Text);
    if Length(cxMemo1.Text) = cxMemo1.Properties.MaxLength then
      ShowMessage('You exceeded the maximum number of characters !' + #13#10 +'Limit is : 150 '+ #13#10 +'For more, use the field "memo" .');
  end else begin
    if cxMemo1.Lines.Text.IsEmpty then
      cxProgressBar1.Position := 0;
    cxProgressBar1.Visible := False;
  end;
end;

【讨论】:

  • 正如建议,尝试改进缩进!代码难读
  • 每次读取Text,都会分配一个新的string。此代码分配 4 个字符串,其中只需要 1 个。将修剪后的 Text 缓存到一个局部变量中,并根据需要在任何地方使用它。
猜你喜欢
  • 2012-01-31
  • 1970-01-01
  • 2021-08-12
  • 2021-10-16
  • 1970-01-01
  • 2023-01-17
  • 2013-08-26
  • 1970-01-01
  • 2022-11-03
相关资源
最近更新 更多