2016-07-26 43 views
1

我需要製作一個程序,用於輸入和顯示產品的信息(品牌名稱,內容,質量和庫存量)。爲什麼我的輸入框不按順序顯示?

問題:即使編寫了代碼以便按順序顯示代碼,但這段代碼中的輸入框並沒有按順序完整顯示。輸入框的順序是:「內容」,「質量」,「庫存」,「品牌名稱」,實際上應該是「品牌名稱」,「內容」,「質量」,「庫存」(這些是輸入框的標題)。

- 我想,這可能是源(向下滾動計劃的其餘部分):

StrObj := TProducts.Create(Inputbox('Brand name', 'Input the brand name', ''),Inputbox('Contents', 'Input the contents', ''), StrToInt(Inputbox('Mass', 'Input the mass', '')), StrToInt(Inputbox('Stock', 'Input the number in stock', ''))); 

注: - 所有的數據都會在那裏打算去

,因此輸入框的順序根本不影響程序。我只想知道是否有辦法使輸入框按順序顯示。

- 這是一項任務,但輸入框的順序不會計算標記。

整個代碼:

應用:

unit TProducts_U; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls, ComCtrls, TProducts_class; 

type 
    TForm1 = class(TForm) 
    btnResult: TButton; 
    redOut: TRichEdit; 
    procedure btnResultClick(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

procedure TForm1.btnResultClick(Sender: TObject); 
var StrObj : TProducts; 
begin 
    StrObj := TProducts.Create(Inputbox('Brand name', 'Input the brand name', ''),Inputbox('Contents', 'Input the contents', ''), StrToInt(Inputbox('Mass', 'Input the mass', '')), StrToInt(Inputbox('Stock', 'Input the number in stock', ''))); 
    redOut.Lines.Add(StrObj.toString); 
end; 

end. 

我的類:

unit TProducts_class; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, Math; 

type 
    TProducts = class 
    private 
    fBname, fContents : string; 
    fMass, fNum : integer; 
    public 
    Constructor Create; overload; 
    Constructor Create(Bname, contents : string; mass, num : integer); overload; 
    Function toString : string; 
    end; 

implementation 

{ TProducts } 

constructor TProducts.Create; 
begin 
    fBname := ''; 
    fContents := ''; 
    fMass := 0; 
    fNum := 0; 
end; 

constructor TProducts.Create(Bname, contents: string; mass, num: integer); 
begin 
    fBname := Bname; 
    fContents := contents; 
    fMass := mass; 
    fNum := num; 
end; 

function TProducts.toString: string; 
begin 

    result := 'Brand Name is : ' + fBname + #13 + 
      'Contents is : ' + fContents + #13 + 
      'Mass is : ' + IntToStr(fMass) + #13 + 
      'We have ' + IntToStr(fNum) + ' in stock'; 

end; 

end. 

回答

5

參數計算順序沒有在Delphi中定義。

您需要爲每個InputBox()調用編寫單獨的指令,然後將它們連接在一起。

例如:

procedure TForm1.btnResultClick(Sender: TObject); 
var 
    StrObj: TProducts; 
    sBrandName, sContents, sMass, sStock: string; 
begin 
    sBrandName := Inputbox('Brand name', 'Input the brand name', ''); 
    sContents := Inputbox('Contents', 'Input the contents', ''); 
    sMass := StrToInt(Inputbox('Mass', 'Input the mass', '')); 
    sStock := StrToInt(Inputbox('Stock', 'Input the number in stock', '')) 
    StrObj := TProducts.Create(sBrandName, sContents, sMass, sStock); 
    // Added try .. finally, otherwise you will leak StrObj 
    try 
    redOut.Lines.Add(StrObj.toString); 
    finally 
    StrObj.Free; 
    end; 
end; 
+0

除了不被不正確的* *得多更具可讀性。 :-) –

+0

OP應該爲用戶按下對話關閉按鈕(紅色X)等準備。 –

相關問題