Monday, July 2, 2012

Getting a Lock on Delphi

Some of Delphi's more powerful and best kept secrets have actually been in the product since the beginning, and in this short post I want to point out one of these, a feature that most Delphi developers I talk to are unaware of.

Last October I gave a keynote address in Düsseldorf, Germany, as part of the 15th annual Delphi Entwickler Konferenz (Developer Conference), generally known as EKON. The talk was titled The Evolution of Delphi: From Delphi to Delphi XE2, and I used the opportunity to reflect on the many groundbreaking innovations introduced in Delphi throughout the years, with one of the biggest innovations being Delphi itself.

Early in the talk I demonstrated the original version of Delphi (which was called Delphi, not Delphi 1, but the name Delphi 1 certainly makes it easier to convey what we are talking about when referring to the first release of this product). Seeing that original 16-bit application run again, albeit in 32-bit Windows 7, was fun and educational. (I had tried to create a virtual machine with Windows 3.11 for Workgroups, but alas, I tossed those disks during my last move.) Yes, Delphi 1 looks funny compared to the sleek and sophisticated stylings of the current product, but it really did have an amazing collection of capabilities.

Among the more profound features was runtime type information, a mechanism for generically examining the published members of objects, determining their names and types, and reading and writing their values. It was groundbreaking, and served as the foundation for the designer that changed the way we built our user interfaces.

But that is not the feature I want to talk about here. Instead, I want to bring attention to a often overlooked and greatly underused feature that can go a long way to making your life easier, at least as far as user interface design is concerned. This feature is a menu item called Lock Controls, and you can see it in the following figure from Delphi's (the original) Edit menu.

Lock Controls is a toggle menu item. Turn it on by select Edit | Lock Controls, and turn it off by selecting it again. When Lock Controls is on, you cannot change the positions of the components in the designer. Turn it off, and you can once again move and resize your controls.

Lock Controls is exceptionally valuable when you have a complex user interface with many controls precisely positioned, and you do not want to accidentally move anything as you select those components to adjust their properties or add event handlers. Once you have the components positioned where you need them, select Edit | Lock Controls to freeze the user interface. Now you can select individual components or groups of components without worrying about unintentionally moving them with your mouse.

Lock Controls applies to all forms, data modules, and frames, not just the one you are currently working on. As a result, you will need to turn it back off before you return to the task of user interface design.

Here is how Lock Controls looks today, in Delphi XE2. It's still in the same place, but works about the same as it did in the beginning. Actually, Lock Controls now prevents any change in position or size of controls. In pre-Delphi 2005 versions, you could still move or resize controls using your keyboard with the Ctrl and arrow keys. Nowadays, locking controls even prevents those operations.


  1. Hi!

    For the editor itself, for the code, I activate sometimes the "Read-Only" feature when I am consulting the code to do "copy" block of code from it to another tab.

    When looking at the "FORM" itself of the program, I was always hoping for years that they would do the same so I would not accidentally move precise-aligned component...

    After reading your today's blog, I feel stupid for all these years not realizing it was there!!!

    Thank you very much for taking the time to mention it. I will use it for sure now.
    I wish they could tie together the "Read-only" for the code and "Lock controls" for the form but, no matter what, it is useful like it is alos.

  2. I found this feature years ago and using it in daily work - the time before was real horror, every time I seleted a component it moved some pixel out of place...

    Now it is one of the first steps after a new Delphi installation to define an IDE Menue Shortcut with GExperts... Never without this small but really cool and usefull feature! :-)

  3. I knew that there is this possibility and I had put it in the normal toolbar (just use right click and customize) previously. With CnWizards I even have it directly over the form editor.

  4. If you have to use that feature, then it is a sign, that your interface won't behave correctly in high-DPI-scenarios. But after the release of MacBook Pro with Retina display (and coming soon follow up from other companies) such scenarios will be more and more common.

  5. Other IDEs have an auto-lock mechanism: the first time you select a component, you can't move it. You have to release the mouse button then re-click to move.

    That pretty much eliminates all the spurious select/move IME.