gpt4 book ai didi

delphi - 带有平面按钮且没有计算器的数值编辑控件

转载 作者:行者123 更新时间:2023-12-03 19:32:08 25 4
gpt4 key购买 nike

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

我需要一个编辑控件,在该控件中,用户只能输入浮点数(小数点后2位),并且可以恢复编辑控件本身内部的按钮(执行图标)的原始值(取自变量,此处不重要)。

这是我使用JEDI库中的TJvCalcEdit完成的。



控件定义:

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)不会显示小数点,仅在关闭计算器后才会显示。



因此,我可以禁用此行为吗?或者有人可以建议我使用其他控件的解决方案(标准,开源还是免费)?

最佳答案

我将使用TButtonedEdit来获取按钮,并在该点之后强制使用最多两位小数的浮点输入,

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;




Sample demo EXE

关于delphi - 带有平面按钮且没有计算器的数值编辑控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15065018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com