Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 获取当前选定的组合框值并将其用作变量_Delphi_Combobox_Tcombobox - Fatal编程技术网

Delphi 获取当前选定的组合框值并将其用作变量

Delphi 获取当前选定的组合框值并将其用作变量,delphi,combobox,tcombobox,Delphi,Combobox,Tcombobox,我的问题是关于Delphi7的 我需要获取当前选中的ComboBox1值,才能将其用作代码中的浮点变量: t:=t+ComboBox1. // Not sure what to write here... 谢谢大家! 不确定是否已经在Delphi 7中,但如果是,我会这样做 // ItemIndex is the index of the selected item // If no item is selected, the value of ItemIndex is -1 if (Comb

我的问题是关于Delphi7的

我需要获取当前选中的
ComboBox1
值,才能将其用作代码中的浮点变量:

t:=t+ComboBox1. // Not sure what to write here...
谢谢大家!

不确定是否已经在Delphi 7中,但如果是,我会这样做

// 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;
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;

在Delphi 10.2 Tokyo中,我只需执行以下操作:


[string]:=ComboBox.Selected.Text

或更好,您可以使用
ComboBox1.Text
而不是
ComboBox1.Items[ComboBox1.ItemIndex])
代码中还有一个“)”。拆下它后,效果很好。非常感谢。我个人不喜欢这段代码,因为它使用了对ComboBox1.ItemIndex属性的两个引用。我不确定这个属性的read方法是做什么的,也许它只是读取一个字段,所以没有性能损失,但它感觉不对。@TLama:访问ComboBox1.Text与ComboBox1.Items[ComboBox1.ItemIndex]不同。这是有可能的文字不在项目列表。工作,正是我需要的!嗯,虽然我总是使用同一个单元(SysUtils)中的
StrToFloatDef()
,但我不知道TryStrToFloat()+1@talereader谢谢顺便说一句,这两个函数都有,并在内部调用该函数。唯一的区别是他们如何使用它;如果您编写例如
var输出:扩展;如果不是TryStrToFloat('0.xx',输出),则开始输出:=0.01;结束然后您将得到与调用
输出:=StrToFloatDef('0.xx',0.01)相同的结果;)这个答案对已经回答的问题没有帮助。