Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

create a timage and tlabel at runtime

Posted on 1998-04-10
12
Medium Priority
?
547 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
[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
  • 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 80 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
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

610 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