Solved

create a timage and tlabel at runtime

Posted on 1998-04-10
12
532 Views
Last Modified: 2010-04-06
question
1. howcan i create a tlabel at runtime and how can i delete or change their properties like changing the font color at runtime every time i click one of those created label.could i use (setfocous) on them so that i can choose whichever label  i want to manipulate?..
2.how can i create a timage and manage to change the picture of the one i chose at runtime.
0
Comment
Question by:sabog7
  • 7
  • 5
12 Comments
 

Author Comment

by:sabog7
ID: 1361375
who ever can answer this question will become my master and idol..... please teach me..lend me ur brains amigo...
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 20 total points
ID: 1361376
Hi

1.  Regarding creating a TLabel at run-time:  The following code (on the OnClick event on a button) will create a label and place it on the current form.

procedure TForm1.Button1Click(Sender: TObject);
var
  lbl: TLabel;
begin
  lbl := TLabel.Create(Self);
  lbl.Parent := Self;
  lbl.Left := 50;
  lbl.Top := 50;
  lbl.Caption := 'Created at run-time.';
end;

To get it to respond to clicks (a TLabel doesn't have a focus event) you need to assign the OnClick event.

For example, after the lbl.Caption := ... code above, you could add this:
  lbl.OnClick := DoLabelClick;

Then, in your form's code you would have the following procedure (which toggles the label's color between red and black):
procedure TForm1.DoLabelClick(Sender: TObject);
begin
  with TLabel(Sender) do
    if (Font.Color = clBlack) then
      Font.Color := clRed
    else
      Font.Color := clBlack;
end;

2.  Regarding the TImage, you create it at run-time in the same way as the TLabel.  To assign a picture (assuming you know the file name) you call:
MyImage.Picture.LoadFromFile('C:\WhatEver\AFileName.bmp');

Regards,
JB
0
 

Author Comment

by:sabog7
ID: 1361377
u got an exellent answear master bob i will never ever forget u in my whole entire life thank u very very much.....
from the slave.......
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1361378
All in the name of information sharing!  :)

JB
0
 

Author Comment

by:sabog7
ID: 1361379
Master
i got an additional question
1. if u create say five tlabel how can u manipulate the no. 3 label like changing the caption and font style of that tlabel?.....
2. i tried to add the ondblclick after the on click but its not working....
can u help me.... MASTER BOB......

 
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1361380
Hi

1. You need some way of keeping track of your labels.  You could do this by declaring "Lbl1, Lbl2, Lbl3, Lbl4, Lbl5: TLabel;" in your form's "public" section, or maybe an array (LblArr: array[1..5] of TLabel) or a TList of labels.

2.  What exactly is it doing?  Do you have a click and a double-click assigned?

JB
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:sabog7
ID: 1361381
on the onclick event i put a certain component(strecthhandle) that the tlabel can be move in any position and on the double click of the tlabel a fontdialogbox will execute and change the font style,color etc.  


0
 

Author Comment

by:sabog7
ID: 1361382
followup question MASTER BOB
i tried the timage creation and it works...

now here's the situation....
1.im trying to create a timage..
2.im using another form to choose an image by using a filelistbox. after closing that form that image i choose will be assign to the one im creating it... thanks in advance MASTER...
IT WILL HELP ME ALOT IN MY CASE STUDY.....

0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1361383
Hi again,

1.  Regarding the Label's OnClick & OnDblClick.
If you dbl click the label, the click event will be fired and then the dbl-click event.  I have tested this, and this is how it works on my PC.

2.  Regarding your image thingie.
I'm not sure I follow you, but if you're trying to get the image (file name??) from the listbox on form 2 to display in the TImage in form1, then this is what to do:
Form2 Code:
on listbox dblClick (e.g.):
  if (listbox.ImageIndex >= 0) then
    ModalResult := mrOk;

Form1 Code:
(Add "Form2" to your uses clause.)
On a button click (or something):
with TForm2.Create(Application) do
  try
    if (ShowModal = mrOk) then
      MyImage.Picture.LoadFromFile(ListBox.Items[ListBox.ItemIndex]);
  finally
    Free;
  end;

I'm not sure if this is what you were looking for...
Cheers,
JB
0
 

Author Comment

by:sabog7
ID: 1361384
MASTER BOB...
i think i got the onclick and dblclick....but this time
im trying to do this in the onclick event
"lbl.OnClick := Button2Click;"
and on Button2Click is (sender as tlabel).font.color :=   clblue;
and when i run it i got an error of invalid class type cast.
why did i got an error like that..i copied this code from a sample program...what is wrong with this code? ...pls help me again......thank u thank u.....
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1361385
Hi

The code "lbl.OnClick := Button2Click" should run fine, but I presume the error is on the (Sender as TLabel) part.  This is because Sender is only a TLabel if you click on the label.  If you click on Button2, then Sender is TButton.

You can try "Sender as TControl" instead of "Sender as TLabel".  Also, "TControl(Sender)" will also work.

E.g. TControl(Sender).Font.Color := clBlue;

Cheers,
JB
0
 

Author Comment

by:sabog7
ID: 1361386
thanks master BOB......
i really apreciate your help
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

22 Experts available now in Live!

Get 1:1 Help Now