Solved

Creating Controls at Runtime in Delphi 6

Posted on 2003-10-30
12
311 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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 this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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