• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

With LabelObject as TLabel - combine with Tpanel within the same with

Hi experts,

what i am trying to do is to combine TLabel and TPanel objects within one with.

var
   HighLightObject:TObject;
begin
   if Sender is TLabel then
       ... here i am looking for particular object and checking some things too boring to be listed
        HighLightObject:=label;

   if Sender is TPanel then
       ... the same here
       HighLightObject:=TPanel;


... now i want to basically highlight object,

if HighLightObject <> nil then
begin
   with HighLightObject as TLabel do
   begin
      HighLightShape.Top:=Top-10;
      HighLightShape.Height:=Height+20;
      HighLightShape.Left:=left-10;
      HighLightShape.Width:=Width+20;
      HighLightShape.Visible:=True;
   end;
end;

... what i don't want to do is to repeat the same for TPanel and other objects (Image,Edit,Memo,Radio,CheckBox ... )
something like this would be perfect
with HighLightObject as TLabel,TPanel,TImage .... do

now down to earth, any suggestions how can i chuck it into the same with...?

(I've given 350 points to this question, as i have spent 3 hours trying to do it... and i don't consider my self dummy)
0
FDzjuba
Asked:
FDzjuba
3 Solutions
 
BlackTigerXCommented:
something like this... using the TControl anscestor:

var
  X:Integer;
begin
  for X:=0 to ComponentCount-1 do
  begin
    if (Components[X] is TControl) then
    begin
      (Components[X] as TControl).Top:=(Components[X] as TControl).Top-10;
      (Components[X] as TControl).Height:=(Components[X] as TControl).Height+20;
    end
  end
0
 
BlackTigerXCommented:
you can also specify the component names to filter out only the ones you want

  for X:=0 to ComponentCount-1 do
  begin
    if (Components[X] is TControl) then
    begin
      if ((Components[X] as TControl).Name='Label1') or
         ((Components[X] as TControl).Name='CheckBox1') then
      begin
        (Components[X] as TControl).Top:=(Components[X] as TControl).Top-10;
        (Components[X] as TControl).Height:=(Components[X] as TControl).Height+20;
      end
    end
  end
0
 
DelaniCommented:
I get from your post you are only trying to use the same properties of the TPanel, TLabel, and so on. The logical thing to do is casting everything only to the common ancestor, in this case TControl. The code would look something like this (based on your provided sample):

var
   HighLightObject:TControl; // *** Use TControl here, so you avoid casting later ****
begin
   if Sender is TLabel then
       ... here i am looking for particular object and checking some things too boring to be listed
        HighLightObject:=label;

   if Sender is TPanel then
       ... the same here
       HighLightObject:=TPanel;


... now i want to basically highlight object,

if HighLightObject <> nil then
begin
   with HighLightObject do // *** You can now use all properties as defined by TControl
   begin
      HighLightShape.Top:=Top-10;
      HighLightShape.Height:=Height+20;
      HighLightShape.Left:=left-10;
      HighLightShape.Width:=Width+20;
      HighLightShape.Visible:=True;
   end;
end;

I hope this helps you.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
BlackTigerXCommented:
right... you can even do:

var
   HighLightObject:TControl; // *** Use TControl here, so you avoid casting later ****
begin
   HighLightObject:=(Sender as TControl); //from now on you can use this variable to check all the common properties to all of the controls
   if Sender is TLabel then
       ... here i am looking for particular object and checking some things too boring to be listed
        //HighLightObject:=label; //you don't need this now

   if Sender is TPanel then
       ... the same here
       //HighLightObject:=TPanel; //you don't need this now

0
 
esoftbgCommented:
I think the right way is below:

var
   HighLightObject:TObject;
begin

//........................................
//........................................

  if HighLightObject <> nil then
  if (HighLightObject is TControl) then
  begin
     with (HighLightObject as TControl) do
     begin
        HighLightShape.Top:=Top-10;
        HighLightShape.Height:=Height+20;
        HighLightShape.Left:=left-10;
        HighLightShape.Width:=Width+20;
        HighLightShape.Visible:=True;
     end;
  end;
end;
0
 
FDzjubaAuthor Commented:
I have increased points to 500 and split them between BlackTigerX , Delani, esoftbg
0
 
DelaniCommented:
Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now