Solved

Make more effective, efficient, simple, and easy.

Posted on 1998-10-04
4
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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