创建复合控件是否明智? [英] Is it wise to create composite controls?

查看:193
本文介绍了创建复合控件是否明智?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个应用程序在一些地方重用一种成语。有一个TPanel,它是一些标签和按钮。目的是允许用户选择日期范围。





& Dates标题是一个标签,所有日期部分是第二个。当用户点击选择按钮时,弹出一个表单,向用户显示一对日期/时间控件和确定/取消按钮。如果用户在选择某些日期后点击确定,则第二个标签将更改为从mm / dd / yyyy到mm / dd / yyyy。



创建打包这些控件的组件?我一直在寻找组件作者的各种资源,而且他们似乎没有指出我正在考虑的问题,比如处理按钮的onclick事件。如果这是一个合理的尝试,我还要指出如何做出这样一个复合控件的说明。

解决方案

这是合理的,是的。



要创建这样一个组件,只需从TCustomPanel派生一个新类,并将子组件添加到类中



像这样:

  TMyDatePicker = class(TCustomPanel)
protected
FChooseButton:TButton;
FClearButton:TButton;
public
构造函数Create(Owner:TComponent);覆盖
结束

构造函数TMyDatePicker.Create(Owner:TComponent)
begin
//继承
Inherited;

//创建选择按钮
FChooseButton:= TButton.Create(Self);
FChooseButton.Parent:=自我;
FChooseButton.Align:= alRight;
FChooseButton.Caption:='选择';

//创建清除按钮
FClearButton:= TButton.Create(Self);
FClearButton.Parent:=自我;
FClearButton.Align:= alRight;
FClearButton.Caption:='清除';
结束

要添加事件处理程序,只需向您的课堂添加新的受保护的过程。



例如:

 程序TMyDatePicker.HandleChooseButtonClick(发件人:TObject)
begin
//点击选择按钮时,做任何你想做的事情
end;

然后将事件处理程序连接到选择按钮的OnClick事件(这应该在Create该类的方法):

  FChooseButton.OnClick:= HandleChooseButtonClick; 

当然还有一点,比如微调按钮对齐并添加图标。此外,您还需要创建自己的事件,例如OnDateSelected或OnDateModified。



但除此之外,我认为上述示例至少应该让你走。 :)


I have this application that reuses a sort of idiom in a number of places. There's a TPanel, and on it are some labels and buttons. The purpose is to allow the user to select a date range.

The "&Dates" caption is one label, and the "All Dates" part is a second one. When the user clicks the "Choose" button a form pops up presenting the user with a pair of Date/Time controls and OK/Cancel buttons. If the user hits OK after selecting some dates, the 2nd label changes to "From mm/dd/yyyy To mm/dd/yyyy".

Is it plausible to create a component that packages up these controls? I have been looking at various resources for component writers and they don't seem to be pointed at the problems I am thinking about, such as handling the onclick events for the buttons. If this is a reasonable thing to attempt, I'd also appreciate pointers to descriptions of how to make such a "composite control."

解决方案

It's reasonable, yes.

To create such a component, just derive a new class from for instance TCustomPanel, and add the sub-components as fields within the class.

Like this:

TMyDatePicker = class(TCustomPanel)
protected
  FChooseButton: TButton;
  FClearButton: TButton;
public
  constructor Create(Owner: TComponent); override; 
end;

constructor TMyDatePicker.Create(Owner: TComponent)
begin
  // Inherited
  Inherited;

  // Create Choose Button
  FChooseButton := TButton.Create(Self);
  FChooseButton.Parent := Self;
  FChooseButton.Align := alRight;
  FChooseButton.Caption := 'Choose';

  // Create Clear Button
  FClearButton := TButton.Create(Self);
  FClearButton.Parent := Self;
  FClearButton.Align := alRight;
  FClearButton.Caption := 'Clear';
end;

To add event handers, just add new protected procedures to your class.

For instance:

procedure TMyDatePicker.HandleChooseButtonClick(Sender: TObject)
begin
  // Do whatever you want to do when the choose button is clicked
end;

Then connect the event handler to the OnClick event of the choose button (this should be done within the Create method of the class):

FChooseButton.OnClick := HandleChooseButtonClick;

There's a bit more to it than this, of course, such as fine tuning the alignments of the buttons and adding icons. Also you'll need to create your own events, such as OnDateSelected or OnDateModified.

But, apart from that, I think the above example should at least get you going. :)

这篇关于创建复合控件是否明智?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆