About Delphi Programming Tips - 2008
Delphi tips from the free About Delphi Programming newsletter.
More tips by year: 2009 | 2008 | 2007 | 2006 | 2005 | 2004 | 2003 | 2002 | 2001 | 2000 | 1999
More tips by year: 2009 | 2008 | 2007 | 2006 | 2005 | 2004 | 2003 | 2002 | 2001 | 2000 | 1999
Prevent a Delphi Form from Being Moved Off Screen
Try creating a simple form application. Now move the form outside of the screen area. You can easily do this by simply dragging the form by its caption (title) area to the right - not that the part of the form to the right of the mouse pointer can easily be moved outside of the work area of the screen!
Try creating a simple form application. Now move the form outside of the screen area. You can easily do this by simply dragging the form by its caption (title) area to the right - not that the part of the form to the right of the mouse pointer can easily be moved outside of the work area of the screen!
Programmatically Fix the TabOrder property in your Delphi applications
The TabOrder property available for any TWinControl descendant control (such as TButton, TEdit, TComboBox, etc.) indicates the position of the control in its parent's tab order. How many times have you "inserted" a visual component (control) between two control - and have forgotten to fix the tab order of the controls?
The TabOrder property available for any TWinControl descendant control (such as TButton, TEdit, TComboBox, etc.) indicates the position of the control in its parent's tab order. How many times have you "inserted" a visual component (control) between two control - and have forgotten to fix the tab order of the controls?
Get Subdirectories of a specified Directory
When developing applications that need to operate on files and directories it is a common task to, for example, get the list of all sub-directories for a given directory. Here's how to get the sub folders for a specified directory.
When developing applications that need to operate on files and directories it is a common task to, for example, get the list of all sub-directories for a given directory. Here's how to get the sub folders for a specified directory.
Dirty Delphi Localization
The "fastest path to Delphi localization" article describes how to do a simple and dirty localization of various VCL messages used by the MessageDlg function and alike. This time a function is used to change the value of a string resource at run time.
The "fastest path to Delphi localization" article describes how to do a simple and dirty localization of various VCL messages used by the MessageDlg function and alike. This time a function is used to change the value of a string resource at run time.
Reposition Inherited Controls Back to Its Original Position
When you change any of the properties of a control on the base form, for example, Color - the color of this control on the inherited form will also change. This is in the heart of Delphi's visual form inheritance. You have inheritance that is visual. What if you need to "undo" the changes? How to make sure that the inherited control again looks and feels like the original?
When you change any of the properties of a control on the base form, for example, Color - the color of this control on the inherited form will also change. This is in the heart of Delphi's visual form inheritance. You have inheritance that is visual. What if you need to "undo" the changes? How to make sure that the inherited control again looks and feels like the original?
Get the Application associated with the Shell Print Command for a File Type
If your want to programmatically print files from Delphi code, using the associated shell application, you need to first make sure that there is an application associated with the shell's print command for a given file type. Here's how to get the application associated with the shell print command for a file type from Delphi
If your want to programmatically print files from Delphi code, using the associated shell application, you need to first make sure that there is an application associated with the shell's print command for a given file type. Here's how to get the application associated with the shell print command for a file type from Delphi
File Size - Get the Size of a File in Bytes using Delphi
When working with files and folders from a Delphi application you sometimes need to know what is the size of a file (in bytes). A custom function, FileSize function returns the size of a file in bytes, -1 if the file was not found.
When working with files and folders from a Delphi application you sometimes need to know what is the size of a file (in bytes). A custom function, FileSize function returns the size of a file in bytes, -1 if the file was not found.
Class Helper for Delphi's TStrings: Implemented Add(Variant)
In Delphi, the TStrings is the base class for objects that represent a list of strings. Examples include Lines property of a TMemo, Items property of a TListBox or TComboBox, etc. To add strings to the list you use the Add method which accepts a string parameter. Here's a class helper for TStrings to extend the Add ethod and allow variant parameters.
In Delphi, the TStrings is the base class for objects that represent a list of strings. Examples include Lines property of a TMemo, Items property of a TListBox or TComboBox, etc. To add strings to the list you use the Add method which accepts a string parameter. Here's a class helper for TStrings to extend the Add ethod and allow variant parameters.
Overloading Delphi's ShowMessage to accept Integer, Boolean, Float, ...
I'm using Delphi's ShowMessage procedure frequently while developing applications as a "debugging" tool to display values of various variables, properties, function results. A downside of the ShowMessage is that it only accepts a string parameter - where variables tend to be integers, bools, floats, etc. :)
I'm using Delphi's ShowMessage procedure frequently while developing applications as a "debugging" tool to display values of various variables, properties, function results. A downside of the ShowMessage is that it only accepts a string parameter - where variables tend to be integers, bools, floats, etc. :)
A Custom Message Dialog with Standard Windows Icons
I needed to create my own customized "MessageDlg" message box with custom buttons, but I still wanted to have access to those spiffy icons that appear when you specify "mtInformation, mtConfirmation..." as the MessageDlg message type. Here's how to use the LoadIcon API to grab standard WIndows dialog icons from Delphi.
I needed to create my own customized "MessageDlg" message box with custom buttons, but I still wanted to have access to those spiffy icons that appear when you specify "mtInformation, mtConfirmation..." as the MessageDlg message type. Here's how to use the LoadIcon API to grab standard WIndows dialog icons from Delphi.
Formatting a File Size in Bytes for Display
When working with files from Delphi you might want to display the size of a file to the user in a Explorer-like format where the file size is not displayed in bytes - but the display depends on the size of the actual file.
When working with files from Delphi you might want to display the size of a file to the user in a Explorer-like format where the file size is not displayed in bytes - but the display depends on the size of the actual file.
Controling the Number of a Delphi application Instances on a Terminal Server
If you need to control the number of instances of your application that are being executed at one time, there are a few approaches you can take: using a mutex, or creating a file mapping. But what if your application is being run under a terminal service session?
If you need to control the number of instances of your application that are being executed at one time, there are a few approaches you can take: using a mutex, or creating a file mapping. But what if your application is being run under a terminal service session?
Get the Control Under the Mouse in a Delphi application
If, in your Delphi application you need to know what control is "under" the mouse while the mouse is being moved over the application, you can use RTL's FindVCLWindow function.
If, in your Delphi application you need to know what control is "under" the mouse while the mouse is being moved over the application, you can use RTL's FindVCLWindow function.
Graphical Progress Bar for Delphi applications - Partial / Continuous Move
When you do not know how many steps are needed for a progress bar - you might want to display a continuous bar - or a moving graphical object. The UpdateImageProgress Delphi procedure takes a reference to a TImage control displaying a picture. By calling the procedure from inside, for example, a timer event (TTimer control), a graphical progress effect is achieved.
When you do not know how many steps are needed for a progress bar - you might want to display a continuous bar - or a moving graphical object. The UpdateImageProgress Delphi procedure takes a reference to a TImage control displaying a picture. By calling the procedure from inside, for example, a timer event (TTimer control), a graphical progress effect is achieved.
Display an Error Message for an OS Error Code
Delphi's SysErrorMessage returns an error message string that corresponds to the specified OS error code. OS error code can be retrieved by a call to GetLastError - last error reported by an operating system API call.
Delphi's SysErrorMessage returns an error message string that corresponds to the specified OS error code. OS error code can be retrieved by a call to GetLastError - last error reported by an operating system API call.
Delphi Project Structure Folder Organisation
One question I always ask myself when starting a new Delphi program - a project with two or more applications - is how to best layout all the Delphi files one project is made of?. Source code files (PAS) and DFM files should go into a source controls system. Executable should be placed into a separate folder. Third party tools (units) should have thier own folders. If I have BPLs or DLLs they also should have their own folder. Here's what my current best practice in Delphi project layout is...
One question I always ask myself when starting a new Delphi program - a project with two or more applications - is how to best layout all the Delphi files one project is made of?. Source code files (PAS) and DFM files should go into a source controls system. Executable should be placed into a separate folder. Third party tools (units) should have thier own folders. If I have BPLs or DLLs they also should have their own folder. Here's what my current best practice in Delphi project layout is...
FirstDelimiter Delphi Function
In Delphi's RTL there's a LastDelimiter function which returns the index of the last occurence in a string of the characters cpecified. What about the first delimiter - a function that would return the index of the first occurence in a string of the characters cpecified. Here's the implementation of FirstDelimiter.
In Delphi's RTL there's a LastDelimiter function which returns the index of the last occurence in a string of the characters cpecified. What about the first delimiter - a function that would return the index of the first occurence in a string of the characters cpecified. Here's the implementation of FirstDelimiter.
Prevent the System from Entering Sleep or Turning off the Display
If you have a critical application running and you want to prevent the system from entering sleep or turning off the display while the application is running you need to call a Windows API function named SetThreadExecutionState. The TSystemCritical is a wrapper of this api call. Full source code included.
If you have a critical application running and you want to prevent the system from entering sleep or turning off the display while the application is running you need to call a Windows API function named SetThreadExecutionState. The TSystemCritical is a wrapper of this api call. Full source code included.
Is your Delphi Application Running under Terminal Services as a Remote Session?
Windows Terminal Services environment enables users to access Windows-based programs that are installed on a terminal server, or to access the full Windows desktop. Users can connect to a terminal server to run programs, save files, and use network resources on that server. Here's a handy Delphi function using the GetSystemMetrics API call to test if the application is remotely controlled.
Windows Terminal Services environment enables users to access Windows-based programs that are installed on a terminal server, or to access the full Windows desktop. Users can connect to a terminal server to run programs, save files, and use network resources on that server. Here's a handy Delphi function using the GetSystemMetrics API call to test if the application is remotely controlled.
Need to Return a TStrignList (any object) from a Delphi Function?
Do you know that a Delphi function can return any object type value, like a string list (TStringList) or a button (TButton)? If you know then I guess you also know that you have to be very careful when calling such a function. When a function returns an instance of an object - always make sure you free the object when no longer needed.
Do you know that a Delphi function can return any object type value, like a string list (TStringList) or a button (TButton)? If you know then I guess you also know that you have to be very careful when calling such a function. When a function returns an instance of an object - always make sure you free the object when no longer needed.
Non Focusable Window - Create a Delphi Form that can Not be Activated
When Delphi creates a form, the Create method calls the CreteWindowEx API function to crete the actual window. CreateParams methods called before the window is actually created lets you change the default style of a window when it is created to suit your particular needs. Here's how to create a form that cannot be activated - will never have the focus - will never be a foreground window.
When Delphi creates a form, the Create method calls the CreteWindowEx API function to crete the actual window. CreateParams methods called before the window is actually created lets you change the default style of a window when it is created to suit your particular needs. Here's how to create a form that cannot be activated - will never have the focus - will never be a foreground window.
Where to Find Glyphs and Icons for a Delphi Application, Menu, Toolbar
A glyph in Delphi lingo is a bitmap image that can be displayed on a BitBtn or SpeedButton controls using the controls Glyph property. Glyphs and icons (and graphics in general) make your application user interface elements look professional and unique.
A glyph in Delphi lingo is a bitmap image that can be displayed on a BitBtn or SpeedButton controls using the controls Glyph property. Glyphs and icons (and graphics in general) make your application user interface elements look professional and unique.
Where to Locate the Missing TServerSocket and TClientSocket Delphi components
If you have installed a newer Delphi version and you have a legacy application using the TServerSocket and TClientSocket component - you would face a problem! Delphi socket components are no longer installed into the component palette by design - therefore you will not be able to compile your project. Here's where to find and how to install the two sockets component.
If you have installed a newer Delphi version and you have a legacy application using the TServerSocket and TClientSocket component - you would face a problem! Delphi socket components are no longer installed into the component palette by design - therefore you will not be able to compile your project. Here's where to find and how to install the two sockets component.
Set TRichEdit Print Margins Programatically
Rich Edit provides a simple [b]Print method[/b] allowing you to programmatically print the (formatted) contents of the rich edit to the active printer - with a single line of code. Unfortunatelly, there's no exposed functionality to set printer margins as you are accustomed to in various word processing applications.
Rich Edit provides a simple [b]Print method[/b] allowing you to programmatically print the (formatted) contents of the rich edit to the active printer - with a single line of code. Unfortunatelly, there's no exposed functionality to set printer margins as you are accustomed to in various word processing applications.
Hide a Delphi 2007 Application Button from the TaskBar (with MainFormOnTaskBar)
With Delphi 2007, a new property of the Application object, the MainFormOnTaskbar property controls how Windows taskbar buttons are handled by VCL. The code using SetWindowLong to change the application's window style parameters no longer works. Here's how to hide the Delphi 2007 application taskbar button.
With Delphi 2007, a new property of the Application object, the MainFormOnTaskbar property controls how Windows taskbar buttons are handled by VCL. The code using SetWindowLong to change the application's window style parameters no longer works. Here's how to hide the Delphi 2007 application taskbar button.
Calculating the Zodiac Sign from the Birth Date
The ZodiacSign, custom Delphi function, will return the name of the zodiac sign for a given date value.
The ZodiacSign, custom Delphi function, will return the name of the zodiac sign for a given date value.
Left Align Caption of a TSpeedButton Delphi button
When you have a group of Delphi's speed buttons one after another in vertical "line", with Layout set to blGlyphLeft the buttons do not look so nice. What you might start looking for is the "Caption Layout" property to let you set the alignement of the button caption. Sadly, you'll find no such property :( Or, would you?
When you have a group of Delphi's speed buttons one after another in vertical "line", with Layout set to blGlyphLeft the buttons do not look so nice. What you might start looking for is the "Caption Layout" property to let you set the alignement of the button caption. Sadly, you'll find no such property :( Or, would you?
Handling Global Application Object Events in Delphi
The TapplicationEvents Delphi component wraps the global Application object available to you in all Delphi applications. When you add a TApplicationEvents object to a (main) form, the unique Application object forwards all events to the TApplicationEvents object. For some events the Application object exposes, the TApplicationEvents does not expose the event in the Object Inspector. Here's what to do.
The TapplicationEvents Delphi component wraps the global Application object available to you in all Delphi applications. When you add a TApplicationEvents object to a (main) form, the unique Application object forwards all events to the TApplicationEvents object. For some events the Application object exposes, the TApplicationEvents does not expose the event in the Object Inspector. Here's what to do.
Dim Out the Main Form of an Application When Modal Form is Displayed
To emphasise the importance of a modal form and the information it presents, you could gray out the main form of your Delphi application when the modal form is activated. Use the OnModalBegin and OnModalEnd events of the TApplicationEvents component along with AlphaBlend transparency effect.
To emphasise the importance of a modal form and the information it presents, you could gray out the main form of your Delphi application when the modal form is activated. Use the OnModalBegin and OnModalEnd events of the TApplicationEvents component along with AlphaBlend transparency effect.
How to Refresh a DBGrid without Loosing the Current Row Position
When DBGrid is used to display data from a dataset (query or table), by design, after you call Refresh method on a dataset (re-open) (for example, using a DBNavigator), the current row position will be reseted to zero (ok, first). This means that if a user has selected a row somewhere near the bottom of a DBGrid, after a Refresh, the active row will be changed to first :( Here's one answer to this problem...
When DBGrid is used to display data from a dataset (query or table), by design, after you call Refresh method on a dataset (re-open) (for example, using a DBNavigator), the current row position will be reseted to zero (ok, first). This means that if a user has selected a row somewhere near the bottom of a DBGrid, after a Refresh, the active row will be changed to first :( Here's one answer to this problem...
Select Which Panel Displays AutoHint in Delphi's TStatusBar
When StatusBar's AutoHint is true, the status bar automatically responds to hint actions by displaying the long version of the hint’s text in the first panel. You might want the hint to be displayed in the second or third panel .. here's how.
When StatusBar's AutoHint is true, the status bar automatically responds to hint actions by displaying the long version of the hint’s text in the first panel. You might want the hint to be displayed in the second or third panel .. here's how.
Take a Screen Shot of an Inactive Window - Using PrintWindow API and Delphi
In most cases you will want to take a screen shot of the active window or the Windows Desktop. What if you need to do a screen capture of all the running applications - most of them will be inactive and not visible to the user? Here's how to use the PrintWindow API from Delphi.
In most cases you will want to take a screen shot of the active window or the Windows Desktop. What if you need to do a screen capture of all the running applications - most of them will be inactive and not visible to the user? Here's how to use the PrintWindow API from Delphi.
Use Custom CheckBox Images for a ListView
When the ViewStyle property of a TListBox Delphi control is set to vsReport and CheckBoxes is set to true, list view includes a check box next to the items in the list. By design this check box is, well, "ugly". It has a nasty thick 2 pisel border - and there's no property to change it. One way to have nice check boxes in a list virw control is to use your own custom graphics (16x16 px bitmap image) and draw it over the original check box. Hre's how...
When the ViewStyle property of a TListBox Delphi control is set to vsReport and CheckBoxes is set to true, list view includes a check box next to the items in the list. By design this check box is, well, "ugly". It has a nasty thick 2 pisel border - and there's no property to change it. One way to have nice check boxes in a list virw control is to use your own custom graphics (16x16 px bitmap image) and draw it over the original check box. Hre's how...
No Main Form Delphi Application
Every Delphi applications must have a main form. The main form is the first form created in the body of the application. When the main form closes, the application terminates. What if you need to create an application where every form is equally important (for example like IE) - where closing the main form will *not* close the application.
Every Delphi applications must have a main form. The main form is the first form created in the body of the application. When the main form closes, the application terminates. What if you need to create an application where every form is equally important (for example like IE) - where closing the main form will *not* close the application.
Formatting Lines in Rich Edit using Delphi's SelText and SelStart
While you can create nice user interface "around" the rich edit control with toolbar buttons to set and change text display attributes, adding formatted lines to rich edit programmaticaly is fairly cumbersome - as you will see.
While you can create nice user interface "around" the rich edit control with toolbar buttons to set and change text display attributes, adding formatted lines to rich edit programmaticaly is fairly cumbersome - as you will see.
Custom Drawing of TListView Items - Font, Colors, Graphics
If you want to apply custom drawing (fonts, color, graphics) for each individual list item you can simply handle an event or two - and have a list view full of color and visually more attractive elements. The OnAdvancedCustomDrawItem event can be used to customize the drawing of individual items on the list view's canvas. Here's a quick Delphi example.
If you want to apply custom drawing (fonts, color, graphics) for each individual list item you can simply handle an event or two - and have a list view full of color and visually more attractive elements. The OnAdvancedCustomDrawItem event can be used to customize the drawing of individual items on the list view's canvas. Here's a quick Delphi example.
Opening Office Documents (Word, Excel) in TWebBrowser on Vista and Office 2007
When, in your Delphi application, you are using the TWebBrowser control to open Word (Excel) document for preview and/or print- if your application is running on Vista with Office 2007 installed you might experience weird behaviors. Documents open in a new Word instance rather than in the web browser window. Here's a quick fix.
When, in your Delphi application, you are using the TWebBrowser control to open Word (Excel) document for preview and/or print- if your application is running on Vista with Office 2007 installed you might experience weird behaviors. Documents open in a new Word instance rather than in the web browser window. Here's a quick fix.
TreeView Items - Dynamic Context (Popup) Menus
In complex tree views, you might want to display customized - dynamic context (popup) menu for each tree node. Here's how to handle the OnContextPopup event to display tree view node aware context menu.
In complex tree views, you might want to display customized - dynamic context (popup) menu for each tree node. Here's how to handle the OnContextPopup event to display tree view node aware context menu.
Impementing UnQuickSort in Delphi - How to Use the TListSortCompare function
Understanding and Using Delphi's TListSortCompare - a function used as a parameter for the Sort method of the TList class. Learn how to randomize TList items - by soting in random order!
Understanding and Using Delphi's TListSortCompare - a function used as a parameter for the Sort method of the TList class. Learn how to randomize TList items - by soting in random order!
What is "var Form1:TForm1" in Delphi Form's Unit Interface section?
When you add a new form to a Delphi project, a form and its associated unit is included in the application. There's a global variable "Form1" in the interface section - what is it used for?
When you add a new form to a Delphi project, a form and its associated unit is included in the application. There's a global variable "Form1" in the interface section - what is it used for?
