PDA

View Full Version : ساختن آرایه ای از دکمه ها برای بازی دوز 5*5



InfiniteLoop
یک شنبه 14 خرداد 1391, 20:17 عصر
درود
اساتید من می خوام یه آرایه 2 بعدی 5*5 از دکمه ها بسازم ( که ساختم هیچی)
مشکل اینجاست که من create این عناصر رو تو formload مینویسم در نتیجه نمیتونم به رویداد های ای عزیزان دسترسی داشته باشم.
مثلا رویداد buttonclick عنصر [1,3] رو بیام توش یه سری دستور بنویسم,
سوال اینجاست که چه طور؟
میشه یه کاری کرد که قبل formload و تو محیط edit ایجادشون کرد؟

در ضمن کلی گشتم چیز مالی پیدا نکردم

Felony
یک شنبه 14 خرداد 1391, 21:05 عصر
میتونه کمکت کنه » http://barnamenevis.org/showthread.php?288863-%D8%A8%D8%B1%D8%B1%D8%B3%DB%8C-%D8%A8%D8%A7%D8%B2%DB%8C-Minesweeper-%D8%A7%D8%B2-%D9%84%D8%AD%D8%A7%D8%B8-%D8%B4%D8%A6-%DA%AF%D8%B1%D8%A7%D8%A6%DB%8C

Ananas
یک شنبه 14 خرداد 1391, 21:13 عصر
سلام.

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
Btns : array[1..5] of array[1..5] of TButton;
function GetBtnIndex(b : TButton):TPoint;
procedure OnBtnClick(Sender : TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.GetBtnIndex(b : TButton):TPoint;
var
i, j: Integer;
begin
for i := Low(Btns) to High(Btns) do
for j := Low(Btns[i]) to High(Btns[i]) do
if (b = Btns[i][j]) then
begin
Result := Point(i, j);
Exit;
end;
end;

procedure TForm1.OnBtnClick(Sender : TObject);
var
p : TPoint;
begin
p := GetBtnIndex(Sender as TButton);
ShowMessage(
'with Index : Buttons[' + IntToStr(p.X) + '][' + IntToStr(p.Y) + ']'#13#10 +
'with Sender : ' + (Sender as TButton).Caption);
case p.X of
1 : case p.Y of
1 : ;
2 : ;
3 : ;
4 : ;
5 : ;
end;
2 : case p.Y of
1 : ;
2 : ;
3 : ;
4 : ;
5 : ;
end;
3 : case p.Y of
1 : ;
2 : ;
3 : ;
4 : ;
5 : ;
end;
4 : case p.Y of
1 : ;
2 : ;
3 : ;
4 : ;
5 : ;
end;
5 : case p.Y of
1 : ;
2 : ;
3 : ;
4 : ;
5 : ;
end;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
i : Integer;
j: Integer;
begin
for i := Low(Btns) to High(Btns) do
for j := Low(Btns[i]) to High(Btns[i]) do
begin
Btns[i][j] := TButton.Create(Self);
Btns[i][j].Parent := Self;
Btns[i][j].Caption := 'Button[' + IntToStr(i) + '][' + IntToStr(j) + ']';
Btns[i][j].OnClick := OnBtnClick;
Btns[i][j].Left := (j - Low(Btns[i])) * (Btns[i][j].Width + 10) + 10;
Btns[i][j].Top := (i - Low(Btns)) * (Btns[i][j].Height + 10) + 10;
end;
end;

end.


یک فرم خالی بساز و این کد رو توش کپی کن.