物品控制从容器控制中删除 [英] Item control to delete itself from container control

查看:131
本文介绍了物品控制从容器控制中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个容器控件,一个TScrollBox,它承载多个项目控件。

There's a container control, a TScrollBox, that parents multiple item controls.

每个项目控件,复合本身包含(父母和自己)一个删除按钮。按按钮启动项目控件的删除。

Every item control, being compound itself, contains (parents & owns) a delete button. Pressing the button initiates the deleting of the item control.

删除涉及释放组件,因此实际操作对于项目应该是外在的。问题是,最好的方式是什么?

The deleting involves freeing the component and so the actual operation should be extrinsic with regard to the item. The question is, what would be the best way to do it?

我实际上知道了几个选项:

I actually know of a couple of options:


  • 一个间隔小的定时器(从按钮点击开始);

  • 一个隐藏的外部按钮(鼠标向上和向上消息发布);

  • 表单的自定义消息处理程序。

虽然我可以自信地实现任何这些方法,因为我自夸,我不知道哪一个将是最好的。此外,定时器选项看起来很幼稚,隐藏的按钮一个黑客,自定义消息有点过分。简而言之,所有这三个人看起来都差不多一半,或多或少。

While I could confidently implement any of those methods, as I flatter myself, I'm not sure which one would be best. Besides, the timer option seems childish, the hidden button one hackish, and the custom message one a bit overkill. In short, all three seem equally half-acceptable, more or less.

我可能只是偏见,不介意相信相反。仍然最重要的是我想知道在这种情况下使用什么常用的方法(可能是我一直都缺少的东西)。

I may simply be prejudiced and don't mind being convinced to the contrary. Still most of all I would like to know what is a common method to use in such cases (maybe something I've been missing all the time).

推荐答案

通常的方法是将消息发布到要释放的控件。看看如何实现 TForm.Release 。实际上,我没有理由不能再使用 CM_RELEASE 消息。

The normal approach is to post a message to the control that is to be freed. See how TForm.Release is implemented for example. In fact I see no reason why you can't even re-use the CM_RELEASE message.

关于发布消息是到队列的后面,只有在任何同步消息(即由 SendMessage 发送的消息)完成处理之后,才会被处理。这避免了在对象被释放后调用方法,这显然是您清楚地知道的错误。

The point about posting the message is that is goes to the back of the queue and only gets processed once any synchronous messages (i.e. those delivered by SendMessage) have completed processing. This avoids calling methods on an object after it has been freed which is obviously an error that you are clearly well aware of.

这篇关于物品控制从容器控制中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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