Solved

Creating Controls at Runtime in Delphi 6

Posted on 2003-10-30
12
318 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
[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
  • 5
  • 5
  • 2
12 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9655194
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
ID: 9655195
Another thing was that you weren't assigning the bitmap to the TImage.
0
 
LVL 5

Accepted Solution

by:
snehanshu earned 200 total points
ID: 9655206
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
Industry Leaders: 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!

 
LVL 1

Author Comment

by:inethog
ID: 9655301
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
ID: 9655310
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
ID: 9655316
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
 
LVL 1

Author Comment

by:inethog
ID: 9655346
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
ID: 9655396
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
ID: 9655420
P.S.
EddieShipman,
  I may be wrong, so please do let me know.
Cheers!
...Snehanshu
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9657515
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
ID: 9659239
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
ID: 9659284
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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