?
Solved

Make more effective, efficient, simple, and easy.

Posted on 1998-10-04
4
Medium Priority
?
139 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 40 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 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