2013-07-13 56 views
0

我是Delphi的新手。Delphi7,创建许多具有相同属性的控件

我想创建一个应用程序,其中将创建一些按钮。声明一个Tbuttons数组并且创建按钮1并不是很令人满意,因为它令人困惑并且花费了很多时间。使用Command For也不令人满意,因为如果需要,我将无法更改某些按钮的属性,例如他们的位置。

所以我决定在TForm1类中声明一个过程,它根据我发送给过程的属性创建按钮。但是,由于某种原因,它不工作(有没有任何语法错误):

unit Unit1; 

interface 

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

type 
    TForm1 = class(TForm)      //Declaring the procedure 
    procedure CreateButton(Button: TButton; L: Integer; T: Integer); 
    procedure FormCreate(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 
    B1, B2: TForm1;   //Declaring controls 
implementation 

{$R *.dfm} 

procedure TForm1.CreateButton(Button: TButton; L: Integer; T: Integer); 
begin 
    Button:= TButton.Create(Self); 
    Button.Parent:= Self; 
    Button.Width:= 100; Button.Height:= 50; 
    Button.Left:= L; Button.Top:= T; 
end; 


procedure TForm1.FormCreate(Sender: TObject); 
Var 
    Button1, Button2: TButton; 
begin 
    B1.CreateButton(Button1, 100, 50);   //Sending properties 
    B2.CreateButton(Button2, 200, 40);   //Sending properties 
end; 

end. 
+0

你是什么意思“不工作”?什么时候 ?哪些错误?没有人可以跳进你的头,用你的眼睛看。请阅读http://www.catb.org/~esr/faqs/smart-questions.html –

+1

在CreateButton中有两个Button.Height ...将第二个更改为Button.Top:= T; – Bill

+0

我是sry,我的意思是没有创建按钮1,2 –

回答

0

AS:与主题启动通信中接听长得太。总的结果是一样http://pastebin.ca/2426760

procedure TForm1.CreateButton(VAR Button: TButton; CONST L: Integer; CONST T: Integer); 

也就是说Pascal语言的基础知识,如何将参数传递给过程/函数。

http://docwiki.embarcadero.com/RADStudio/XE4/en/Parameters_(Delphi)

其实,我不认为这是与参数
按钮=零,这意味着“Button1的”和“将Button2”的值的任何问题都不会发送,但是

http://pastebin.ca/2427238


Kudoes比尔为察觉THI秒。使用单独的属性来定位控件既效率低下,又容易出现复制粘贴错误。

使用第二个链接:

procedure TForm1.CreateButton(out Button: TButton; const L: Integer; const T: Integer); 
begin 
    Button:= TButton.Create(Self); 
    Button.Parent:= Self; 
    Button.SetBounds(L, T, 100, 50); 
end; 

其实你是做什么用的指针到新创建的按钮? 在你的代码中,你只需要松开它们!

procedure TForm1.FormCreate(Sender: TObject); 
Var 
    Button1, Button2: TButton; 
begin 
... 
end; 

在这个代码中,这些指针会丢失!如果你确实需要这些值 - 在程序之外传递它们。如果你不 - 不问他们 - http://en.wikipedia.org/wiki/YAGNIhttp://en.wikipedia.org/wiki/KISS_principle

Procedure TForm1.CreateButton(const L, T: Integer); 
begin 
    With TButton.Create(Self) do begin 
     Parent := Self; 
     SetBounds(L, T, 100, 50); 
     Caption := 'Caption at ' + IntToStr(T); 
     Name := 'Name at ' + IntToStr(L); 
    End; 
end; 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    B1.CreateButton(100, 50);   //Sending properties 
    B2.CreateButton(200, 40);   //Sending properties 
end; 

我们的B1,B2 ......

你声称要在表格上的2个按钮,但你的代码显示您尝试在第二张表格上制作三条表格和一个按钮,并在第三张表格上单击一个按钮。那么你真的想要什么?你是否检查过B1和B2表单是否是在尝试添加按钮时创建的?

也许你真的想

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    SELF.CreateButton(100, 50);   //Sending properties 
    SELF.CreateButton(200, 40);   //Sending properties 
end; 

然后一起去DRY原则,并保持所有的变量在一个地方。

http://docwiki.embarcadero.com/Libraries/XE2/en/System.Types.TPoint

Procedure TForm1.CreateButtons(const a: array of TPoint); 
Var p: TPoint; 
Begin 
    for p in a do 
     CreateButton(p.x, p.y); 
End; 

type TPointDynArray = array of TPoint; 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    CreateButtons(TPointDynArray.Create(
     Point(100,50), Point(200, 40))); 
end; 

荣誉给Delphi array initialization

之后,您可以随时添加更多的坐标数组,并保持一致。 好,打倒这个德尔福7个异能会 - 有些冗余 - 编码像

const BtnCnt = 2; 
     BtnLocations : array [1..BtnCnt] of TSize = (
     (cx: 100, cy: 50), (cx: 200, cy: 40) 
    ); 

Procedure TForm1.CreateButtons(const a: array of TSize); 
Var i: integer; 
Begin 
    for i := Low(a) to High(a) do 
     with a[i] do 
      CreateButton(cx, cy); 
End; 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    CreateButtons(BtnLocations); 
end; 

不过,虽然德尔福5和Delphi的7是伟大的版本,他们是非常不合时宜的。我绝对建议你要么upgradeing德尔福XE或更近,或侧迈向CodeTyphon


TForm1 = class(TForm)      //Declaring the procedure 
    procedure CreateButton(Button: TButton; L: Integer; T: Integer); 

声明,在窗体类的出版部分中的一个通用的过程也不是一个很好的风格。你最好在PRIVATE部分声明它们。坚持“最低可见度”将有助于使相互依赖性得到控制。否则一年之内你的计划就会变成意大利面条混乱,你不能在不破坏其他一切的情况下改变任何东西。我现在正在做一个拥有10多年历史的项目,我看到“一切都是公开的”后果非常明显。这是一个很大的痛苦!

+0

其实我不认为参数有问题。 –

+0

@NikolaidisDimitris那么你有意将第一个参数声明为'CreateButton'? “Button”参数从“FormCreate”转移到“CreateButton”以及后者如何使用它? –

+0

B1和B2是2个按钮。 OnFormCreate过程发送这2个按钮的参数。 B1 Button的Name = Button1,left = 100,top = 50.因此,CreateButton的变量“Button,L,T”取值为:Button:= Button,T:= 50 = Button.Top; L:= 100 = Button.Left –