1

我正在用 RAD Studio 编写一个 C++ 项目,但这也适用于 Delphi。

我需要一个编辑控件,其中用户只能输入浮点数(2 个小数位)并且可以通过单击编辑控件本身内的按钮(实际图标)来恢复原始值(取自变量,此处不重要)。

这就是我所做的,使用TJvCalcEdit来自 JEDI 库。

在此处输入图像描述

控制定义:

object Sconto1: TJvCalcEdit
  [non-important attributes...]
  ButtonFlat = True
  Glyph.Data = {
    D6020000424DD6020000000000003600000028000000100000000E0000000100
    180000000000A0020000130B0000130B00000000000000000000FFFFFFFFFFFF
    FFFFFFFFFFFFFFFFFF999EC29396C3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
    FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9198C48694CBA7BAFE8493CA72
    75B9FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8B96C5
    8695CBA7BAFEA7BAFEA7BAFEA7BAFE747EB66D71B5FFFFFFFFFFFFFFFFFFFFFF
    FFFFFFFFFFFFFFFFFFFF8493CAA7BAFEA7BAFEA7BAFEA7BAFEA7BAFEA7BAFE84
    93CA7E83CE6D71B4FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F94C3A7BAFE
    A7BAFEA7BAFEA7BAFEA7BAFEA7BAFE8492CA8288D27B7FCA6D71B4FFFFFFFFFF
    FFFFFFFFFFFFFFFFFFFFFFFFFF8492CAA7BAFEA7BAFEA7BAFE828ECA7B82C993
    96FA6D6FB67B7FCA7B7FCA6D6FB4FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9299C2
    A5B7FE7E88CA787DC99396FA9396FA9396FA9396FA6D6FB67B7FCA7B7FCA6D6F
    B4FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7678C89396FA9396FA9396FA9396FA93
    96FA9396FA9396FA6D6FB67B7FCA7B7FCA6C6FB3FFFFFFFFFFFFFFFFFFFFFFFF
    FFFFFF7678C89396FA9396FA9396FA9396FA9396FA9396FA9396FA6D6FB67B7F
    CA7B7FCA7576B0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7678C89396FA9396FA93
    96FA9396FA9396FA9396FA9396FA6D6FB67B7FCA6266A2D6D0E2FFFFFFFFFFFF
    FFFFFFFFFFFFFFFFFF7678C89396FA9396FA9396FA9396FA9396FA9396FA9396
    FA6D6FB67B7FCA7C7EB0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7678C893
    96FA9396FA9396FA9396FA9396FA9396FA9396FA7679C66B6DACFFFFFFFFFFFF
    FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7678C89396FA9396FA9093F58B8EEC7678
    C87C7FC6ACABE5FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
    FFFF7678C88283C5A4A4E7C4C1EBFFFFFFFFFFFFFFFFFFFFFFFF}
  ImageKind = ikCustom
  DecimalPlacesAlwaysShown = False
  OnButtonClick = EScontoButtonClick
end

单击按钮时调用的方法:

void __fastcall TFRigOrd::EScontoButtonClick(TObject *Sender)
{
    TJvCalcEdit* edit = dynamic_cast<TJvCalcEdit*>(Sender);
    edit->Value = oldSconto1;
}

问题:在此方法结束时,控件下方会出现一个计算器弹出窗口,需要用户执行操作。我不希望这种情况发生,因为我正在以编程方式更改值。我猜这是一个默认值,因为这样的按钮是用来触发计算器的。此外,您看到的值 (255) 没有小数点,只有在计算器关闭后才会显示。

在此处输入图像描述

那么,我可以禁用此行为吗?或者有人可以建议我使用另一种控件(标准、开源或免费)的解决方案吗?

4

2 回答 2

4

我会使用 aTButtonedEdit来获取按钮,并强制浮点输入在点后最多两位小数,我会这样做

TButtonedEdit = class(ExtCtrls.TButtonedEdit)
protected
  procedure KeyPress(var Key: Char); override;
  procedure WMPaste(var Message: TWMPaste); message WM_PASTE;
end;

...

procedure TButtonedEdit.KeyPress(var Key: Char);
  function InvalidInput: boolean;
  var
   dc: integer;
  begin
    result := false;
    if Character.IsControl(Key) then Exit;
    dc := Pos(DecimalSeparator, Text);
    if not (Key in ['0'..'9', DecimalSeparator]) then Exit(true);
    if Pos(DecimalSeparator, Text) > 0 then
    begin
      if Key = DecimalSeparator then Exit(true);
      if (Length(Text) - dc > 1)
        and (Pos(DecimalSeparator, Text) < SelStart + 1) and
        (SelLength = 0) then Exit(true);
    end;
  end;

begin
  inherited;
  if InvalidInput then
  begin
    Key := #0;
    beep;
  end;
end;

procedure TButtonedEdit.WMPaste(var Message: TWMPaste);
var
  s: string;
  i: integer;
  hasdc: boolean;
  NewText: string;
  NewSelStart: integer;
begin
  if Clipboard.HasFormat(CF_TEXT) then
  begin
    s := Clipboard.AsText;

    NewText := Text;
    Delete(NewText, SelStart + 1, SelLength);
    Insert(s, NewText, SelStart + 1);


    // Validate
    hasdc := false;
    for i := 1 to Length(NewText) do
    begin
      if NewText[i] = DecimalSeparator then
        if hasdc then
        begin
          beep;
          Exit;
        end
        else
          hasdc := true
      else if not (NewText[i] in ['0'..'9']) then
      begin
        beep;
        Exit;
      end;
    end;

    // Trim
    if hasdc then
      NewText := Copy(NewText, 1, Pos(DecimalSeparator, NewText) + 2);

    NewSelStart := SelStart + Length(s);
    Text := NewText;
    SelStart := NewSelStart;
    SelLength := 0;
  end
  else
    inherited;
end;

截屏

示例演示 EXE

于 2013-02-25T14:47:42.087 回答
0

使用股票 VCL 按钮编辑器

http://docwiki.embarcadero.com/Libraries/en/Vcl.ExtCtrls.TButtonedEdit

用于OnChange过滤掉错误的输入(或使用JvValidators


另一种基于 JediVCL 的方法是使用支持基本按钮的编辑器
http://wiki.delphi-jedi.org/wiki/JVCL_Help:TJvComboEdit

EditMask就像has 一样,它具有属性,TMaskEdit因此您可以对其进行调整以仅接受数字。

至少OnChange事件将允许你过滤非数字文本输入。

于 2013-02-25T14:06:58.313 回答