我的問題是關於德爾福7 我需要獲得當前選擇ComboBox1價值,用它作爲我的代碼浮點變量:獲取當前選定的組合框的值,並把它作爲變量
t:=t+ComboBox1. // Not sure what to write here...
謝謝!
我的問題是關於德爾福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;
// 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;
或者更好[TryStrToFloat(http://docwiki.embarcadero.com/VCL/en/SysUtils.TryStrToFloat),你可以用'ComboBox1.Text',而不是'ComboBox1.Items [ComboBox1.ItemIndex] )' – TLama
代碼中有一個額外的「)」。刪除它後,它工作得很好。謝謝! – enflam3
我個人不喜歡這段代碼,因爲它使用了兩個對ComboBox1.ItemIndex屬性的引用。我不確定這個屬性的讀取方法是什麼,也許它只是讀取一個字段,所以沒有執行懲罰,但它只是感覺不對。 – dummzeuch
+1,TryStrToFloat存在於D7 – Simon
工作,正是我所需要的! – enflam3
雖然我總是使用同一單元(SysUtils)中的「StrToFloatDef()」,但不知道「TryStrToFloat()」。 +1 – talereader