Solved

Make more effective, efficient, simple, and easy.

Posted on 1998-10-04
4
136 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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

717 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