nathc
asked on
Why can't I draw an outline around my TLabel?
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(Send er, 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.It emIndex];
l.Canvas.Pen.Style := psDot;
l.Canvas.Rectangle(0,0,l.W idth,l.Hei ght);
MyArray[1] := l;
end;
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
l.Canvas.Pen.Style := psDot;
l.Canvas.Rectangle(0,0,l.W idth,l.Hei ght);
end
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(Send
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.It
l.Canvas.Pen.Style := psDot;
l.Canvas.Rectangle(0,0,l.W
MyArray[1] := l;
end;
end;
procedure TForm1.Button1Click(Sender
begin
l.Canvas.Pen.Style := psDot;
l.Canvas.Rectangle(0,0,l.W
end
ASKER
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??
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??
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.
You will need to redraw any manual canvas changes after every repaint.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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??
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
end;
Any ideas??
ASKER
Sorry mottor, got it working. Just shifted the procedure bit elsewhere thanks very much :)
...
.......
l.Caption := ListBox1.Items[ListBox1.It
l.Canvas.Pen.Style := psDot;
Panel1.Repaint; // Look here
l.Canvas.Rectangle(0,0,l.W
....