获取当前选定的Combobox值并将其用作变量

enf*_*am3 9 delphi combobox tcombobox

我的问题是关于Delphi 7.我需要获取当前选择的ComboBox1值,以便在我的代码中将其用作浮点变量:

t:=t+ComboBox1. // Not sure what to write here...
Run Code Online (Sandbox Code Playgroud)

谢谢!

TLa*_*ama 10

不确定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;
Run Code Online (Sandbox Code Playgroud)


spl*_*ash 5

// 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;
Run Code Online (Sandbox Code Playgroud)

  • @TLama:访问ComboBox1.Text与ComboBox1.Items [ComboBox1.ItemIndex]不相同。文本可能不在“项目”列表中。 (2认同)