通过在Delphi VCL中组合两个控件(TEdit和TTrackBar)来创建新组件 [英] Creating a new component by combining two controls (TEdit and TTrackBar) in Delphi VCL

查看:396
本文介绍了通过在Delphi VCL中组合两个控件(TEdit和TTrackBar)来创建新组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Windows开发Delphi 10.1 VCL应用程序。



对于整数或浮点输入,我需要一个与滑块连接的数字输入字段。当用户更改输入字段中的数字时,滑块位置也会相应更改。当用户更改滑块位置时,数字字段中的数字将更新。



我可以使用 TEdit TTrackBar ,并在其 OnChange 事件处理程序中添加必要的更新功能。





问题是我需要以不同形式输入许多此类输入。因此,我想创建一个新组件,将两个控件 TEdit TTrackBar 组合在一个组件中。


  1. 创建新组件是否是多次使用此类滑块输入的最佳策略?


  2. 创建这种新组件的最佳方法是什么?



解决方案


创建新组件是否是多次使用此类滑块输入的
的最佳策略?


不一定总是正确的。 (至少按照我的标准)。


创建这样一个新组件的最佳方法是什么?


我知道三种解决您问题的方法。



方法1:



使用新组件向导创建组件,您可以在其中动态创建 Tedit Tgroupbox 后代中的Ttrackbar 子组件。



以下是我的操作方式那。

  unit Combindedittrack; 

接口

使用
System.SysUtils,System.Classes,Vcl.Controls,Vcl.comctrls,Vcl.StdCtrls;

类型
TCombindedittrack = class(Tgroupbox)
私人
{私人声明}
Fedit:tedit;
Ftrackbar:TTrackBar;
过程editonchangeproc(Sender:TObject);
过程trackbaroOnchange(Sender:TObject);
protected
{受保护的声明}

public
{公共声明}
构造函数Create(owner:tcomponent);覆盖
析构函数销毁;覆盖
已发布
{已发布的声明}
结尾;

程序注册;

实施

程序注册;
开始
RegisterComponents('Samples',[TCombindedittrack]);
结尾;

构造函数TCombindedittrack.Create(owner:tcomponent);
开始
继承Create(owner);
SetBounds(0,0,250,50);
Fedit:= tedit.Create(self);
和Fedit一起做
开头
文本:=’’; //<-在这里控制外观
top:= 10;
还剩:= 10;
height:= 27;
width:= 50;
parent:= self;
Onchange:= editonchangeproc; //您为Tedit
结尾的Onchange事件处理程序;

Ftrackbar:= ttrackbar.Create(self);
和Ftrackbar一起做
开始
top:= 10; //<-在这里控制外观
:= 60;
高度:= 30;
width:= 50;
parent:= self;
Onchange:= trackbaronchangeproc; //您为Ttrackbar
结尾的Onchange事件处理程序;
结尾;

析构函数TCombindedittrack.Destroy;
开始
Ftrackbar。
Fedit。免费;
继承了;
结尾;

过程TCombindedittrack.trackbaroOnchange(Sender:TObject);
begin
// //-在此处跟踪栏onchange处理。
结尾;

程序TCombindedittrack.editonchangeproc(Sender:TObject);
开始
//<-在此处编辑onchange处理。
结尾;

结尾。

方法2:



使用像这样的框架(我在delphi 10西雅图)。



1)文件->新建->其他->(搜索



2)现在添加编辑和轨迹栏并设置其Onchange事件。



3)保存单位。



4)在工具面板(标准组件部分)上,单击框架组件。



5)选择您刚刚创建的框架。



每次使用该框架时,您都会得到一个副本。



方法3:



使用这样的组件模板(同样,我在delphi 10西雅图)



1)选择已创建并修改的 tedit ttrackbar



2)在工具栏的组件上,单击创建组件模板。



3 )命名您的模板,然后按OK。



4)选择模板为浅色tte,然后是模板。



现在请注意,甚至将代码(事件)也添加到了项目中。



最后



在delphi和IDE方面,我真的无法为您提供明确的答案 b
$ b




编辑:




编辑: strong>由于许多评论都坚持认为答案应说明这样做的最佳方法。这是基于以下情况的最佳方法。



让我们提出一些在选择时应考虑的关键点



1。。如果愿意,可以轻松修改组合控件(以我的经验为准)。



2。。完成此任务所需的时间(这意味着
您将花费最少的调试和编码来完全完成任务的时间)。



3。。一般的源代码可读性。



4。对项目的未来有用。



现在让我们开始基于这些标准来批评这三种方法。



方法1:



C1 (条件编号1):只需修改组件的源实现,每个副本/使用都将具有相同的效果和属性。但是,方法3并非如此。



C2:这取决于您对组件编写的了解,但是对于这个组件,我却花了我很多时间5分钟创建它,我只是delphi的初学者。对于调试,如果出现问题并且组件实现存在问题,那么您只需要修复一次(请参见C1)



C3:在您的组件的表单源代码中,它们是没有实现的,只需将其添加到表单中,然后所有内容都被隐藏(例如,添加 tedit 并查看在您的表单源中实施)。



C4:您正在创建一个组件,这将为您打开创建自己的一组组件(如Flatstyle或Indy)打开大门开源组件。因此,下次您需要类似这样的东西时,只需将其放入表单设计器即可。



方法2:框架



C1:这类似于方法1,因为您正在创建组件,但是这次是直观的。修改源框架将更改副本的效果和属性,也可以为副本添加额外的处理。



副本的Onchange事件的事件处理程序是这样的

 过程TForm1.Frame2Edit1Change(Sender:TObject); 
开始
Frame2.Edit1Change(Sender); //<-这是您在
开头设置的原始onchange事件//<-如果希望其中一个副本的行为与原始
结束不同,则可以在此处进行额外的处理;

C2:同时,也许比方法1更快。 p>

C3:总的来说,它与方法1相同。



C4:与第一种方法不同,您不能使用在项目B中的项目A中创建的框架。因此,您的编码和调试将保留在项目A中。



第3种方式:组件模板。



C1:您不是要创建的组件您在上一个项目中执行的确切步骤的补充/宏。



C2:同时并且可能比方式1更快。



C3::每次向表单中添加模板时,都会添加事件代码(如果是较长的Onchange代码,则不是很好的视图)。



C4:您可以在项目B中使用在项目A中创建的模板。但是,在项目A中编写的内容将在项目B中(请参阅c1)甚至项目B中不存在的变量引用(考虑到每次使用模板之间的时间间隔,这都很难调试和误导)。



结论:提出的每种方式都会消耗时间进行编码和调试,并且所有这些方式都将完成任务,但是,为什么?简单性和重用性和最低风险的解决方案在这里,方法1是安全的选择,因为它使您有机会安全地进行更新和升级。



第一种方式的好处还在于,过一会儿您将忘记实现以及内部工作方式。唯一需要记住的是组件的用途,因为它将成为您使用的各种组件之一(您不知道 Tedit 是如何实现的,并且您不需要,但您仍可以在创建的每个项目中使用它。)



根据给定的标准,方法1是最好的 >。


I am developing a Delphi 10.1 VCL application for Windows.

For integer or float input I need a number input field which is connected with a slider. When the user changes the number in the input field the slider position changes accordingly. When the user changes the slider position the number in the number field is updated.

I can solve this by using a TEdit and a TTrackBar and add the necessary update functionality in their OnChange event handlers.

The problem is that I need many of such inputs on different forms. Therefore I would like to create a new component which combines the two controls TEdit and TTrackBar in one component.

  1. Is the creation of a new component the best strategy for the multiple use of such a slider input?

  2. What is the best way to create such a new component?

解决方案

Is the creation of a new component the best strategy for the multiple use of such a slider input?

Not necessarily true all the time. (by my standards at least).

What is the best way to create such a new component?

I know three ways to solve your problem.

Way number 1:

create the component using the new component wizard where you create dynamically the Tedit and the Ttrackbar sub components in a Tgroupbox descendant.

the following is how I would do that.

unit Combindedittrack;

interface

uses
  System.SysUtils, System.Classes, Vcl.Controls, Vcl.comctrls ,Vcl.StdCtrls;

type
  TCombindedittrack = class(Tgroupbox)
  private
    { Private declarations }
    Fedit:tedit;
    Ftrackbar: TTrackBar;
    procedure editonchangeproc(Sender: TObject);
    procedure trackbaroOnchange(Sender: TObject);
  protected
    { Protected declarations }

  public
    { Public declarations }
    constructor Create(owner:tcomponent); override;
    destructor Destroy; override;
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TCombindedittrack]);
end;

constructor TCombindedittrack.Create(owner:tcomponent);
begin
inherited Create(owner);
SetBounds(0, 0, 250, 50);
Fedit:=tedit.Create(self);
with Fedit do
  begin
  text := '';  //<-- you control the appearence here
  top := 10;
  left := 10;
  height := 27;
  width := 50;
  parent:=self;
  Onchange:=editonchangeproc; // your Onchange event handler for the Tedit
  end;

Ftrackbar:=ttrackbar.Create(self);
with Ftrackbar do
  begin
  top := 10;  //<-- you control the appearence here
  left := 60;
  height := 30;
  width := 50;
  parent:=self;
  Onchange:=trackbaronchangeproc;  // your Onchange event handler for the Ttrackbar
  end;
end;

destructor TCombindedittrack.Destroy;
begin
Ftrackbar.Free;
Fedit.Free;
inherited;
end;

procedure TCombindedittrack.trackbaroOnchange(Sender: TObject);
begin
// <-- track bar onchange handling here.
end;

procedure TCombindedittrack.editonchangeproc(Sender: TObject);
begin
// <-- edit onchange handling here.
end;

end.

Way number 2:

Use frames like this (I'm on delphi 10 seattle).

1) File-->New-->Other-->(search for frames on delphi files).

2) Now add the edit and the track bar and set their Onchange events.

3) Save the unit.

4) on the tool palette (the standard component section) click on the frame component.

5) choose the frame you just created.

You will have a replica of the frame each time you use it.

Way number 3:

Use component template like this (again I'm on delphi 10 seattle)

1) Select your already created and modified tedit and ttrackbar.

2) On the toolbar's "component" click "create component template".

3) Name your template and press OK.

4) Select the Template palette and then your template.

Now notice that even your code (events) are added as well to your project.

Finally

With the level I have on delphi and the IDE I'm really unable to give you a clear answer to which is the best way but nevertheless I have shared all what I know that could help you.


edit: Since a lot of comments are insisting that the answer should state which is the best way to do this. this is the best way based on the following.

let's put some of the key point that should be accounted for when choosing

1. Ease of modifying the combined control(s) if you wish so (by my experience you will).

2. time needed to complete this task (it means the time it will take you to fully complete the task with minimum debugging and coding).

3. general source code readability.

4. usefulness for the future of your projects.

Now lets start criticizing the three methods based on the those criteria.

Way number 1:

C1(criteria number 1): Just modify the the source implementation of the component and each replica/use will have the same effects and properties. However this is not the case for way number 3.

C2: It depends on your knowledge of component writing but for this component it took me 5 min to create it and I'm only a beginner in delphi. For the debugging if something went wrong and the problem is in the component implementation than you just need to fix once (see C1)

C3: their is no implementation in your form(s) source code for your component just add it to your form and every thing is hidden (for example add a tedit and go to see the implementation in your forms source).

C4: You are creating a component after all this will open the door for you to create your own set of components like the Flatstyle or Indy open source components. so next time you need some thing like this you just drop it in your form designer and you are done.

Way number 2: frames

C1: It is like way number 1 because you are creating a component but it is visually this time. modifying the source frame will change the effects and properties of the replicas, also you can add extra handling to your replicas.

the event handler of the replica's Onchange event is like this

procedure TForm1.Frame2Edit1Change(Sender: TObject);
begin
  Frame2.Edit1Change(Sender); //<-- this is the original onchange event you setup at the beginning 
  //<-- you can extra handling here if you want one of the replicas to behave differently than the original  
end;

C2: same time and maybe faster than way number 1.

C3: over all, it has the same out come as way number 1.

C4: unlike way number 1 you can not use frames created in project A in project B. So your coding and debugging will stay in project A.

Way number 3: component template.

C1: you are not creating a component you are creating a repleca/macro of the exact steps you did in your last project. changing one will not change the others they are separated.

C2: same time and maybe faster than way number 1.

C3: each time you add a template to your form the events code will be added (not a good view if it is a long Onchange code).

C4: You can use templates created in project A in project B. However what you wrote in project A will be in project B (see c1) even the references of variables that don't exist in project B (this can be hard to debug and misleading, considering the period of time between each use of the template).

Conclusion: each of the ways presented will consume time to code and debug and all of them will do the task, How ever for the sake of simplicity and the reuse with minimum risks Way number 1 is the safe choice here because it will give you the chance to update and upgrade safely. also debug faster.

the good thing also about way number 1 is that after a while when you will forget the implementation and how things are working internally. The only thing that should stay in mind is the purpose of the component because it will become one of the various component you use (you don't know how Tedit is implemented and you don't need to but yet you use it in every single project you create).

based on the criteria given Way number 1 is the best.

这篇关于通过在Delphi VCL中组合两个控件(TEdit和TTrackBar)来创建新组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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