Creating Controls at Runtime in Delphi 6

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.
LVL 1
inethogAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Eddie ShipmanAll-around developerCommented:
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;
Eddie ShipmanAll-around developerCommented:
Another thing was that you weren't assigning the bitmap to the TImage.
snehanshuCommented:
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

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
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

inethogAuthor Commented:
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.


Eddie ShipmanAll-around developerCommented:
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;
Eddie ShipmanAll-around developerCommented:
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.
inethogAuthor Commented:
Thanks alot to everyone who posted here :)

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

Cheers,

D.
snehanshuCommented:
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
snehanshuCommented:
P.S.
EddieShipman,
  I may be wrong, so please do let me know.
Cheers!
...Snehanshu
Eddie ShipmanAll-around developerCommented:
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.

snehanshuCommented:
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.
snehanshuCommented:
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.
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.