Solved

create a timage and tlabel at runtime

Posted on 1998-04-10
12
536 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Correct Component for Shopping Cart. 2 107
delphi parse string to params 3 126
Press three keys together and trigger a function 3 57
Convert a string into a TDateTime 5 61
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

825 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