4

When a developer double-clicks an event of a control in the Object Inspector and there has not been any code for that event Delphi will automatically create that event's procedure declaration and its code. I would like to know how a developer can tell Delphi or in the least indicate to Delphi where in the unit the implementation part of the events should be placed.

When I create a unit I usually insert a predefined set of comments and I have certain areas in the unit where I add certain type of code. At the very end of the unit I have all the automatically created event procedures. Sometimes I get it right that Delphi will continue to place the automatically created event code at the end of the unit but sometimes it keeps on putting it in between my own manually created procedure. I want to tell Delphi where to place it, if possible.

1 Answer 1

8

Back in the old days, the IDE added new methods at the end of a class. At some point, I cannot remember when, the IDE was changed so that it attempts to insert new methods to maintain an alphabetical order. If your methods are not in alphabetical order, then I for one have no idea how the IDE decides where to put them. I think it still tries to place them in order, but the code that the IDE uses is based on the (false) assumption that the methods are ordered. Frankly it is something of a mess.

In my opinion, the original approach was better. At least it was predictable. In reality, for most serious development, the developer cares about how to order the methods in the implementation. Each developer will have their own way to do this. And even the same developer will choose different approaches in different situations. The approaches used will typically not be based entirely on the name of the method. What the method does is far more important. Furthermore, I doubt that anyone uses the approach that the methods should be ordered in the same order that they were generated.

So, the obvious conclusion here is that the IDE cannot do a perfect job, and there needs to be manual intervention. The developer is going to want to move methods around, and if the IDE could do anything to help it would make it easy for the developer to re-arrange method implementations, perhaps by dragging them around in a tree view representation of the source.

For you, I don't believe that it is possible to persuade the IDE to place all new methods after all existing methods. So you will simply have to get into the habit of moving a newly generated method to your preferred location.

Sign up to request clarification or add additional context in comments.

3 Comments

If I recall correctly, there's also a difference between if there's a line with { TForm1 } present or not (or whichever the class name of the form is)
@David After reading your reply I played around and realized that Delphi still wants to add the methods alphabetically. So I saw that as long as my last manually created procedure's name is alphabetically after the event procedure name of a freshly created event then Delphi will add the newly auto created procedure before my own. If I move that newly created one to where I want it then everything after that will be placed where I want it until a newly created event procedure has a name that is before the first event procedure AND my own which will be placed before my own again. Rinse, repeat.
Seeing that you gave the direction which allowed me to find a solution, I will mark your answer as such. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.