Solved

Make more effective, efficient, simple, and easy.

Posted on 1998-10-04
4
130 Views
Last Modified: 2010-04-06
I have many labels in form.
To make easier, I'm using array where this array contain the name of each labels.

  TMyForm = class(TForm)
    MyLabel1: TLabel;
    .
    .
    .
    .
    .
    MyLabel30: TLabel;

  private
    { Private declarations }
    TMyLabel: array [1..30] of TLabel;

  public
    { Public declarations }
  end;

I'm filled the name of each labels in procedure FormCreate, like this :

  procedure TFormUtama.FormCreate(Sender: TObject);
  begin
    TMyLabel[1] := MyLabel1;
    .
    .
    .
    .
    .
    TMyLabel[30] := MyLabel30;
  end;

I give as an example if I will change the color of label.

  for i := 1 to 30 do
  begin
    if TMyLabel[i].Caption = 'Delphi' then
      TMyLabel[i].Font.Color := clBlue;
  end;

This way is easier compared with if I am not using array, I have to check one by one of labels.
But I think if I'm using array, I have to fill the name of labels.
Any other way to solve this problem ?
How to make more effective, efficient, simple, and easy ?
So my program not too long.
Thanks.

Regards Joely
0
Comment
Question by:joely
  • 3
4 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 10 total points
ID: 1341760
Hello Joely. Take a look at the other question I've left a comment. Here is something about this question...
--------------------
var
  Labels : array[1..3] of TLabel;//Global variable so we can free it later with the button

{Create Labels}
procedure TForm1.Button1Click(Sender: TObject);
var
  I : integer;
begin
  for i := 1 to 3 do begin
    Labels[i] := TLabel.Create(self);
    with Labels[i] do begin
      Parent := Form1;
      Caption := 'Delphi ' + IntToStr(i);
      Top := 10;
      Left := i*100;
      Width := 100;
      Height := 16;
      Visible := True;
      if Odd(i) then
        Font.Color := clRed
      else
        Font.Color := clBlue;
    end;
  end;
end;

{Free Labels}
procedure TForm1.Button2Click(Sender: TObject);
var
  I : Integer;
begin
  for i := 1 to 3 do
    Labels[i].Free;
end;
--------------
btw- If you have questions ask....

Regards,
Viktor Ivanov
0
 

Author Comment

by:joely
ID: 1341761
Hello Viktor

I don't want to make this component at runtime.
Because I think make a program slowly (Do you agree ???).

For my question about "Click, Drag, and Drop", I am using component descends from TWinControl.
But I have still make initialization like TLabel.

Regards,
Joely
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1341762
yes but takes less space thus it takes up memory only at run time and that's not included in the EXE file... Depends on what you want. Less Bytes in your EXE or a bit slower. Actually if you create about 30 labels it won't be that slow but if you decide to create something with more thatn 100 labels it can get a bit slower with like half a second or so.... depends on the computer.. IF you got Pentium II your computer won't even feel that this is there and won's slow the machine even if you have 500 labels.... All depends on the computers' features...

I got your question....

Is this what you want to do....

TMyForm = class(TForm)
       MyLabel1: TLabel;
       .
       .
       .
       .
       .
       MyLabel30: TLabel;

     private
       { Private declarations }
       TMyLabel : array [1..30] of TLabel;

     public
       { Public declarations }
     end;

   I'm filled the name of each labels in procedure FormCreate, like this :

     procedure TFormUtama.FormCreate(Sender: TObject);
var
  I : integer;
     begin
  for i := 1 to 30 do
    TMyLabel := TLabel(FindComponent('Label' + IntToStr(i)));
     end;

   I give as an example if I will change the color of label.

     for i := 1 to 30 do
     begin
       if TMyLabel[i].Caption = 'Delphi' then
         TMyLabel[i].Font.Color := clBlue
    else
    TMyLabel[i].Font.Color := clRed;
     end;
---------
As long as the other question goes about the Drag, Click Drop..... you can do what I told you to do if the component is a descendant of TWinControl.... here is the code again...

begin
  if ssLeft in Shift then begin
    ReleaseCapture;
    TControl(Sender).Perform(WM_SYSCOMMAND, $F012, 0);
  end;
end;

P.S. More help??? Just ask ;->

Regards,
Viktor Ivanov
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1341763
Actually I forgot something in your proc...

Here is the fix..In the OnCreate() of the form

procedure TFormUtama.FormCreate(Sender: TObject);
var
  I : integer;
begin
  for i := 1 to 30 do
    TMyLabel[i] := TLabel(FindComponent('Label' + IntToStr(i)));
end;

Regards,
Viktor Ivanov
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now