Solved

create a timage and tlabel at runtime

Posted on 1998-04-10
12
535 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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, f…

911 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

15 Experts available now in Live!

Get 1:1 Help Now