2016-07-26 44 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)等准备。 –

相关问题