如何在Delphi中创建和实例化用户定义的组件?

如何在Delphi中创建和实例化用户定义的组件?,delphi,delphi-7,Delphi,Delphi 7,我想为我的应用程序创建一个搜索框。搜索框将包括两个内容:搜索字段和搜索按钮。我认为,正确的做法是将这两个组件放在一个组合箱中,作为一个容器将它们放在里面。我想我需要创建一个派生自TGroupBox类的类,该类在创建时将接收一个表名作为要搜索的参数。搜索框和按钮这两个组件将成为它的子组件——这就是它将如何工作的基本原则 此图显示了搜索框的外观: 以下是我迄今为止所做的工作: unit clsTSearchBox; interface uses Classes, SysUtils, S

我想为我的应用程序创建一个搜索框。搜索框将包括两个内容:搜索字段和搜索按钮。我认为,正确的做法是将这两个组件放在一个组合箱中,作为一个容器将它们放在里面。我想我需要创建一个派生自TGroupBox类的类,该类在创建时将接收一个表名作为要搜索的参数。搜索框和按钮这两个组件将成为它的子组件——这就是它将如何工作的基本原则

此图显示了搜索框的外观:

以下是我迄今为止所做的工作:

unit clsTSearchBox;

interface

    uses Classes, SysUtils, StdCtrls, Dialogs, ADODB, DataModule;

    type
        TSearchBox = class (TGroupBox)
            constructor CreateNew(AOwner: TObject; Dummy: Integer);     
        end;

implementation

    constructor TSearchBox.CreateNew(AOwner: TObject; Dummy: Integer);
    begin
        inherited;
        Self.Height  := 200;
        Self.Width   := 400;
        Self.Caption := 'Test:'
    end;

end.
正如你所看到的,不多。我刚刚创建了一个从TGroupBox类派生的类。请帮助我编写适当的代码,在我的表单上实例化搜索框组件,因为我真的不知道怎么做。我只需要正确创建对象的代码


提前感谢大家。

您的分组框希望如下所示:

type
  TSearchBox = class(TGroupBox)
  private
    FSearchEdit: TEdit;
    FFindButton: TButton;
  public
    constructor Create(AOwner: TComponent); override;
  end;
constructor TSearchBox.Create(AOwner: TComponent);
begin
  inherited;
  FSearchEdit := TEdit.Create(Self);
  FSearchEdit.Parent := Self;
  FSearchEdit.SetBounds(...);//set position and size here
  FFindButton := TButton.Create(Self);
  FFindButton.Parent := Self;
  FFindButton.SetBounds(...);//set position and size here
end;
FSearchBox := TSearchBox.Create(Self);
FSearchBox.Parent := BookTabSheet;
FSearchBox.SetBounds(...);//set position and size here
对于
t组件
子代,通常应覆盖名为
Create
的虚拟构造函数。这将允许您的组件被流框架实例化

实现如下所示:

type
  TSearchBox = class(TGroupBox)
  private
    FSearchEdit: TEdit;
    FFindButton: TButton;
  public
    constructor Create(AOwner: TComponent); override;
  end;
constructor TSearchBox.Create(AOwner: TComponent);
begin
  inherited;
  FSearchEdit := TEdit.Create(Self);
  FSearchEdit.Parent := Self;
  FSearchEdit.SetBounds(...);//set position and size here
  FFindButton := TButton.Create(Self);
  FFindButton.Parent := Self;
  FFindButton.SetBounds(...);//set position and size here
end;
FSearchBox := TSearchBox.Create(Self);
FSearchBox.Parent := BookTabSheet;
FSearchBox.SetBounds(...);//set position and size here
最重要的一课可能是,必须设置动态创建控件的
Parent
属性。这是强制基础窗口的父/子关系所必需的

要在运行时创建其中一个,请按如下方式编写代码:

type
  TSearchBox = class(TGroupBox)
  private
    FSearchEdit: TEdit;
    FFindButton: TButton;
  public
    constructor Create(AOwner: TComponent); override;
  end;
constructor TSearchBox.Create(AOwner: TComponent);
begin
  inherited;
  FSearchEdit := TEdit.Create(Self);
  FSearchEdit.Parent := Self;
  FSearchEdit.SetBounds(...);//set position and size here
  FFindButton := TButton.Create(Self);
  FFindButton.Parent := Self;
  FFindButton.SetBounds(...);//set position and size here
end;
FSearchBox := TSearchBox.Create(Self);
FSearchBox.Parent := BookTabSheet;
FSearchBox.SetBounds(...);//set position and size here
此代码将在窗体的构造函数中运行,或在
OnCreate
事件中运行


我相信您现在已经了解了基本的想法。

您的分组框应该是这样的:

type
  TSearchBox = class(TGroupBox)
  private
    FSearchEdit: TEdit;
    FFindButton: TButton;
  public
    constructor Create(AOwner: TComponent); override;
  end;
constructor TSearchBox.Create(AOwner: TComponent);
begin
  inherited;
  FSearchEdit := TEdit.Create(Self);
  FSearchEdit.Parent := Self;
  FSearchEdit.SetBounds(...);//set position and size here
  FFindButton := TButton.Create(Self);
  FFindButton.Parent := Self;
  FFindButton.SetBounds(...);//set position and size here
end;
FSearchBox := TSearchBox.Create(Self);
FSearchBox.Parent := BookTabSheet;
FSearchBox.SetBounds(...);//set position and size here
对于
t组件
子代,通常应覆盖名为
Create
的虚拟构造函数。这将允许您的组件被流框架实例化

实现如下所示:

type
  TSearchBox = class(TGroupBox)
  private
    FSearchEdit: TEdit;
    FFindButton: TButton;
  public
    constructor Create(AOwner: TComponent); override;
  end;
constructor TSearchBox.Create(AOwner: TComponent);
begin
  inherited;
  FSearchEdit := TEdit.Create(Self);
  FSearchEdit.Parent := Self;
  FSearchEdit.SetBounds(...);//set position and size here
  FFindButton := TButton.Create(Self);
  FFindButton.Parent := Self;
  FFindButton.SetBounds(...);//set position and size here
end;
FSearchBox := TSearchBox.Create(Self);
FSearchBox.Parent := BookTabSheet;
FSearchBox.SetBounds(...);//set position and size here
最重要的一课可能是,必须设置动态创建控件的
Parent
属性。这是强制基础窗口的父/子关系所必需的

要在运行时创建其中一个,请按如下方式编写代码:

type
  TSearchBox = class(TGroupBox)
  private
    FSearchEdit: TEdit;
    FFindButton: TButton;
  public
    constructor Create(AOwner: TComponent); override;
  end;
constructor TSearchBox.Create(AOwner: TComponent);
begin
  inherited;
  FSearchEdit := TEdit.Create(Self);
  FSearchEdit.Parent := Self;
  FSearchEdit.SetBounds(...);//set position and size here
  FFindButton := TButton.Create(Self);
  FFindButton.Parent := Self;
  FFindButton.SetBounds(...);//set position and size here
end;
FSearchBox := TSearchBox.Create(Self);
FSearchBox.Parent := BookTabSheet;
FSearchBox.SetBounds(...);//set position and size here
此代码将在窗体的构造函数中运行,或在
OnCreate
事件中运行


我相信您现在已经了解了基本概念。

如果您将所有3个组件都放在一个TFrame中,在控件上添加所需的任何代码,然后实例化一个框架实例,听起来可能会更容易


然后,框架中包含groupbox、edit和按钮。您只需使用TYourFrame创建框架。在设计时创建或执行它。

如果您将所有3个组件放在一个TFrame中,在控件上添加所需的任何代码,然后实例化框架的实例,听起来可能会更容易


然后,框架中包含groupbox、edit和按钮。您只需使用TYourFrame创建框架。在设计时创建或执行它。

我理解这一点,但如何从表单上的TSearchBox类创建对象?你能帮我吗?因为我不知道该怎么做。我尝试过,但由于代码中的错误,它无法编译。虽然在我看来我写的是正确的。我的答案的最后部分涵盖了这一点。@MikhailRybkin你只需像其他人一样把它写在表格上就行了component@Arioch'仅当您将组件安装到IDE中时。否则,您必须在代码中创建它。我写了FSearchBox:TSearchBox;在窗体的对象和变量声明部分。一切似乎都正常,但当我试图保存项目时,Delphi显示了这样一条消息:“Field BookManager.FSearchBox没有相应的组件。请删除声明?”我理解这一点,但我仍然知道如何从表单上的TSearchBox类创建对象?你能帮我吗?因为我不知道该怎么做。我尝试过,但由于代码中的错误,它无法编译。虽然在我看来我写的是正确的。我的答案的最后部分涵盖了这一点。@MikhailRybkin你只需像其他人一样把它写在表格上就行了component@Arioch'仅当您将组件安装到IDE中时。否则,您必须在代码中创建它。我写了FSearchBox:TSearchBox;在窗体的对象和变量声明部分。一切似乎都正常,但当我尝试保存项目时,Delphi显示以下消息:“Field BookManager.FSearchBox没有相应的组件。是否删除声明?”尝试查找
自定义容器包
。如果它能在D7上工作-您只需使用groupbox、edit和button创建一个常规表单,然后将其编译成新组件一般提示:
GExperts
CnWizards
都有组件到代码的操作。将任何组件拖放到表单上,然后运行该操作,您将获得该组件的创建代码。学习它,如果您喜欢,也可以对您的组件进行同样的操作。尝试查找
自定义容器包
。如果它能在D7上工作-您只需使用groupbox、edit和button创建一个常规表单,然后将其编译成新组件一般提示:
GExperts
CnWizards
都有组件到代码的操作。将任何组件拖放到表单上,然后运行该操作,您将获得该组件的创建代码。如果您喜欢,请学习它并对您的组件执行相同的操作。