Recent

Author Topic: Cant Make Dynamic Buttons Click Work Please Help  (Read 5469 times)

itsarmin

  • Newbie
  • Posts: 3
Cant Make Dynamic Buttons Click Work Please Help
« on: September 23, 2015, 11:38:06 pm »
Hi. im so new, I'm Trying to create dynamic buttons(they read their name from stringlist:Textfile ) and i got it work, but ONCLICK event i cant make it work to read from another line in that text file and apply it to a Memo.caption .

procedure Tfrm_addnew.btn_NewOkClick(Sender: TObject);
var
  TheNumber:integer;
  i:integer;
  Str:Tstringlist;
  Btns:Array [1..100] of TButton;
begin
//Get The Number------
  Str:=TStringList.Create;
  Str.LoadFromFile('infor.txt');
  TheNumber:=(str.Count-2)div 2;
//Creat Buttons-------
for i:=1 to TheNumber do
  begin
    btns:=TButton.Create(Self);
    with Btns do
      begin
        Parent:=frm_Databox.ScrBox;
        Height:=20;
        Width:=99;
        Top:=20*i-20;
        Caption:=str.ValueFromIndex[i*2];
        OnClick:=Thebox.frm_Databox.Note.Caption:=str.ValueFromIndex[i*2];
       end;
  end;
  Str.free;
close;
end;   

derek.john.evans

  • Guest
Re: Cant Make Dynamic Buttons Click Work Please Help
« Reply #1 on: September 23, 2015, 11:53:33 pm »
OnClick must be set to the address of a TNotifyEvent compatible method:

ie: TNotifyEvent is defined as:
Code: Pascal  [Select][+][-]
  1. TNotifyEvent = procedure(Sender: TObject) of object;  
  2.  

Therefore your method must be: (Note: The method name can be changed)
Code: Pascal  [Select][+][-]
  1. procedure Tfrm_addnew.ButtonClick(Sender: TObject);
  2. begin
  3. end;
  4.  

Then link the OnClick event to the TButton using:
Code: Pascal  [Select][+][-]
  1. btns.OnClick := @ButtonClick;
  2. // Or, for Delphi mode, use: (Note: In Delphi mode the @ isn't required)
  3. btns.OnClick := ButtonClick;
  4.  

http://wiki.freepascal.org/TButton

There are a lot of different event types, so its common practice to use the popupmenu tool"Find declaration of ???? ...", and then copy/paste the event prototype into your class.

** EDIT **

TButtons have a Tag property which you can use to store the Index (Which must me set in your for loop).

You can get the button that was clicked using (Sender as TButton), and therefore you can get the Index of the button using (Sender as TButton).Tag.
« Last Edit: October 02, 2015, 03:03:55 am by Geepster »

itsarmin

  • Newbie
  • Posts: 3
Re: Cant Make Dynamic Buttons Click Work Please Help
« Reply #2 on: September 24, 2015, 01:20:57 am »
Thank you so much, tag and @ was really helpful now The only problem is that , i don't know what is the name of the buttons that are being created dynamically it
gives me error , please help me with this
I'm Uploading the project, I know its to much to ask, but maybe you or someone can take a look at it.
Thank you so much

derek.john.evans

  • Guest
Re: Cant Make Dynamic Buttons Click Work Please Help
« Reply #3 on: September 24, 2015, 01:37:37 am »
Line: 45 // should be:
Code: [Select]
procedure Tfrm_addnew.BtnsClick(Sender: TObject);   

Line: 52 // should be:
Code: [Select]
a:=(Sender as TButton).tag*2+1;

itsarmin

  • Newbie
  • Posts: 3
Re: Cant Make Dynamic Buttons Click Work Please Help
« Reply #4 on: September 24, 2015, 04:30:06 pm »
Awesome , Thank you so much for helping me . all working good . :)

 

TinyPortal © 2005-2018