Solved

Why can't I draw an outline around my TLabel?

Posted on 2004-10-03
6
330 Views
Last Modified: 2010-04-05
Hi,
Take a look at the code below.

The code in the Shape1DragDrop event handler FAILS to create a dotted outline around the TLabel which is  created firstly in this procedure.

BUT....the same two lines of code dropped into the Button1Click event handler below DOES create the outline around the same label!

Any ideas why this is??
I don't want the user to have to click a button to draw the outline around this label :)

Thanks

procedure TForm1.Shape1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
if Source is TListBox then
begin
  l := TLabel.Create(Self);
  l.Left := X;
  l.Top := Y;
  l.Parent := Panel1;
  l.Caption := ListBox1.Items[ListBox1.ItemIndex];
  l.Canvas.Pen.Style := psDot;
  l.Canvas.Rectangle(0,0,l.Width,l.Height);
  MyArray[1] := l;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  l.Canvas.Pen.Style := psDot;
  l.Canvas.Rectangle(0,0,l.Width,l.Height);
end
0
Comment
Question by:nathc
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:mottor
ID: 12211113
Because changing caption requires repaint:
...
.......
    l.Caption := ListBox1.Items[ListBox1.ItemIndex];
    l.Canvas.Pen.Style := psDot;
    Panel1.Repaint;                                     //  Look here
    l.Canvas.Rectangle(0,0,l.Width,l.Height);
....
0
 

Author Comment

by:nathc
ID: 12211160
Thanks mottor, that worked but raised a new issue.
The first label dropped onto Shape1 works fine but when I drop the second one on the dashed border around the first label disappears.
Is this something to do with the repaint??
0
 
LVL 6

Expert Comment

by:Jaymol
ID: 12211258
The repaint will only do what it thinks it should do, and that is to redraw every object as it should be.  If you have made any manual changes (eg. drawing directly onto the canvas) then these changes will be lost as they are not actually part of the object.

You will need to redraw any manual canvas changes after every repaint.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 4

Accepted Solution

by:
mottor earned 125 total points
ID: 12211330
Add this to your code and use TMyLabel instead of TLabel :

type TMyLabel = class(TLabel)
     protected
       procedure Paint; override;
     end;
procedure TMyLabel.Paint;
begin
  inherited;
  Canvas.Pen.Style := psDot;
  Canvas.Rectangle(0,0,Width,Height);
end;

0
 

Author Comment

by:nathc
ID: 12213964
Hey Mottor,

I get this error
[Error] LabelCreate.pas(42): Identifier redeclared: 'TMyLabel.Paint'
[Error] LabelCreate.pas(43): This form of method call only allowed in methods of derived types

When I try the code below.

type TMyLabel = class(TLabel)
     protected
       procedure Paint; override;
     end;
-->procedure TMyLabel.Paint; <--- This line!!
begin
  inherited;
  Canvas.Pen.Style := psDot;
  Canvas.Rectangle(0,0,Width,Height);
end;

Any ideas??
0
 

Author Comment

by:nathc
ID: 12214249
Sorry mottor, got it working. Just shifted the procedure bit elsewhere thanks very much :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 316
Help on project with Soap 10 57
LAN or WAN ? 11 95
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 70
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

808 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