#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0

    Delphi runtime created button onclick with sender


    Hello, I have created a new panel by:
    Code:
    NewPnl := TPanel.Create(self);
        With NewPnl do
        Begin
          name := 'Pnl' + IntToStr(NoOfPnls);
          Left := 5;
          Top := 5;
          Width := 40;
          Height := 10;
          Caption := 'New Panel';
          Parent := form1;  
          Color := clRed;
          onClick := NewPnlClick(NoOfPnls);
        end;
    NoOfPnls := NoOfPnls +1;
    Which creates a new panel each time I want to, called Pnl1, Pnl2, Pnl3 etc...
    What I want to do is have a different onClick event for each panel, since each panel is for something different. I can easily create an onClick event for the runtime created panels by:
    Code:
    onClick := NewPnlClick
    and creating a procedure called NewPnlClick. But in order to have a different result for each panel I want to send the 'NoOfPnls'. The 'NoOfPnls' is a global integer that keeps track of how many panels have been created.
    However when I use the onClick with a sender:
    Code:
    onClick := NewPnlClick(NoOfPnls);
    I get an error saying:
    "[DCC Error] Form1.pas(117): E2010 Incompatible types: 'TNotifyEvent' and 'procedure, untyped pointer or untyped parameter'"

    Can anyone help with this?

    Thanks in advance,

    ~Perran
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    South Africa
    Posts
    59
    Rep Power
    11
    Hi,

    die line

    Onclick := NewPnlClick cannot contain the parameters. Here you are just assigning the newly created procedure NewPnlClick. basically saying the OnClick handler now points to NewPnlClick.

    Once the user clicks, the event will execute the handler named NewPnlClick. These procedures must be of the form

    procedure NewPnlClick(Sender: TObject). Once this procedure is called (on the click action), the Sender will refer to the button that made the call to this procedure. I suggest you use the TAG property of the related button and change its value (integer) to the number of panels that the specific button should create. then, in the NewPnlClick event handler you do something like

    Code:
    procedure TForm1.NewPnlClick (Sender: TObject);
    begin
      If Not Assigned(Sender) Then Exit;
      If Not (Sender is TButton) Then Exit;
    
      For i := 1 to TButton(Sender).Tag Do
      Begin 
        //Create your panel here 
      End
    end;

    Comments on this post

    • Perran agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    12
    Rep Power
    0
    Thanks dve83 that really helped.

    ~ Perran

IMN logo majestic logo threadwatch logo seochat tools logo