create a timage and tlabel at runtime

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.
sabog7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sabog7Author Commented:
who ever can answer this question will become my master and idol..... please teach me..lend me ur brains amigo...
0
JimBob091197Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sabog7Author Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

JimBob091197Commented:
All in the name of information sharing!  :)

JB
0
sabog7Author Commented:
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
JimBob091197Commented:
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
sabog7Author Commented:
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
sabog7Author Commented:
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
JimBob091197Commented:
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
sabog7Author Commented:
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
JimBob091197Commented:
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
sabog7Author Commented:
thanks master BOB......
i really apreciate your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.