Answer the question
In order to leave comments, you need to log in
Why is the line not drawn from the right?
I work in Delphi 7.
I create labels, draw lines between them, but for some reason a line is not drawn to the right of the left label.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
p,c,z,j:integer;
function rez(p:integer): integer;
function toside(j:integer): integer;
var MyLabel,MyLabel2:TLabel;
implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
rez(p);
p:=c;
toside(j);
j:=z;
MyLabel := TLabel.Create(Form1);
MyLabel.Parent := Form1;
MyLabel.Left:=300 ;
MyLabel.Top:=Mylabel.Top+c;
MyLabel.Caption:='1';
MyLabel2 := TLabel.Create(Form1);
MyLabel2.Parent := Form1;
MyLabel2.Left:=MyLabel.Left+z ;
MyLabel2.Top:=Mylabel.Top;
MyLabel2.Caption:='3';
with Form1.Canvas do
begin
Pen.Color:=clRed;
MoveTo(MyLabel.left,MyLabel.top+(MyLabel.Font.Size+MyLabel.Font.Size div 2) div 2);
LineTo(MyLabel2.left,MyLabel2.top+(MyLabel2.Font.Size+MyLabel2.Font.Size div 2) div 2);
end;
end;
function rez(p:integer): integer;
begin
c:=p+20;
end;
function toside(j:integer): integer;
begin
z:=j+38;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
j:=11;
end;
end.
Answer the question
In order to leave comments, you need to log in
Would you attach a screenshot or something :) I
will assume that the line is drawn to the Label's border, which is not visible, but is. And you probably need to pull it closer to the text (number "1").
Try playing around with the Width property (maybe also AutoSize).
UPD2. Remember already, drawing on the form should be done only in OnPaint. Minimize-expand the window - the line will disappear The code will be something like this.
TForm1 = class(TForm)
...
private
MyLabel,MyLabel2:TLabel; // тут они занулятся автоматически, да и фэншуйнее
...
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
...
MyLabel2.Caption:='3';
Invalidate; // Запросить перерисовку
end;
procedure TForm1.FormPaint(Sender: TObject); // Событие OnPaint
begin
if MyLabel <> nil then
with Canvas do begin
Pen.Color:=clRed;
MoveTo(MyLabel.left,MyLabel.top+(MyLabel.Font.Size+MyLabel.Font.Size div 2) div 2);
LineTo(MyLabel2.left,MyLabel2.top+(MyLabel2.Font.Size+MyLabel2.Font.Size div 2) div 2);
end;
end;
MyLabel := TLabel.Create(Form1);
MyLabel.Parent := Form1;
It's better to use Self rather than Form1.Form1: TForm1;
p,c,z,j:integer;
If these variables were needed, it would be better to add them to Form1. If…function rez(p:integer): integer;
begin
c:=p+20;
end;
We here, obviously, calculate the dimensions of our inscriptions. Can't this be done without side effects? Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question