从容器控件中删除自身的项控件 [英] Item control to delete itself from container control

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

问题描述

有一个容器控件,一个 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天全站免登陆