In any (VCL) Delphi application more stuff happens behind the scenes than you might be aware of.
As known, every (TWinControl descendant) control placed on a form (like TButton or TEdit) and even the (T)Form itself is actually a wrapper for a Windows screen object. Every such object is “defined” by something called Window Handle. You can use the Handle value to do stuff with the underlying Windows screen object like hide or show it or move around its parent (another Windows screen object) or change some of its properties.
Continue reading
Author Archives: zarkogajic
Quick Algorithm: Display an Array of Integers as Set of Integer Ranges
Say you have an array of integer values, for example, pages of a document or years when something happened, like years when new Delphi version was released:
delphiReleased: TArray<integer> = [1995, 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2005, 2006, 2007, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2020, 2021, 2022];
If you would want to display the above years in a user friendly manner as a set of ranges, you could go for:
1995 - 1999, 2001 - 2003, 2005 - 2007, 2009 - 2018, 2020 - 2022
Here’s an algorithm (in a console app code) that does the above
Continue reading
Implement a Close Tab Button for a TTabControl (plus: a round button over hot tab)
Delphi’s TTabControl is a container type VCL control – allowing controls in its client area and displaying a set of tabs (as tabs or buttons). The TTabControl, unlike TPageControl, is not made up of several pages (for each tab), rather the TTabControl has one “page”. By design, the TTabControl is used when displaying/editing list of objects of the same type. When changing the active tab – you need to update the controls it hosts.
An example of a tabbed control UI is an Internet Browser – take a look at Chrome, IE, Edge, or Firefox, etc. tabs. See how each tab has a close button. No such similar close button, to “close a tab”, can be specified or set via properties or methods of the TTabControl. What if you need one (for whatever the purpose)?
So, how to implement a close button for a TTabControl’s Tab? Plus: how to display the close button only over the hot tab (under the mouse) and have the button nicely rounded.
Continue reading
Delayed Close Programmatically Dropped TComboBox (If Mouse NOT Over Combo and Combo’s List) Using Multi Threading
The Combo Box Windows control (aka TComboBox in Delphi) is one of the most frequently used user interface elements along with buttons and edits in Windows applications. TComboBox control represents an edit box with a scrollable drop-down list attached to it. Users can select an item from the list or type directly into the edit box. When the Style property is set to csDropDownList the combo allows to display a list of predefined items a user can select from the combo’s drop down list.
I’ve had a request from a user of my application: can the selection list be dropped down and then closed if no selection has been made in some period of time? Well, the user is asking. So, certainly it can!
The task to drop combo’s list is super easy: just set DroppedDown property to true (this sends CB_SHOWDROPDOWN to the combo). Then wait – but do not block the main thread !! – so some multithreading needed. Ok, use Delphi’s PPL. Finally, once the wait period is over, close the combo’s list – but, let’s say, only if the user is not hovering the mouse over combo’s items!
Looks pretty straightforward.
Continue reading
Cancellable Parallel Tasks With Modal Progress Dialog, i.e., On Delphi’s TTask, PPL, ThreadPool, WaitAll with Progress and the rest
Quite some time has passed since working with multiple threads was introduced in Delphi (Delphi 2, way back in 1996). The TThread class at that time was a very lightweight wrapper around the Windows CreateThread function. Those “crazy” enough to venture into the world of parallel executing code aka multi-threaded applications know how (proper) writing and debugging of such applications can become frustrating (at least).
Some more time has passed, and in Delphi XE7, Parallel Programming Library (PPL) was introduced to simplify the efforts we have to invest when there are needs to have more tasks (a unit of work you need to get done) being executed at the same time / in parallel.
If, until “now”, you were afraid of even trying to understand how to implement parallel programming in your applications – you should not be so. I think each and every application can benefit in some way of code being executed in multiple treads. The “Using TTask from the Parallel Programming Library” from Embarcadero’s DocWiki has more than enough to get you started.
I’m not going to list examples when multi-threading can come handy – I’m certain all this is known. Also, I’m not going to repeat the words of so many who wrote articles and blog posts (here’s one and another) explaining what TTask is, how VCL is not thread safe and how you need to use some synchronization to update the UI of the application (being run in the main thread), how to wait for all tasks to finish their execution or how to speed up your loops using TParallel.For.
Partially Reimplementing / Overriding Interface Implementation
In Delphi, the reserved word “interface” has two distinct meanings. In a Delphi unit, “interface” denotes the start of a unit section used to declare public constants, data types, variables, procedures and functions visible and accessible to other units using this unit. In OOP jargon, an “object interface” or simply “interface” is a kind of a class with no implementation (but not like a class with abstract methods). An interface defines methods that can be implemented by a class (a class “implements” the interface). Object interfaces might be used when multiple inheritance is needed, and are frequently used when working with COM objects. For some intro to interfaces: Interfaces in Delphi Programming 101.
Continue reading
Implementing SelectDelay for TComboBox – Postpone Event Handler Execution in Delphi
Delphi’s TComboBox combines an edit box with a scrollable list – with the Style property defining the display style of the combo. When set to csDropDownList there’s no edit box – users can only pick from the predefined items displayed by the list. If you need to react, when the selection changes, you will write code for the OnSelect event.
Now, presume our combo is a kind of a filter selector – when the selection changes you need to update some other UI control. For example, combo lists invoices and selecting one (item from its list) displays the selected invoice items in some grid control.
Depending on the number of invoice items, where they are stored, how you fetch them, and so on – the actual code to (fetch items and) refresh the UI might take longer time – whatever longer means. If you scroll quickly through the combo items – OnSelect will get fired for each visited item in the list and your application might appear irresponsible, lagging and slow.
Continue reading
Deleaker – Memory Leaks Hunter (Add-On for RAD Studio)
One of the common problems we programmers face (and hopefully we are aware of) are memory leaks, or leaks of any other kind of resources. For example, Windows limit the number of GDI or USER32 objects that a process can allocate at one time. When things go down the wrong road you would want to have the tools to help you find (again) the correct path to free-what-you-create.
Continue reading