?
Solved

Custom Component - Where's the canvas?

Posted on 2006-10-31
6
Medium Priority
?
222 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
Technology Partners: 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

752 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