Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Custom Component - Where's the canvas?

Posted on 2006-10-31
6
Medium Priority
?
223 Views
Last Modified: 2010-04-05
Hi,

I'm creating a new custom components (TCustomControl decendent) in BDS2006 (.Net for VCL).  I have a procedure which is called with the components caption is changed.  This procedure needs the Canvas to be available as it uses the TextHeight and TextWidth to work out the co-ordinates for the new caption.

The problem is, when I first drop the component onto a form, I get an exception because the canvas isn't available.  Here's the order that things are done:

1. Drop component on form
2. Create method called
3. Caption is set to a default
4. Event triggered for set_Caption
5. ProcessCaption procedure is called
exception raised

I'd be hoping that there'd be a:
6. Control is painted.

What have I got to do to be a ble to get the canvas to be available to the control in more places than just the Paint procedure?  Do I need to declare it as a property or something?

Cheers!

Pag.
0
Comment
Question by:PAG_Promax
[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
  • 3
  • 3
6 Comments
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 17855669
add the following to your custom control:

...
public
  property Canvas;
0
 

Author Comment

by:PAG_Promax
ID: 17855910
Thanks for that.  Amazing how simple things can be :)
0
 

Author Comment

by:PAG_Promax
ID: 17855922
Oops.  Take that back.  It's giving me :

---------------------------
bds
---------------------------
Control '' has no parent window.
---------------------------
OK  
---------------------------


All I am doing is setting the width of the control to the Canvas.TextWidth('Test') in the OnCreate method (right at the end of the procedure).

Any ideas?

Thanks,

Pag.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 1000 total points
ID: 17856868
You need to set the parent property of your control to a valid TWinControl descendant. Something like this:

unit Unit1;

interface

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

type
  TMyControl = class(TCustomControl)
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
    property Canvas;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  with TMyControl.Create(self) do
  begin
    left:= 10;
    Top:= 10;
    Width:= 50;
    Height:= 50;
  end;
end;

{ TMyControl }

constructor TMyControl.Create(AOwner: TComponent);
begin
  inherited;
  if AOwner is TWinControl
    then Parent:= TWinControl(AOwner);
end;

procedure TMyControl.Paint;
begin
  Canvas.FillRect(ClientRect);
end;

end.
0
 

Author Comment

by:PAG_Promax
ID: 17862816
My component is actually a  TGraphicControl descendent.  Is that a problem?

I've tried what you suggested, and the error message has gone, so I'm assuming it's working properly.

It makes sense what you've suggested to.  So thanks for that.  The points are yours!!

Cheers

Pag
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 17864209
It's fine if your control descends from TGraphicControl. My previous comment related to the parent control. The parent control must descend from TWinControl.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

618 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