Board index » delphi » Dephi equivalent to VB control array?

Dephi equivalent to VB control array?

Hi,

I know this one is in some FAQ somewhere, but I didn't find it yet. I'm
using the following code to highlight a label on mousemove event, the
problem is that I need one procedure for each label with this code.

procedure TfrmMain.lblCBTMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
     if lblCBT.Font.Color <> clYellow then
        begin
        lblShadow.Visible := False;
        lblCBT.Font.Color := clYellow;
        lblShadow.Caption := lblCBT.Caption;
        lblShadow.Left := lblCBT.Left + 3;
        lblShadow.Top := lblCBT.Top + 3;
        lblShadow.Visible := True;
        end;
end;

In VB I could easily use a control array to reference the sender label
directly  (using it's index property) but I have no clue on how to do this
in Dephi. I just got Delphi 4 Standard and I like the tool very much but
have more difficulties with the doc.

I would be happy just being able to use something like "Sender.Name" or a
common handler set at design time like:

procedure HighlightLabel(Sender: TObject; Shift: TShiftState; X, Y:
Integer);
begin
     if Sender.Font.Color <> clYellow then
        ...
end;

How do I get to use the Sender object.

Thanks for any pointer.

Regards,
Raymond

 

Re:Dephi equivalent to VB control array?


Raymond,

This is the sort of thing you need to do:

Charles Johnson

procedure TForm1.AllLabelsMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);

begin
  if Sender is TLabel then with Sender as TLabel do
  begin
    Font.Color := clYellow;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to ComponentCount - 1 do
  if Components[i] is TLabel then
  (Components[i] as TLabel).OnMouseMove := AllLabelsMouseMove;
end;

Re:Dephi equivalent to VB control array?


I think i know what your looking for...
there are two ways to do this.
 #1.
     at Formcreate time you can load and Tobject array table with the
Label value, Sender is nothing more then the Address of the Form and the
control with in it..
 if you were to load and array table of these values then you could scan
the
array for a sender match and know the index..

 #2.
   Create your labels at run time and have them in an array part of the
form..
 You can place an Array inside and object like this.
form1 = class(ecgtt)
  myLabels:Array[0..10] of Tlabel;
end;

  in your formcreate just create them,,.,
  MyLabels[?] := Tlabel.create;'
  MyLabels[?]:= caption := '??????';
 ectt,,,

Raymond Bissonnette <rayb...@cam.org> wrote in article
<6rgdlg$...@tandem.CAM.ORG>...

Quote
> Hi,

> I know this one is in some FAQ somewhere, but I didn't find it yet. I'm
> using the following code to highlight a label on mousemove event, the
> problem is that I need one procedure for each label with this code.

> procedure TfrmMain.lblCBTMouseMove(Sender: TObject; Shift: TShiftState;
X,
>   Y: Integer);
> begin
>      if lblCBT.Font.Color <> clYellow then
>         begin
>         lblShadow.Visible := False;
>         lblCBT.Font.Color := clYellow;
>         lblShadow.Caption := lblCBT.Caption;
>         lblShadow.Left := lblCBT.Left + 3;
>         lblShadow.Top := lblCBT.Top + 3;
>         lblShadow.Visible := True;
>         end;
> end;

> In VB I could easily use a control array to reference the sender label
> directly  (using it's index property) but I have no clue on how to do
this
> in Dephi. I just got Delphi 4 Standard and I like the tool very much but
> have more difficulties with the doc.

> I would be happy just being able to use something like "Sender.Name" or a
> common handler set at design time like:

> procedure HighlightLabel(Sender: TObject; Shift: TShiftState; X, Y:
> Integer);
> begin
>      if Sender.Font.Color <> clYellow then
>         ...
> end;

> How do I get to use the Sender object.

> Thanks for any pointer.

> Regards,
> Raymond

Re:Dephi equivalent to VB control array?


In article <6rgdlg$...@tandem.CAM.ORG>, "Raymond Bissonnette" <rayb...@cam.org>
writes:

Quote
>In VB I could easily use a control array to reference the sender label
>directly  (using it's index property) but I have no clue on how to do this
>in Dephi. I just got Delphi 4 Standard and I like the tool very much but
>have more difficulties with the doc.

>I would be happy just being able to use something like "Sender.Name" or a
>common handler set at design time like:

The best way is just to typecast the Sender object to access the particular
"Sender"

 Sender is specified as a TObject so you cannot say Sender.Font.Color because a
TObject does not have a Font (let alone a colour) and the compiler says that
you cannot do that. But you can "typecast" the TObject to your particular
object which does have a Font. But the compiler is taking your word and if you
are wrong its a run-time error. So you code :-

TLabel(Sender).Font.Color := clYellow;

There are two arrays of objects - Components[] (items whose Owner is the object
with the array), and Controls[] (items whose Parent is the object with the
array). Most items on a form are Owned by the form. But items visually placed
on an object are children of that particular object (ie a TButton on a Form is
owned and parented by the form, a TButton placed on a TPanel is owned by the
Form, but Parented by the TPanel).

Having these arrays you can look for identity with some known value. Rather
like the above but look through the array of components to establish a match
with numbers you have specified. Suppose you put a value of 24 in the Tag
property of an TLabel, you could find the particular TLabel by coding :-

for i := 0 to ComponentCount - 1 do
  if Components[i].Tag := 24 then  // this is your component
    etc etc

 . .  or look for the name . . .

for i := 0 to ComponentCount - 1 do
  if Components[i].Name := 'MyLabel1'  then  // this is your component
    etc etc

 . . . but note that Components[i] is a TComponent and so you cannot access a
property (like Tag or Name) unless a TComponent has that property or you
typecast the Components[i] to your component descendant which has the property
you're using (ie TLabel(Components[i]).Font.Color := clYellow).

Similarly for the Controls[] array.

Note also that Delphi will use the Form instance (Self) by default to make a
fully qualified name. So if you code Controls[i], Delphi takes it as
"YourFormInstance".Controls[i]. If you want to look at the controls on a TPanel
you would have to specify MyPanel.Controls[i].

Alan Lloyd
alangll...@aol.com

Re:Dephi equivalent to VB control array?


Thanks,

It worked great.

Regards,
Raymond

Quote
CEHJohnson wrote in message

<1998082017361900.NAA15...@ladder01.news.aol.com>...
Quote
>Raymond,

>This is the sort of thing you need to do:

>Charles Johnson

>procedure TForm1.AllLabelsMouseMove(Sender: TObject; Shift: TShiftState; X,
>  Y: Integer);

>begin
>  if Sender is TLabel then with Sender as TLabel do
>  begin
>    Font.Color := clYellow;
>  end;
>end;

>procedure TForm1.FormCreate(Sender: TObject);
>var
>  i: integer;
>begin
>  for i := 0 to ComponentCount - 1 do
>  if Components[i] is TLabel then
>  (Components[i] as TLabel).OnMouseMove := AllLabelsMouseMove;
>end;

Other Threads