【发布时间】:2011-11-19 12:17:02
【问题描述】:
我的问题是关于 Delphi 7。
我需要获取当前选定的 ComboBox1 值,以便在我的代码中将其用作浮点变量:
t:=t+ComboBox1. // Not sure what to write here...
谢谢!
【问题讨论】:
我的问题是关于 Delphi 7。
我需要获取当前选定的 ComboBox1 值,以便在我的代码中将其用作浮点变量:
t:=t+ComboBox1. // Not sure what to write here...
谢谢!
【问题讨论】:
不确定TryStrToFloat 是否已经在 Delphi 7 中,但如果是,我会这样做。
procedure TForm1.ComboBox1Change(Sender: TObject);
var
Value: Double;
begin
if TryStrToFloat(ComboBox1.Text, Value) then
T := T + Value
else
ShowMessage('You''ve entered wrong value ...');
end;
【讨论】:
TryStrToFloat(),虽然我一直使用StrToFloatDef(),它在同一个单元(SysUtils)中。 +1
TryStrToFloat 和 StrToFloatDef 这两个函数在内部调用 TextToFloat 函数。唯一的区别是他们如何使用它;如果你写例如var Output: Extended; begin if not TryStrToFloat('0.xx', Output) then Output := 0.01; end; 那么你将得到与调用Output := StrToFloatDef('0.xx', 0.01); 相同的结果;)
// ItemIndex is the index of the selected item
// If no item is selected, the value of ItemIndex is -1
if (ComboBox1.ItemIndex >= 0) then
begin
t := t + StrToFloat(ComboBox1.Items[ComboBox1.ItemIndex]);
end;
【讨论】:
ComboBox1.Text而不是ComboBox1.Items[ComboBox1.ItemIndex])
在 Delphi 10.2 Tokyo 我只是这样做:
[字符串] := ComboBox.Selected.Text
【讨论】: