Component和Control区别

经过实践发现:

Component包含不可见的组件(就是找不到visible属性的组件,例如:ADOQUERY、POPUPMENU等),

Control不包含


以下内容为转载:     

       很多时候会将一个Panel1放在Form1上,然后再将一个Button1放在Panel1上,这样的话,这个Button1的Owner就是Form1而Parent则是Panel1。



        所有的Delphi的Component都有Owner 属性,Owner属性表示这个Component的所有者是谁,比如上面的例子,Button1的所有者(Owner )就是Form1,当Form1析构时,会先将Button1释放掉。也就是说,Owner会自动地控制Component的生命周期,它负责构件的创建和释放。如在上例中,系统默认Form上所有Component的所有者是Form1。顺便指出,Create方法应带有表示Component的Owner的参数.,如果Owner设置的为Nil值,那这个Component必须创建者编码析构它,Owner属性是只读的,并且在运行期是无法修改它的值。


         类似,但不同于Owner属性,Parent 属性则表示Component从属于另一个Component,简单的说自身是其他Component的Child Component,例如 TForm,TGroupBox ,TPanel等。Parent是用来控制 在它的客户区范围内的Child Component,Parent决定如何展示包含的Child Component,例如:Left,Top等属性都是相对于Parent的位置。


         Parent属性可以在运行期被修改。并非所有的Component都有Parent,Parent属性可以为Nil值,可以用HasParent 方法返回的Boolean值来判断Component是否拥有Parent。 我们可以设置Parent属性来做一些控制,例如:我们可以在Form1上置Panel1和Panel2,然后再放一个Button1在Panel1上,在Button1的OnClick事件中写上:Button1.Parent := Panel2;  运行这段代码你会发现,开始Button1是在Panel1上,然后按下Button1触发OnClick事件后,Button1‘跳’到了Panel2上。我们要在运行期间创建一个Button的话,就一定要注意指定它的Parent属性,否则Button不会显示出来,因为它必须拥有一个Parent的容器来显示自己。如果你在设计这个Button时察看下属性编辑器,你会看到ParentFont和ParentShowHint等属性,类似于这样的属性设置为True的话,就会使Button的Font以及ShowHint属性按Parent的Font和ShowHint的值来设置,保持一致的风格。比如Parent的Font是红色,那么Button的字体也将是红色。


        ControlCount和ComponentCount的区别也就取决于Parent与Owner的区别,还是拿上面的例子解释下Panel1的ControlCount为1而ComponentCount却为0,是因为Button1的Parent属性指向的是Panel1而Owner属性则还是Form1,所以Form1的ComponentCount值才是1。我们可以利用Controls属性与Components属性去遍历Parent所包含的Child Component和Owner所管理的Child Component。
### Component vs Controller in Software Architecture and Frameworks In modern web development, distinguishing between components and controllers is essential to understand how different parts of an application interact with each other. #### Components Components represent self-contained units that encapsulate both behavior and appearance within a user interface. These elements are reusable across various sections or even projects due to their modular nature[^1]. A component typically includes its own template (HTML), style (CSS/SCSS), and logic (JavaScript). For instance, buttons, cards, modals can all be implemented as individual components which may receive data through properties or emit events when actions occur inside them. ```html <!-- Example of a simple button component --> <button class="btn btn-primary">{{ label }}</button> ``` #### Controllers Controllers manage business rules and coordinate interactions among models, views, and services. They act like conductors orchestrating what happens after receiving input from users via forms submission, clicking on links/buttons etc., processing this information according to predefined algorithms/logic before sending back responses either directly updating DOM elements or indirectly by modifying state objects shared amongst multiple components. For example, consider a login form where the controller would handle authentication checks against stored credentials once submitted: ```javascript // Pseudo-code demonstrating basic controller functionality for handling logins. function LoginController() { function authenticate(username, password) { // Logic here... if(validUser){ redirectToDashboard(); }else{ showErrorMessage(); } } return {authenticate}; } ``` The separation ensures clear boundaries exist between presentation layer concerns handled primarily by **components**, while core functionalities reside under control structures managed separately using **controllers**. This approach promotes maintainability, testability, and scalability throughout large-scale applications.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值