Solved

Creating Controls at Runtime in Delphi 6

Posted on 2003-10-30
12
306 Views
Last Modified: 2010-04-05
I want to create an Image control on my form, (named frmMain).

In the Image control, I wish to place a picture (in this case, a picture of the Ace of Spades) that is loaded from my Resource file (which I can access without any problems).

Here's the code that I have...

procedure DrawCard();
var
  bmp : TBitmap;
  Card : TImage;

begin
  bmp := TBitmap.Create;
  bmp.LoadFromResourceName(hInstance, 'CARD1');

  Card := TImage.Create(frmMain);

  with Card do begin
    Left := 120;
    Top := 120;
    Width := 71;
    Height := 96;
    Parent := frmMain;
    Visible := True;
  end;

  bmp.Free;
  Card.Free;    // Release the resource
end;

Can anyone tell me why I can't see my Image control with the picture of the ace of spades in it?  How do I "fix" it?

Thanks alot,

D.
0
Comment
Question by:inethog
  • 5
  • 5
  • 2
12 Comments
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
Well, it is because the Card (TImage) object  is a local variable and goes out of scope
when you exit the procedure DrawCard.

If you made the TImage Card a public or private variable of your form, it wouldn't happen.
You'll also have to create and free the object in your formcreate and formdestroy and
make DrawCard a form method. Make these changes...OTTOMH so, please, test this first.


type
  TfrmMain = class(TForm)
    .
    .
  public
    Card : TImage;
    procedure DrawCard;
  end;

implementation

procedure TTfrmMain.DrawCard;
var
  bmp : TBitmap;
begin
  bmp := TBitmap.Create;
  bmp.LoadFromResourceName(hInstance, 'CARD1');
  Card.Picture.Bitmap.Assign(bmp);
  bmp.Free;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  with Card := TImage.Create(Self) do
  begin
    Left := 120;
    Top := 120;
    Width := 71;
    Height := 96;
    Parent := frmMain;
    Visible := True;
  end;
end;

procedure TfrmMain..FormDestroy(Sender: TObject);
begin
  if Assigned(Card) then
    Card.Free;    // Release the resource
end;
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
Another thing was that you weren't assigning the bitmap to the TImage.
0
 
LVL 5

Accepted Solution

by:
snehanshu earned 200 total points
Comment Utility
inethog,
In your procedure,
1) You are freeing Card after creatint it: so remove card.free
2) You are not assigning the bitmap to the image.
3) You can load the batmip into Card directly by
 Card.Picture.Bitmap.LoadFromResourceName(hInstance, 'CARD1');
So, your procedure should be:

procedure DrawCard();
var
  Card : TImage;

begin

  Card := TImage.Create(frmMain);
  Card.Picture.Bitmap.LoadFromResourceName(hInstance, 'CARD1');
  with Card do begin
    Left := 120;
    Top := 120;
    Width := 71;
    Height := 96;
    Parent := frmMain;
    Visible := True;
  end;
end;

Cheers!
...Snehanshu
0
 
LVL 1

Author Comment

by:inethog
Comment Utility
Ok, I've made the changes as suggested but when I try to compile the code,

this line [ with frmMain.Card := TImage.Create(Self) do ] gives me an error.
[ DO expected but found := ]

Here's the complete code listing for the module that I'm working on....


unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, Cards, ImgList, ExtCtrls;

type
  TfrmMain = class(TForm)
    mnuMain: TMainMenu;
    mnuGame: TMenuItem;
    mnuNew: TMenuItem;
    N1: TMenuItem;
    mnuDeal: TMenuItem;
    N2: TMenuItem;
    mnuExit: TMenuItem;
    imgCards: TImageList;
    procedure mnuExitClick(Sender: TObject);
    procedure mnuNewClick(Sender: TObject);

  private
    { Private declarations }

  public
    { Public declarations }
    Card : TImage;
    procedure DrawCard();

  end;

var
  frmMain : TfrmMain;

implementation

{$R *.DFM}
{$R NEWCARDS.RES}

procedure TfrmMain.mnuExitClick(Sender: TObject);
begin
  { ** Terminate the Application ** }
  application.Terminate;
end;

procedure TfrmMain.mnuNewClick(Sender: TObject);
begin
  { ** Begin a new game ** }

  DrawCard();

  mnuNew.Enabled := False;
  mnuDeal.Enabled := True;
end;

procedure TfrmMain.DrawCard();
var
  bmp : TBitmap;

begin
  bmp := TBitmap.Create;
  bmp.LoadFromResourceName(hInstance, 'CARD1');
  frmMain.Card.Picture.Bitmap.Assign(bmp);
  bmp.Free;

  with frmMain.Card := TImage.Create(Self) do
  begin
    Left := 120;
    Top := 120;
    Width := 71;
    Height := 96;
    Parent := frmMain;
    Visible := True;
  end;
end;

end.


0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
OK, modify to this:

Card := TImage.Create(Self);
with Card do
  begin
    Left := 120;
    Top := 120;
    Width := 71;
    Height := 96;
    Parent := frmMain;
    Visible := True;
  end;
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
But, do like I said and create Card in the FormCreate and free it in the FormDestroy.
That is the best way to do it.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:inethog
Comment Utility
Thanks alot to everyone who posted here :)

I'm still a newb @ this and I do appreciate the help.

Cheers,

D.
0
 
LVL 5

Expert Comment

by:snehanshu
Comment Utility
inethog,
  I am glad I could help.

EddieShipman,
  I think what inethog had done was THE way to create components at runtime: Atleast Marco Cantu says so in Mastering Delphi :-) I think the form frees the components it owns on destroy. How else could you create more than one TImage dynamically?
...Snehanshu
0
 
LVL 5

Expert Comment

by:snehanshu
Comment Utility
P.S.
EddieShipman,
  I may be wrong, so please do let me know.
Cheers!
...Snehanshu
0
 
LVL 26

Expert Comment

by:EddieShipman
Comment Utility
I am sad that you chose the answer you did. I have been programming Delphi since  and
the way I showed is the way it is supposed to be done.

The way I showed makes SURE that there is no memory leak.

0
 
LVL 5

Expert Comment

by:snehanshu
Comment Utility
EddieShipman,

What do you say about this code?


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, extctrls,
  StdCtrls;

type
  TDesImg = class(TImage)
  destructor destroy; override;
  end;

  TfrmMain = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.DFM}

procedure TfrmMain.Button1Click(Sender: TObject);
procedure DrawCard();
var
  Card : TDesImg;

begin

  Card := TDesImg.Create(frmMain);
//  Card.Picture.Bitmap.LoadFromResourceName(hInstance, 'CARD1');
  Card.Picture.Bitmap.LoadFromFile('D:\SHU\GAMES\cricketscore253.bmp');
  with Card do begin
    Left := 120;
    Top := 120;
    Width := 71;
    Height := 96;
    Parent := frmMain;
    Visible := True;
  end;
end;
begin
  DrawCard
end;

{ TDesImg }
//If the object is automatically destroyed, the file will be created
destructor TDesImg.destroy;
var
  MyLst: TStringList;
begin
  Mylst := TStringList.Create;
  If fileExists('C:\Destroyed.txt') then
    MyLst.LoadFromFile('C:\Destroyed.txt');
  MyLst.Add('IAmDestroyed');

  MyLst.SaveToFile('C:\Destroyed.txt');
  MyLst.Free;
  inherited;
end;

end.
0
 
LVL 5

Expert Comment

by:snehanshu
Comment Utility
From Delphi Help Files:

TComponent.Owner

Indicates the component that is responsible for freeing this component.

property Owner: TComponent;


Description

Use Owner to find the owner of a component. When one component owns another, the memory for the owned component is freed when its owner's memory is freed. This means that when a form is destroyed, all the components on the form are also destroyed.

By default, a form owns all components that are on it. In turn, the form is owned by the application. Thus when the application shuts down and its memory is freed, the memory for all forms (and all their owned components) is also freed.

The owner of a component is determined by the parameter passed to the constructor when the component is created. For components created in the form designer, the form is automatically assigned as the owner.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

10 Experts available now in Live!

Get 1:1 Help Now