How to reference the object property created with `with Tedit.Create(Self)`?

Asked

Viewed 40 times

1

How to reference the property of a purpose created with with TEdit.Create(Self), and the use of this will be used in another object that is also being created as with TSpeedButton.Create(Self)?

Follows the code:

with TEdit.Create(Self) do
begin
  Name   := 'ed'+IntToStr(i);
  Parent := Self;
  Top    := 0;
  Left   := 0;
  Width  := 50;

  with TSpeedButton.Create(Self) do
  begin
    Name   := 'sb'+IntToStr(i);
    Parent := Self;
    Top    := 0;
    //Left := TEdit().Left + TEdit().Width; //Como referenciar o TEdit?
    Width  := 20;
  end;
end;
  • What is the need to use this waterfall?

  • Because you don’t create a Tedit type variable, feed it with the respective Tedit you want to call, it gets easier. It is not very advisable to use the with, then it becomes difficult to analyze the source and give maintenance.

  • Actually, I was already aware of this way of referencing. The cascading mode is because I’m kind of doing a framework. All objects will be created at runtime according to the records in the database tables. The doubt was more focused on the use of with cascading, in this case I will even have to create variables right.

1 answer

4


I see no need to cascade the creation of the component, but anyway you refer using the name of the component you created:

with TEdit.Create(Self) do
begin
  Name   := 'ed'+IntToStr(i);
  Parent := Self;
  Top    := 0;
  Left   := 0;
  Width  := 50;

  with TSpeedButton.Create(Self) do
  begin
    Name   := 'sb'+IntToStr(i);
    Parent := Self;
    Top    := 0;
    Left := TEdit('ed'+IntToStr(i)).Left + TEdit('ed'+IntToStr(i)).Width; 
    Width  := 20;
  end;
end;

I would do it separately, each in its code block:

with TEdit.Create(Self) do
begin
  Name   := 'ed'+IntToStr(i);
  Parent := Self;
  Top    := 0;
  Left   := 0;
  Width  := 50;
end;

with TSpeedButton.Create(Self) do
begin
  Name   := 'sb'+IntToStr(i);
  Parent := Self;
  Top    := 0;
  Left := TEdit('ed'+IntToStr(i)).Left + TEdit('ed'+IntToStr(i)).Width; 
  Width  := 20;
end;

And the correct thing would still be to create Variables for each component, so it is more organized and easy to locate the components.

var
  vEdtUsuario : TEdit;
  vBtn_Ok     : TSpeedButton;
  • Actually, I was already aware of this way of referencing. The cascading mode is because I’m kind of doing a framework. All objects will be created at runtime according to the records in the database tables. The doubt was more focused on the use of with cascading, in this case I will even have to create variables right.

  • 1

    Yes, it increments their name with the amount of records being returned by the Bank.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.