Recent

Author Topic: Send a control name to a function or procedure  (Read 279 times)

andyf97

  • New Member
  • *
  • Posts: 19
Send a control name to a function or procedure
« on: March 29, 2023, 09:56:14 pm »

I want to pass the name of the component to a function or procedure that will eventually run a popup menu related to that component, having trouble sending the name of the control and I get an error.

I suppose that I should convert the TObject name to a string somehow.

unit1.pas(130,22) Error: Incompatible type for arg no. 1: Got "AnsiString", expected "TObject"


Code: Pascal  [Select][+][-]
  1.  
  2.  
  3. // this is just a little procedure to see the name passes, just for testing I want the name to go to the caption of form1
  4. procedure TForm1.SliderRightClick(sender: TObject);
  5. begin
  6.   Form1.caption := '';
  7. end;
  8.  
  9.  
  10. // this is the right click popup event of the control.
  11. procedure TForm1.ECSlider1ContextPopup(Sender: TObject; MousePos: TPoint;  
  12. var Handled: Boolean);
  13.  
  14. Var this:string;
  15.  
  16. begin
  17.  
  18.  this:=TComponent(sender).name;
  19. // This does contain the control name ECSlider1
  20.  
  21.   SliderRightClick(this);
  22. // How can I send the name stored in the variable this to the procedure TForm1.SliderRightClick
  23.  
  24. end;                    
  25.  
  26.  
  27.  
  28.  
  29.  

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1399
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Send a control name to a function or procedure
« Reply #1 on: March 29, 2023, 10:09:39 pm »
What is wrong by using sender as argument?
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

andyf97

  • New Member
  • *
  • Posts: 19
Re: Send a control name to a function or procedure
« Reply #2 on: March 29, 2023, 10:17:47 pm »

Not sure what you mean.

I have a load of these sliders and want to just copy a bit of duplicated code into each ones popup event and not need make them all specific when the names are already specific.



What is wrong by using sender as argument?




KodeZwerg

  • Hero Member
  • *****
  • Posts: 1399
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Send a control name to a function or procedure
« Reply #3 on: March 29, 2023, 10:19:45 pm »
Code: Pascal  [Select][+][-]
  1. SliderRightClick(Sender);
And do the rest in your SliderRightClick method.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

andyf97

  • New Member
  • *
  • Posts: 19
Re: Send a control name to a function or procedure
« Reply #4 on: March 29, 2023, 10:32:15 pm »
I do not have a rightclick events, I have a ContextPopup that all appears fine. I just dont get how to pass the name of it, to a function or procedure event tho I can put the name into a variable. But after writing I realized what you meant.

Yes that works but then I get another error got a Tobject but expected a Ttranslatesting.

Code: Pascal  [Select][+][-]
  1. SliderRightClick(Sender);
And do the rest in your SliderRightClick method.
« Last Edit: March 29, 2023, 10:35:42 pm by andyf97 »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1399
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Send a control name to a function or procedure
« Reply #5 on: March 29, 2023, 10:37:55 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ECSlider1ContextPopup(Sender: TObject; MousePos: TPoint;  
  2. var Handled: Boolean);
  3.  
  4. Var this:string;
  5.  
  6. begin
  7.  
  8.  this:=TComponent(sender).name;
  9. // This does contain the control name ECSlider1
  10.  
  11. //  SliderRightClick(this);
  12. // How can I send the name stored in the variable this to the procedure TForm1.SliderRightClick
  13.  
  14. // Thats how I would do, or I do really absolute not understand you....
  15.   SliderRightClick(Sender);
  16. end;
  17.  
  18. procedure TForm1.SliderRightClick(sender: TObject);
  19. begin
  20.   Form1.caption := TComponent(sender).name;
  21. end;
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

dseligo

  • Hero Member
  • *****
  • Posts: 986
Re: Send a control name to a function or procedure
« Reply #6 on: March 29, 2023, 10:38:52 pm »
Do something like this:

Code: Pascal  [Select][+][-]
  1.     // this is just a little procedure to see the name passes, just for testing I want the name to go to the caption of form1
  2.     procedure TForm1.SliderRightClick(sendername: String);
  3.     begin
  4.       Form1.caption := sendername;
  5.     end;
  6.      
  7.      
  8.     // this is the right click popup event of the control.
  9.     procedure TForm1.ECSlider1ContextPopup(Sender: TObject; MousePos: TPoint;  
  10.     var Handled: Boolean);
  11.      
  12.     Var this:string;
  13.      
  14.     begin
  15.      
  16.      this:=TComponent(sender).name;
  17.     // This does contain the control name ECSlider1
  18.      
  19.       SliderRightClick(this);
  20.     // How can I send the name stored in the variable this to the procedure TForm1.SliderRightClick
  21.      
  22.     end;

Or this:
Code: Pascal  [Select][+][-]
  1.     // this is just a little procedure to see the name passes, just for testing I want the name to go to the caption of form1
  2.     procedure TForm1.SliderRightClick(sender: TObject);
  3.     begin
  4.       If Sender is TComponent then
  5.         Form1.caption := (Sender as TComponent).Name;
  6.     end;
  7.      
  8.      
  9.     // this is the right click popup event of the control.
  10.     procedure TForm1.ECSlider1ContextPopup(Sender: TObject; MousePos: TPoint;  
  11.     var Handled: Boolean);
  12.     begin
  13.     // This does contain the control name ECSlider1
  14.       SliderRightClick(Sender);
  15.     // How can I send the name stored in the variable this to the procedure TForm1.SliderRightClick
  16.     end;

andyf97

  • New Member
  • *
  • Posts: 19
Re: Send a control name to a function or procedure
« Reply #7 on: March 29, 2023, 10:49:59 pm »
I was so close but so far, That was the magic I was missing,  many thanks for your help. 

Code: Pascal  [Select][+][-]
  1.   Form1.caption := TComponent(sender).name;
  2.  



Code: Pascal  [Select][+][-]
  1. procedure TForm1.ECSlider1ContextPopup(Sender: TObject; MousePos: TPoint;  
  2. var Handled: Boolean);
  3.  
  4. Var this:string;
  5.  
  6. begin
  7.  
  8.  this:=TComponent(sender).name;
  9. // This does contain the control name ECSlider1
  10.  
  11. //  SliderRightClick(this);
  12. // How can I send the name stored in the variable this to the procedure TForm1.SliderRightClick
  13.  
  14. // Thats how I would do, or I do really absolute not understand you....
  15.   SliderRightClick(Sender);
  16. end;
  17.  
  18. procedure TForm1.SliderRightClick(sender: TObject);
  19. begin
  20.   Form1.caption := TComponent(sender).name;
  21. end;

 

TinyPortal © 2005-2018