简体   繁体   English

Delphi自定义控件:一个带有TLabel的TRichEdit

[英]A Delphi Custom Control: A TRichEdit with a TLabel Above It

I want to create an custom control (descendant of TRichEdit). 我想创建一个自定义控件(TRichEdit的后代)。 I simply want add some text above the editfield. 我只想在editfield上面添加一些文字。

I've created my own control and I override the constructor to create a TLabel for the caption. 我已经创建了自己的控件,并覆盖了构造函数,为标题创建了一个TLabel。 It works, but my problem: How is it possible to move the label above the richedit? 它有效,但我的问题是:如何将标签移到richedit上面? When I set Top := -5 the label begins to disappaer. 当我设置Top:= -5时,标签开始变得令人失望。

Here's the code of the constructor: 这是构造函数的代码:

constructor TDBRichEditExt.Create(AOwner: TComponent);
begin
  inherited;
  lblCaption := TLabel.Create(self);
  lblCaption.Parent := parent;
  lblCaption.Caption := 'Header';
  lblCaption.Top := -5;
end;

I think it's logic that the label disappaers since the richedit is the parent. 我认为标签令人失望,因为richedit是父母。 I've tried 我试过了

lblCaption.Parent := self.parent;

To make the form which owns the richedit the parent - but this dosn't work... 为了使拥有richedit的表格成为父母 - 但这不起作用......

How could I achieve this? 我怎么能实现这个目标? Thank you all! 谢谢你们!

I think it's logic that the label disappaers since the richedit is the parent 我认为标签令人失望,因为richedit是父母

This is wrong. 这是错的。 In your code, the parent of the TLabel is the parent of the TDBRichEditExt , as it should be. 在你的代码中,母公司TLabel是的父TDBRichEditExt ,因为它应该是。 Notice that, in a method of TDBRichEditExt , Parent and Self.Parent is the same thing. 请注意,在方法TDBRichEditExtParentSelf.Parent是同样的事情。 If you would like the parent of the TLabel to be the TDBRichEditExt itself - which you do not - then you should set lblCaption.Parent := self; 如果您希望的父母TLabelTDBRichEditExt本身-你 -那么你应该设置lblCaption.Parent := self; .

Now, if the parent of the TLabel is the parent of the TDBRichEditExt , then the Top property of the TLabel refers to the parent of TDBRichEditExt , not to the TDBRichEditExt itself. 现在,如果母公司TLabel是的父TDBRichEditExt ,然后将Top的财产TLabel指父TDBRichEditExt ,不以TDBRichEditExt本身。 Hence, if the parent of the TDBRichEditExt is a TForm , then Top := -5 means that the TLabel will be positioned five pixels above the form's upper edge. 因此,如果的父TDBRichEditExtTForm ,则Top := -5TLabel将被定位的形式的上边缘的五个像素的上方 You mean 你的意思是

lblCaption.Top := Self.Top - 5;

But -5 is a far too small number. 但-5是一个太小的数字。 What you really should use is 你真正应该使用的是

lblCaption.Top := Self.Top - lblCaption.Height - 5;

which in addition makes a 5 px space between the label and the Rich Edit. 此外,标签和Rich Edit之间的间距为5 px。

Also, you would like 另外,你想

lblCaption.Left := Self.Left;

Another issue 另一个问题

But this will not work, because at the time the component is created, I do not think that the Parent has been set yet. 但这不起作用,因为在创建组件时,我认为尚未设置父组件。 So what you will need is to do the positioning of the label at a more appropriate time. 因此,您需要的是在更合适的时间进行标签的定位。 In addition, this will move the label each time your component is moved, which is very important! 此外,每次移动组件时都会移动标签,这非常重要!

TDBRichEditExt = class(TRichEdit)
private
  FLabel: TLabel;
  FLabelCaption: string;
  procedure SetLabelCaption(LabelCaption: string);
public
  constructor Create(AOwner: TComponent); override;
  procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer); override;
published
  LabelCaption: string read FLabelCaption write SetLabelCaption;
end;

procedure TDBRichEditExt.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
  inherited;
  if not assigned(Parent) then
    Exit;
  FLabel.Parent := self.Parent;
  FLabel.Top := self.Top - FLabel.Height - 5;
  FLabel.Left := self.Left;
end;

Details 细节

In addition, when you hide the TDBRichEditExt , you want to hide the label as well. 此外,当您隐藏TDBRichEditExt ,您也想要隐藏标签。 Thus you need 因此你需要

protected
  procedure CMVisiblechanged(var Message: TMessage); message CM_VISIBLECHANGED;

where 哪里

procedure TDBRichEditExt.CMVisiblechanged(var Message: TMessage);
begin
  inherited;
  if assigned(FLabel) then
    FLabel.Visible := Visible;
end;

And similarly for the Enabled property, and you also need to update the parent of the TLabel each time the parent of the TDBRichEditExt is changed: 同样地,对于在Enabled属性,并且还需要更新的父TLabel每个父时间TDBRichEditExt改变:

protected
  procedure SetParent(AParent: TWinControl); override;

with

procedure TDBRichEditExt.SetParent(AParent: TWinControl);
begin
  inherited;
  if not assigned(FLabel) then Exit;
  FLabel.Parent := AParent;
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM