Forum > LCL

How to reference a control without explicitly naming it

(1/2) > >>

Username:
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:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TEdit(Sender).text:='0';

howardpc:
or, safer,

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Edit_SP_1_0Change(Sender: TObject);begin  if (Sender is TEdit) then    TEdit(Sender).Text := '0';end;

Username:
DOH!  %)
Thanks all.  ::)

Mr.Madguy:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  if Sender is TEdit then... 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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  var Edit:TEdit;begin  Edit := TEdit(Sender); 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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  var Edit:TEdit;begin  Edit := Sender as TEdit; 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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---try  Edit := Sender as TEdit;  //It's TEdit!except  //It's not TEdit!end; 

Navigation

[0] Message Index

[#] Next page

Go to full version