Control to remove from container control

There is a container control, TScrollBox, which contains several control elements.

Each control, being composite, contains (parents and owns) a delete button. Pressing the button initiates the deletion of the control item.

Removal involves the release of a component, and therefore the actual operation must be external to the element. The question is, what would be the best way to do this?

I really know a few options:

  • a timer with a short interval (which starts with a button press);
  • hidden external button (on which mouse messages and up messages are displayed);
  • handler of the form handler.

Although I could confidently implement any of these methods, as I flatter myself, I am not sure which one will be better. In addition, the timer option seems childish, the hidden button is one hacker button, and the user message went too far. In short, all three seem equally half acceptable, more or less.

I can just be biased and not mind being convinced of the opposite. However, first of all, I would like to know what is the usual method for use in such cases (maybe something I was missing all the time).

+3
source share
2 answers

The usual approach is to send a message to the control, which should be released. See, for example, as TForm.Release . In fact, I see no reason why you cannot even reuse the CM_RELEASE message.

The fact is that a message is sent to the back of the queue and is processed only after processing any synchronous messages (i.e., sent SendMessage ). This avoids calling methods on the object after it is released, which is obviously an error that you are well aware of.

+3
source

First, I would suggest writing your own control that inherits from TScrollBox, and that you provide an instance and sub-control removal as a function inside this scroll box, and not something done β€œopen” in your form. This code will go in its own block, and only its public elements will be visible from the outside. This is just an object oriented framework.

Secondly, if you remove controls from the scroll window, the timer is simply a source of chaos. Perhaps if you also subclassed each control that you put in this container, you could use the mechanism used by TForm.Release (it sends them CM_RELEASE messages) and implements CM_RELEASE in a way that controls the deletion of itself when sending this message, however I find this ugly, except when editing controls that are destroyed when they lose focus.

I would immediately delete these methods, without resorting to a timer, by subclassing both the TScrolLBox class and any other classes that I want to insert into it, and then with the removal of the control it will be processed by the parent object (TScrollBox), and not by external manipulations of any type.

+2
source

Source: https://habr.com/ru/post/1345735/


All Articles