2016-07-26 34 views
1

商品の情報(ブランド名、内容、量、在庫量など)を入力して表示するプログラムを作成する必要があります。入力ボックスが順番に表示されないのはなぜですか?

問題:このコードの入力ボックスは、コードを順番に表示するように記述されていても、完全には表示されません。入力ボックスは、「目次」、「質量」、「在庫」、「ブランド名」の順であり、実際には「ブランド名」、「内容」、「質量」、「在庫」です。入力ボックスのキャプション)。

-Iは、(プログラムの残りの部分のために下にスクロールして)これはソースかもしれないと思う:

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

ダイアログは、ユーザがダイアログの閉じるボタン(赤いX)を押すなどの準備が必要です。 –

関連する問題