Recent

Author Topic: How to reference a control without explicitly naming it  (Read 806 times)

Username

  • New Member
  • *
  • Posts: 30
How to reference a control without explicitly naming it
« on: May 10, 2022, 07:44:38 am »
In a button routine, or for example the onclick event in the sample code below, "self" refers to Form1. Is there a similar word or way to reference the TEdit control?

procedure TForm1.Edit_SP_1_0Change(Sender: TObject);
begin
   if (Edit_SP_1_0.text='') then //Works as it should.
     begin
     Edit_SP_1_0.text:='0';
     end;
end;
//**********WRONG...*************
procedure TForm1.Edit_SP_1_0Change(Sender: TObject);
begin
   if (self.text='') then //Here self is Form1. I want to reference the Edit control somehow like this.
     begin
     self.text:='0';
     end;
end;
Any way to do this?
TIA

bytebites

  • Hero Member
  • *****
  • Posts: 625
Re: How to reference a control without explicitly naming it
« Reply #1 on: May 10, 2022, 08:00:06 am »
Code: Pascal  [Select][+][-]
  1. TEdit(Sender).text:='0';

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to reference a control without explicitly naming it
« Reply #2 on: May 10, 2022, 08:00:44 am »
or, safer,
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit_SP_1_0Change(Sender: TObject);
  2. begin
  3.   if (Sender is TEdit) then
  4.     TEdit(Sender).Text := '0';
  5. end;

Username

  • New Member
  • *
  • Posts: 30
Re: How to reference a control without explicitly naming it
« Reply #3 on: May 10, 2022, 08:03:32 am »
DOH!  %)
Thanks all.  ::)

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: How to reference a control without explicitly naming it
« Reply #4 on: May 10, 2022, 08:03:44 am »
Yeah, same event handler can be used for different controls, if procedure declaration (i.e. number of parameters) is the same. But you need to check Sender's type then. It can be done via "is" operator.
Code: Pascal  [Select][+][-]
  1.   if Sender is TEdit then...
  2.  
There are two ways to cast Sender to other class: with and without type check. If you've already verified it's type, then cast without type check can be performed. It's "free" operation, because for processor all pointers are untyped.
Code: Pascal  [Select][+][-]
  1.   var Edit:TEdit;
  2. begin
  3.   Edit := TEdit(Sender);
  4.  
Cast with type check is performed via "as" operator and will rise exception if type doesn't match or control's class isn't inherited from desired class.
Code: Pascal  [Select][+][-]
  1.   var Edit:TEdit;
  2. begin
  3.   Edit := Sender as TEdit;
  4.  
This operation isn't "free", because it implies built-in type check via "is" operator. Then code's logic can be based on exception handling, rather than on explicit type check.
Code: Pascal  [Select][+][-]
  1. try
  2.   Edit := Sender as TEdit;
  3.   //It's TEdit!
  4. except
  5.   //It's not TEdit!
  6. end;
  7.  
« Last Edit: May 10, 2022, 08:18:13 am by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Username

  • New Member
  • *
  • Posts: 30
Re: How to reference a control without explicitly naming it
« Reply #5 on: May 10, 2022, 08:11:20 am »
Yeah, same event handler can be used for different controls, if procedure declaration (i.e. number of parameters) is the same. But you need to check Sender's type then. It can be done via "is" operator.
Code: Pascal  [Select][+][-]
  1.   if Self is TEdit then...
  2.  

Mr,Madguy, did you mean "If Sender is TEdit"? or Self?

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: How to reference a control without explicitly naming it
« Reply #6 on: May 10, 2022, 08:19:44 am »
Mr,Madguy, did you mean "If Sender is TEdit"? or Self?
Yeah, wrote Self everywhere, then noticed my mistake, but failed to fix it everywhere. Self would point to control, where event handler is declared. In most cases - your form.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Username

  • New Member
  • *
  • Posts: 30
Re: How to reference a control without explicitly naming it
« Reply #7 on: May 10, 2022, 08:25:24 am »
OK Thanks.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: How to reference a control without explicitly naming it
« Reply #8 on: May 11, 2022, 07:56:53 pm »
This operation isn't "free", because it implies built-in type check via "is" operator.
Rather very similar. This check is more gentle: allow nil.

 

TinyPortal © 2005-2018