![]() |
|
|
#1
|
||||
|
||||
|
Есть такой кусок кода в программе
Код:
type BitButtonClass = class
class procedure BitClick(Sender : TObject);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var Forma : TForm;
BitButton1 : TBitBtn;
BitButton2 : TBitBtn;
BitButton3 : TBitBtn;
A : TDBGrid;
begin
//
Forma := TForm.Create(Self);
With Forma do
begin
Left := 100;
Top := 100;
Height := 300; // Высота формы
Width := 300; // Ширина формы
BorderStyle := bsDialog;
end;
BitButton1 := TBitBtn.Create(Forma);
With BitButton1 do
begin
Parent := Forma;
Left := 20;
Top := 235;
Height := 25; // Высота
Width := 75; // Ширина
Caption :='Добавить';
Visible := true;
OnClick := BitButtonClass.BitClick;
end;
class procedure BitButtonClass.BitClick(Sender: TObject);
begin
if TBitBtn(Sender).Name = 'BitButton1' then
begin
ShowMessage('1111');
end;
if (Sender as TBitBtn).Name = 'BitButton2' then
begin
ShowMessage('2');
end;
if (Sender as TBitBtn).Name = 'BitButton3' then
begin
ShowMessage('3');
end;
ShowMessage('4');
end; |
|
#2
|
|||
|
|||
|
Убери class для процедуры и все будет передаваться.
|
|
#3
|
||||
|
||||
|
Всё передаётся, забыл свойство Name присвоить.
Код:
procedure TForm1.Button2Click(Sender: TObject);
var
MyForm : TForm;
Memo1 : TMemo;
BitButton1,
BitButton2,
BitButton3 : TButton;
begin
MyForm := TForm.Create(Self);
with MyForm do
begin
Left := 100;
Top := 100;
Height := 300; // Высота формы
Width := 300; // Ширина формы
BorderStyle := bsDialog;
Memo1 := TMemo.Create(MyForm);
with Memo1 do
begin
Parent := MyForm;
Left := 10;
Top := 10;
Height := 210; // Высота
Width := 270; // Ширина
Visible := True;
end;
BitButton1 := TButton.Create(MyForm);
with BitButton1 do
begin
Parent := MyForm;
Left := 20;
Top := 235;
Height := 25; // Высота
Width := 75; // Ширина
Caption := 'Добавить';
Name := 'BitButton1';
Visible := True;
OnClick := BitButtonClass.BitClick;
end;
BitButton2 := TButton.Create(MyForm);
with BitButton2 do
begin
Parent := MyForm;
Left := 100;
Top := 235;
Height := 25; // Высота
Width := 75; // Ширина
Caption := 'Удалить';
Name := 'BitButton2';
Visible := True;
OnClick := BitButtonClass.BitClick;
end;
BitButton3 := TButton.Create(MyForm);
with BitButton3 do
begin
Parent := MyForm;
Left := 180;
Top := 235;
Height := 25; // Высота
Width := 75; // Ширина
Caption := 'Отмена';
Name := 'BitButton3';
Visible := True;
OnClick := BitButtonClass.BitClick;
end;
end;
MyForm.ShowModal;
MyForm.Free;
end;
class procedure BitButtonClass.BitClick(Sender: TObject);
begin
if Sender is TButton then
begin
if (Sender as TButton).Name = 'BitButton1' then
ShowMessage('Кнопка 1')
else if (Sender as TButton).Name = 'BitButton2' then
ShowMessage('Кнопка 2')
else if (Sender as TButton).Name = 'BitButton3' then
ShowMessage('Кнопка 3');
end;
end;Можно также не просто показать какая кнопка, но и реальные действия "навесить". Код:
procedure TForm1.Button2Click(Sender: TObject);
...
Memo1 := TMemo.Create(MyForm);
with Memo1 do
begin
Parent := MyForm;
Left := 10;
Top := 10;
Height := 210; // Высота
Width := 270; // Ширина
Name := 'Memo1'; // добавим имя
Visible := True;
end;
...
end;
class procedure BitButtonClass.BitClick(Sender: TObject);
begin
if Sender is TButton then
begin
if (Sender as TButton).Name = 'BitButton1' then
TMemo(TForm((Sender as TButton).Parent).FindComponent('Memo1')).Lines.Add('Новая строка')
else if (Sender as TButton).Name = 'BitButton2' then
TMemo(TForm((Sender as TButton).Parent).FindComponent('Memo1')).Lines.Clear
else if (Sender as TButton).Name = 'BitButton3' then
TForm((Sender as TButton).Parent).Close;
end;
end;Последний раз редактировалось angvelem, 02.02.2013 в 05:11. |
| Этот пользователь сказал Спасибо angvelem за это полезное сообщение: | ||
Fantomas_RUS (02.02.2013)
| ||