Results 1 to 4 of 4

Thread: 'with do' question

  1. #1

    'with do' question

    Is there a way to do that? :

    Code:
    with TLabel.create(nil) do begin
      ...
      someList.Add(this TLabel?);
    end;
    other than of course
    Code:
    L:=tlabel.create
    with L do begin
      ...
      someList.Add(L)
    end

  2. #2
    No that's not possible. Best you can do is:
    Code:
    someList.Add(TLabel.create(form1));
    with TLabel(someList.Items[someList.Items.Count-1]) do begin
      ...
    end;
    (You shouldn't create them with nil. Otherwise you have to do the garbage collection (freeing them) all by yourself.)

  3. #3
    I actually don't do that but it was shorter to type this way
    thanks

  4. #4
    If you are adding all of this labels to some list you could go and create your own TMyLabel class which is deprecated class of TLabel. Doing so you can change default TLabel constructor so that Label gets added into your list ass soon as it is created:
    Code:
    type
      TMyLabel = class(TLabel)
      public
        constructor Create(Owner: TComponent; List: TObjectList);
      end;
    
      ....
    
    constructor TMyLabel.Create(Owner: TComponent; List: TList);
    begin
        inherited Create(Owner);
        List.Add(self);
    end;
    And now you can simply use:
    Code:
    with TMyLabel.create(Form1,someList) do begin
      ...
    end;

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •