Тема: Sender
Показать сообщение отдельно
  #3  
Старый 02.02.2013, 04:32
Аватар для angvelem
angvelem angvelem вне форума
.
 
Регистрация: 18.05.2011
Адрес: Омск
Сообщения: 3,970
Версия Delphi: 3,5,7,10,12,XE2
Репутация: выкл
По умолчанию

Всё передаётся, забыл свойство 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;
__________________
Je venus de nulle part
55.026263 с.ш., 73.397636 в.д.
Ответить с цитированием