Recent

Author Topic: [SOLVED]Send an Element to function ?!?  (Read 9430 times)

majid.ebru

  • Hero Member
  • *****
  • Posts: 530
[SOLVED]Send an Element to function ?!?
« on: May 21, 2017, 11:14:17 am »
Hi
.
i read some post but i can not find answer!!
.
how can i send an element(like a button or an editbox) to a function ?!?!?
.
how can i send an element with pointer to function and use it in the function??
.
Thank you
« Last Edit: July 01, 2017, 12:35:10 pm by majid.ebru »

sky_khan

  • Guest
Re: Send an Element to function ?!?
« Reply #1 on: May 21, 2017, 11:28:10 am »
as any other argument

Code: Pascal  [Select][+][-]
  1. type
  2.   TLabelFunc = function (aLabel:TLabel):string of object;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   ShowTime(Label1);
  7. end;
  8.  
  9. procedure TForm1.Button2Click(Sender: TObject);
  10. begin
  11.   ShowTimeBy(@ShowTime);
  12. end;
  13.  
  14. procedure TForm1.ShowTimeBy(aLabelFunc: TLabelFunc);
  15. begin
  16.   aLabelFunc(Label1);
  17. end;
  18.  
  19. function TForm1.ShowTime(aLabel: TLabel): string;
  20. begin
  21.   Result:=FormatDateTime('MM DD YYYY hh:nn',Now);
  22.   aLabel.Caption:=Result;
  23. end;
  24.  
« Last Edit: May 21, 2017, 11:38:07 am by SkyKhan »

majid.ebru

  • Hero Member
  • *****
  • Posts: 530
Re: Send an Element to function ?!?
« Reply #2 on: May 21, 2017, 12:04:45 pm »
i confused ?!?!?!?!??!
 :'( :-\ :'( :-\ :'( :-\
.
can you say an eyse sample
.
just send an element to function and change it
.
(for example send a button to function(procedure) and change name button)
« Last Edit: May 21, 2017, 12:06:25 pm by majid.ebru »

Thaddy

  • Hero Member
  • *****
  • Posts: 19387
  • Glad to be alive.
Re: Send an Element to function ?!?
« Reply #3 on: May 21, 2017, 12:07:41 pm »
Well, then you have to explain better what you want, because SkyKhan's example is pretty obvious.
(Apart from missing two const modifiers in the method headers, but that does not affect functionality.)
objects are fine constructs. You can even initialize them with constructors.

Handoko

  • Hero Member
  • *****
  • Posts: 5557
  • My goal: build my own game engine using Lazarus
Re: Send an Element to function ?!?
« Reply #4 on: May 21, 2017, 12:34:00 pm »
Try my example, you can download the ready to run code.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     Button3: TButton;
  18.     Button4: TButton;
  19.     Memo1: TMemo;
  20.     procedure Button1Click(Sender: TObject);
  21.     procedure Button2Click(Sender: TObject);
  22.     procedure Button3Click(Sender: TObject);
  23.     procedure Button4Click(Sender: TObject);
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. procedure ChangeButton(Sender: TObject);
  34. var
  35.   NewCaption: string;
  36. begin
  37.   if not(Sender is TButton) then Exit;
  38.  
  39.   NewCaption := 'Btn' + IntToStr(Random(999));
  40.   Form1.Memo1.Text := 'You have clicked ' + TButton(Sender).Caption + '. ' +
  41.                       'And now it is changed to ' + NewCaption + '.';
  42.  
  43.   TButton(Sender).Caption := NewCaption;
  44. end;
  45.  
  46. { TForm1 }
  47.  
  48. procedure TForm1.Button1Click(Sender: TObject);
  49. begin
  50.   ChangeButton(Sender);
  51. end;
  52.  
  53. procedure TForm1.Button2Click(Sender: TObject);
  54. begin
  55.   ChangeButton(Sender);
  56. end;
  57.  
  58. procedure TForm1.Button3Click(Sender: TObject);
  59. begin
  60.   ChangeButton(Sender);
  61. end;
  62.  
  63. procedure TForm1.Button4Click(Sender: TObject);
  64. begin
  65.   ChangeButton(Sender);
  66. end;
  67.  
  68. end.

majid.ebru

  • Hero Member
  • *****
  • Posts: 530
Re: Send an Element to function ?!?
« Reply #5 on: May 21, 2017, 03:12:41 pm »
Thank you.
.
but i should send many elements to function.
.
in your sample , you just send one element to function.
.
i should send two button and one editbox and one stringgrid to function
.
.
[i have 4 panels and in each panel has two button and one editbox and one stringgrid. i don't want to write code for eath panel and i want to write one code for each panel]
.
.
thank you

sky_khan

  • Guest
Re: Send an Element to function ?!?
« Reply #6 on: May 21, 2017, 03:29:46 pm »
In Turkish we say "Armut piş ağzıma düş". I think it can be translated like "Let a pear is ripened and fall into my mouth".
I dont know any similar idiom in English. Do you ?

Handoko

  • Hero Member
  • *****
  • Posts: 5557
  • My goal: build my own game engine using Lazarus
Re: Send an Element to function ?!?
« Reply #7 on: May 21, 2017, 03:39:05 pm »
@SkyKhan

Calm down friend, he's just a newbie.

@majid.ebru

In my code I use TObject and pass Sender as the variable, you can change it to the things you want, like:

Code: Pascal  [Select][+][-]
  1. procedure ChangeButton(Button1, Button2: TButton; Edit: TEdit; Grid: TStringGrid);

You just have to make sure the unit that has that function, has StdCtrls and Grids in the unit clause.

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: Send an Element to function ?!?
« Reply #8 on: May 21, 2017, 03:45:49 pm »
another way you can change several controls at the same time:
Code: Pascal  [Select][+][-]
  1. procedure DisableControls(MyControls:Array of TControl);
  2. var
  3.   h:Integer;
  4. begin
  5.   for h:=Low(MyControls) to High(MyControls) do
  6.     TControl(MyControls[h]).Enabled:=false;
  7. end;
  8.        

And calling this procedure so:
Code: Pascal  [Select][+][-]
  1.  DisableControls([button2,button3,button4,Label1,label2,label3,StringGrid1,Edit1]);

Handoko

  • Hero Member
  • *****
  • Posts: 5557
  • My goal: build my own game engine using Lazarus
Re: Send an Element to function ?!?
« Reply #9 on: May 21, 2017, 03:52:20 pm »
+1 bylaardt

Using open array to pass parameter and accessing it using low and high.
It is a nice trick.
 ;D

Handoko

  • Hero Member
  • *****
  • Posts: 5557
  • My goal: build my own game engine using Lazarus
Re: Send an Element to function ?!?
« Reply #10 on: May 21, 2017, 03:53:59 pm »
(for example send a button to function(procedure) and change name button)

Maybe I'm wrong, but as far as I know, we cannot change button's name runtime.

majid.ebru

  • Hero Member
  • *****
  • Posts: 530
Re: Send an Element to function ?!?
« Reply #11 on: May 21, 2017, 04:27:34 pm »
Hi again
.
i think that i cann't explain my problem.
.
i have 2 button in each panel and each 2 button dose same code.
.
i want to use pointer and use pointer in function.
.
in the function caption of buttons and text of editbox must change?!?!
.
thank you for answer.
« Last Edit: May 22, 2017, 05:44:35 pm by majid.ebru »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Send an Element to function ?!?
« Reply #12 on: May 21, 2017, 05:01:45 pm »
Maybe I'm wrong, but as far as I know, we cannot change button's name runtime.

You can change a component's name at runtime. See attached example.

Handoko

  • Hero Member
  • *****
  • Posts: 5557
  • My goal: build my own game engine using Lazarus
Re: Send an Element to function ?!?
« Reply #13 on: May 21, 2017, 05:24:58 pm »
Cool. But I failed to open your example.
I'm using Lazarus 1.6.4 Ubuntu Mate 64-bit.

In what cases we need to change the component's name runtime? I don't think we will need it.

Handoko

  • Hero Member
  • *****
  • Posts: 5557
  • My goal: build my own game engine using Lazarus
Re: Send an Element to function ?!?
« Reply #14 on: May 21, 2017, 05:26:47 pm »
@majid.ebru

Not really sure what you want to do. You can download my example, perhaps that is what you want.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ExtCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Button3: TButton;
  19.     Button4: TButton;
  20.     Button5: TButton;
  21.     Button6: TButton;
  22.     Button7: TButton;
  23.     Button8: TButton;
  24.     Edit1: TEdit;
  25.     Edit2: TEdit;
  26.     Edit3: TEdit;
  27.     Edit4: TEdit;
  28.     Label1: TLabel;
  29.     Panel1: TPanel;
  30.     Panel2: TPanel;
  31.     Panel3: TPanel;
  32.     Panel4: TPanel;
  33.     procedure Panel1MouseEnter(Sender: TObject);
  34.     procedure Panel1MouseLeave(Sender: TObject);
  35.     procedure Panel2MouseEnter(Sender: TObject);
  36.     procedure Panel2MouseLeave(Sender: TObject);
  37.     procedure Panel3MouseEnter(Sender: TObject);
  38.     procedure Panel3MouseLeave(Sender: TObject);
  39.     procedure Panel4MouseEnter(Sender: TObject);
  40.     procedure Panel4MouseLeave(Sender: TObject);
  41.   end;
  42.  
  43. var
  44.   Form1: TForm1;
  45.  
  46. implementation
  47.  
  48. {$R *.lfm}
  49.  
  50. procedure DisableControls(Btn1, Btn2: TButton; Edit: TEdit);
  51. begin
  52.   Btn1.Caption := '-disabled-';
  53.   Btn2.Caption := '-disabled-';
  54.   Btn1.Enabled := False;
  55.   Btn2.Enabled := False;
  56.   Edit.Enabled := False;
  57. end;
  58.  
  59. procedure EnableControls(Btn1, Btn2: TButton; Edit: TEdit);
  60. begin
  61.   Btn1.Caption := 'New';
  62.   Btn2.Caption := 'Save';
  63.   Btn1.Enabled := True;
  64.   Btn2.Enabled := True;
  65.   Edit.Enabled := True;
  66. end;
  67.  
  68. { TForm1 }
  69.  
  70. procedure TForm1.Panel1MouseEnter(Sender: TObject);
  71. begin
  72.   EnableControls(Button1, Button2, Edit1);
  73. end;
  74.  
  75. procedure TForm1.Panel1MouseLeave(Sender: TObject);
  76. begin
  77.   DisableControls(Button1, Button2, Edit1);
  78. end;
  79.  
  80. procedure TForm1.Panel2MouseEnter(Sender: TObject);
  81. begin
  82.   EnableControls(Button3, Button4, Edit2);
  83. end;
  84.  
  85. procedure TForm1.Panel2MouseLeave(Sender: TObject);
  86. begin
  87.   DisableControls(Button3, Button4, Edit1);
  88. end;
  89.  
  90. procedure TForm1.Panel3MouseEnter(Sender: TObject);
  91. begin
  92.   EnableControls(Button5, Button6, Edit3);
  93. end;
  94.  
  95. procedure TForm1.Panel3MouseLeave(Sender: TObject);
  96. begin
  97.   DisableControls(Button5, Button6, Edit3);
  98. end;
  99.  
  100. procedure TForm1.Panel4MouseEnter(Sender: TObject);
  101. begin
  102.   EnableControls(Button7, Button8, Edit4);
  103. end;
  104.  
  105. procedure TForm1.Panel4MouseLeave(Sender: TObject);
  106. begin
  107.   DisableControls(Button7, Button8, Edit4);
  108. end;
  109.  
  110. end.
« Last Edit: May 21, 2017, 05:41:04 pm by Handoko »

 

TinyPortal © 2005-2018