Solved

Make more effective, efficient, simple, and easy.

Posted on 1998-10-04
4
129 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
System restore point 4 77
Delphi XE10, MySQL Query 4 107
code issue 8 84
When I am typing into a TEdit last word repeats 7 86
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

14 Experts available now in Live!

Get 1:1 Help Now