Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 10 total
Thread Label in TPanel
Sun, Oct 15 2017 7:51 PMPermanent Link

KimHJ

Comca Systems, Inc

I'm trying to create a label inside  a TPanel. I get an error at: MyPanel := FindComponent('PT' + Ticketnum) as TPanel;

var
MyPanel: TPanel;
begin      
 PickScrollPanel.BeginUpdate;
 try     
       with TPanel.Create(PickScrollPanel) do
          begin
           Layout.Position:=lpTopLeft;
          Layout.Consumption:=lcRight;
          Layout.Overflow:=loBottom;
          Background.Fill.Color:=clLightGreen;
          CaptionBar.Background.Fill.Color:=clDarkGreen;
          CaptionBar.Caption:='Ticket '+Ticketnum;
          CaptionBar.AllowMinimize:=True;  
          Captionbar.AllowClose := False;
          CaptionBar.AllowMove:=False;
          CaptionBar.Height := 22;
          CaptionBar.Font.Style.Bold := True;
          Width:=180;
          Height:=150;
          Margins.Left:=5;
          Margins.Top:=5;
          Name := 'PT' + Ticketnum;
          end;
          MyPanel := FindComponent('PT' + Ticketnum) as TPanel;
      with TLabel.Create(MyPanel) do
          begin
                Layout.Position:=lpTopLeft;
                Layout.Consumption:=lcRight;
                Layout.Overflow:=loBottom;
                Margins.Top := 3;
                Margins.Left := 3;
                Margins.Right := 3;
                Margins.Bottom := 3;
                Name := 'PTLB' + Ticketnum;
                Caption := 'This a test\r\n and this is a new line';
                Font.Color := clElevateBlue;
                Parent := MyPanel;
          end;
 finally
    PickScrollPanel.EndUpdate;
 end;


Thanks for any help.
Kim
Mon, Oct 16 2017 1:31 AMPermanent Link

Michael Dreher

KimHJ wrote:

// I get an error at: MyPanel := FindComponent('PT' + Ticketnum) as TPanel;

The help says on type casting:

  // Language Reference / Casting Types:
  // [casting] is accomplished by enclosing the target value with the target type name and parentheses.

So this will work:

MyPanel := TPanel(FindComponent('PT' + Ticketnum));

I'am not sure if the syntax ( <expression> "as" <type> ) is valid. Although "as" is shown in the keyword list.
Michael Dreher
Mon, Oct 16 2017 4:23 AMPermanent Link

Matthew Jones

I do wish "with" would die. But it does seem odd to have the panel reference there, and then not be able to use it directly afterward.


   MyPanel := TPanel.Create(PickScrollPanel);
   with MyPanel do
   begin
...

--

Matthew Jones
Mon, Oct 16 2017 11:22 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< I do wish "with" would die. But it does seem odd to have the panel reference there, and then not be able to use it directly afterward. >>

As far as I can see, that's not the issue.  The issue is the use of "as" for typecasting.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Oct 16 2017 11:23 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Kim,

<< I'm trying to create a label inside  a TPanel. I get an error at: MyPanel := FindComponent('PT' + Ticketnum) as TPanel; >>

I'm not sure why the "as" operator wasn't implemented, but it appears that this is indeed the case.  For now the workaround is to use the direct cast, as Michael indicated.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Oct 16 2017 12:13 PMPermanent Link

Matthew Jones

Tim Young [Elevate Software] wrote:

> << I do wish "with" would die. But it does seem odd to have the panel reference there, and then not be able to use it directly afterward. >>
>
> As far as I can see, that's not the issue.  The issue is the use of "as" for typecasting.

Sure, I was just picking up on something I saw as illogical. Particularly as it then eliminates the need for the bad cast that was failing too.

--

Matthew Jones
Mon, Oct 16 2017 1:44 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< Sure, I was just picking up on something I saw as illogical. Particularly as it then eliminates the need for the bad cast that was failing too. >>

Sorry, I read your post wrong - I thought you were referring to the end of the first "with" block, when you were referring to the usage of MyPanel at the *beginning* of the first "with" block.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Oct 16 2017 1:45 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

And yes, it would eliminate the necessity of the cast in the first place. Smile

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Oct 16 2017 5:58 PMPermanent Link

KimHJ

Comca Systems, Inc

Michael Dreher wrote:

>>So this will work:

MyPanel := TPanel(FindComponent('PT' + Ticketnum));

I'am not sure if the syntax ( <expression> "as" <type> ) is valid. Although "as" is shown in the keyword list.<<

I already tried that before i posted, it will compile but the MyPanel is nil.

Kim
Mon, Oct 16 2017 8:52 PMPermanent Link

KimHJ

Comca Systems, Inc

"Matthew Jones" wrote:

>>    MyPanel := TPanel.Create(PickScrollPanel);
   with MyPanel do
   begin<<

Thanks Matthew that works.

Kim
Image