gpt4 book ai didi

delphi - 为什么我的输入框没有按顺序显示?

转载 作者:行者123 更新时间:2023-12-03 19:13:43 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





In Delphi, are parameters evaluated in order when passed into a method?

(2 个回答)


5年前关闭。




我需要制作一个程序来输入和显示产品的信息(品牌名称、内容、质量以及库存量)。

问题:这段代码中的输入框并没有完全按顺序显示,即使代码是按顺序显示的。输入框的顺序是:“Contents”、“Mass”、“Stock”、“Brand name”,而实际上应该是“Brand name”、“Contents”、“Mass”、“Stock”。(这些是输入框的标题)。

-我认为这可能是来源(向下滚动查看程序的其余部分):

 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.

最佳答案

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;

关于delphi - 为什么我的输入框没有按顺序显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38594440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com