2011-11-19 27 views

回答

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; 
+0

+1,TryStrToFloat存在於D7 – Simon

+1

工作,正是我所需要的! – enflam3

+1

雖然我總是使用同一單元(SysUtils)中的「StrToFloatDef()」,但不知道「TryStrToFloat()」。 +1 – talereader

4
// 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; 
+1

或者更好[TryStrToFloat(http://docwiki.embarcadero.com/VCL/en/SysUtils.TryStrToFloat),你可以用'ComboBox1.Text',而不是'ComboBox1.Items [ComboBox1.ItemIndex] )' – TLama

+0

代碼中有一個額外的「)」。刪除它後,它工作得很好。謝謝! – enflam3

+0

我個人不喜歡這段代碼,因爲它使用了兩個對ComboBox1.ItemIndex屬性的引用。我不確定這個屬性的讀取方法是什麼,也許它只是讀取一個字段,所以沒有執行懲罰,但它只是感覺不對。 – dummzeuch

相關問題