About Delphi Programming Tips - 2009
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
Check if an XML Conforms to the Rules of a DTD
A DTD file can be created to specify valid tags and attributes. The purpose of a DTD is to define the structure of an XML document. It defines the structure with a list of legal elements. Here's how to programmatically validate an XML against a DTD using TXMLDocument and Delphi.
A DTD file can be created to specify valid tags and attributes. The purpose of a DTD is to define the structure of an XML document. It defines the structure with a list of legal elements. Here's how to programmatically validate an XML against a DTD using TXMLDocument and Delphi.
Avoid New MDI Child Resizing Animation (and Delay) in Delphi MDI Applications
If you are creating MDI applications using Delphi, you must have noticed some "quirks" or issues that you cannot simply handle / fix from your code. When creating (to show) an MDI child an animation of resizing will take place. This animation might look ugly if the code executed during the creation of your MDI child takes some time to process. Here's how to prevent window updating when creating new MDI child forms.
If you are creating MDI applications using Delphi, you must have noticed some "quirks" or issues that you cannot simply handle / fix from your code. When creating (to show) an MDI child an animation of resizing will take place. This animation might look ugly if the code executed during the creation of your MDI child takes some time to process. Here's how to prevent window updating when creating new MDI child forms.
Passing a Simple Type Variable (Integer) for a Pointer Parameter.
On procedureal pointers and callback functions. On value parameters in function calls. On pointers and simple types. On everything.
On procedureal pointers and callback functions. On value parameters in function calls. On pointers and simple types. On everything.
TVatCalculator - Value Added Tax Calculator Implementation in Delphi
VAT or Value Added Tax is a common form of taxation in the European Union (EU). VAT is added on top of the cost of a product or service and generates revenue for a government. If you are working on an accounting-type application, you will have to deal with VAT, Gross and Net values all the time. The TVATCalculator class provides functions to help you calculate gross from net for a given vat value, net from gross, net for vat, ...
VAT or Value Added Tax is a common form of taxation in the European Union (EU). VAT is added on top of the cost of a product or service and generates revenue for a government. If you are working on an accounting-type application, you will have to deal with VAT, Gross and Net values all the time. The TVATCalculator class provides functions to help you calculate gross from net for a given vat value, net from gross, net for vat, ...
Programmatically Retrieve Your IP Address Behind a Router / Proxy / Gateway
An IP Address (Internet Protocol Address) identifies and allows computers (printers, routers) to communicate with each other on a computer network. If you need to know the WAN-IP of your router using Delphi code, you can take advantage of the "http://www.whatismyip.com" service which can reveal the IP address of machine. Here's how.
An IP Address (Internet Protocol Address) identifies and allows computers (printers, routers) to communicate with each other on a computer network. If you need to know the WAN-IP of your router using Delphi code, you can take advantage of the "http://www.whatismyip.com" service which can reveal the IP address of machine. Here's how.
Delayed Event Handling Using Event Handler Detaching
When you have an event handler which takes some time (> 0.1 sec) to run, but you don't want another click or a double click to call the event handler a second time before the first call has finished, you can detach the event handler from the event while the event handling code is being executed.
When you have an event handler which takes some time (> 0.1 sec) to run, but you don't want another click or a double click to call the event handler a second time before the first call has finished, you can detach the event handler from the event while the event handling code is being executed.
Changing the Title of a Print Dialog in Delphi
The TPrintDialog component displays a standard Windows dialog box for sending jobs to a printer. Unfortunately, the TPrintDialog does not expose the Title property. By handing the OnShow event of a print dialog you can specify the text for the dialogs title bar:
The TPrintDialog component displays a standard Windows dialog box for sending jobs to a printer. Unfortunately, the TPrintDialog does not expose the Title property. By handing the OnShow event of a print dialog you can specify the text for the dialogs title bar:
Generic Solution to Freeing Objects in Delphi's TStringList Collections
Delphi TStrings object does not own the objects you add this way. Objects added to the TStrings object still exist even if the TStrings instance is destroyed. Objects must be explicitly destroyed by the application / developer - you; or your application will leak memory. Here's a generic solution to freeing the memory used by objects stored along strings in a TStrings descendant.
Delphi TStrings object does not own the objects you add this way. Objects added to the TStrings object still exist even if the TStrings instance is destroyed. Objects must be explicitly destroyed by the application / developer - you; or your application will leak memory. Here's a generic solution to freeing the memory used by objects stored along strings in a TStrings descendant.
Programmatically Disconnect a Mapped Network Drive
When working with files and folders in your Delphi applications you might need to provide a user with an option to map a shared network path to a drive letter AND to un-map or disconnect a mapped network location. This time, we want to remove a network connection from Delphi code...
When working with files and folders in your Delphi applications you might need to provide a user with an option to map a shared network path to a drive letter AND to un-map or disconnect a mapped network location. This time, we want to remove a network connection from Delphi code...
Programmatically Execute the Map Network Drive Dialog
When working with files and folder in your Delphi applications you might need to provide a user with an option to map a network path to a drive letter. If you want to programmatically call the Map Network Drive Dialog in your Delphi application, you need to call the WNetConnectionDialog1 Window API function.
When working with files and folder in your Delphi applications you might need to provide a user with an option to map a network path to a drive letter. If you want to programmatically call the Map Network Drive Dialog in your Delphi application, you need to call the WNetConnectionDialog1 Window API function.
Convert an Amount of Microseconds into a TDateTime Value
When testing your code for speed, or when you have some lengthy function, and you need to display how much time some process took to execute, you would maybe need to take microseconds into account also. Here's a custom MicrosecondsToTime function you can use to convert an amount of microseconds into a TDateTime value and then convert to a more user friendly representation using the FormatDateTime function.
When testing your code for speed, or when you have some lengthy function, and you need to display how much time some process took to execute, you would maybe need to take microseconds into account also. Here's a custom MicrosecondsToTime function you can use to convert an amount of microseconds into a TDateTime value and then convert to a more user friendly representation using the FormatDateTime function.
Challenge: Fastest Unique Random Number Generator
Your challenge is to create the fastest algorithm to fill in an array with unique integer numbers.
Your challenge is to create the fastest algorithm to fill in an array with unique integer numbers.
Print Documents From Delphi - Print PDF, DOC, XLS, HTML, RTF, DOCX usign Delphi
If your Delphi application needs to operate on various types of files, one of the tasks you might have for your application is to allow the user of the application to print a file, whatever the file type is. Here's how to use ShellExecute API call to print any printable document to the default of user selected printer, programmatically from Delphi.
If your Delphi application needs to operate on various types of files, one of the tasks you might have for your application is to allow the user of the application to print a file, whatever the file type is. Here's how to use ShellExecute API call to print any printable document to the default of user selected printer, programmatically from Delphi.
Get Selected Tabs of a Multiselect TTabControl
Delphi's TTabControl (Win32 pallete) can be used to add a control with multiple tab settings to a form. The Multiselect property of the TTabControl specifies whether multiple tabs can be selected at the same time. Here's how to get the list of indexes or strings one can use to determine what tabs are selected when multiselect for TtabControl is set to true.
Delphi's TTabControl (Win32 pallete) can be used to add a control with multiple tab settings to a form. The Multiselect property of the TTabControl specifies whether multiple tabs can be selected at the same time. Here's how to get the list of indexes or strings one can use to determine what tabs are selected when multiselect for TtabControl is set to true.
Create Manifest for Vista UAC if Not Running Under Administrative Rights
By programmatically editing the Windows Registry, using the TRegistry object, you can you can "automagically" start your Delphi programs whenever Windows launches. On Vindows Vista, UAC will stop you from altering the Registry if a user runing the application does not have administrative rights. Learn how to create an application manifest file to request admin execution level. Automate the process of creating manifest files using a simple (source included) Delphi application.
By programmatically editing the Windows Registry, using the TRegistry object, you can you can "automagically" start your Delphi programs whenever Windows launches. On Vindows Vista, UAC will stop you from altering the Registry if a user runing the application does not have administrative rights. Learn how to create an application manifest file to request admin execution level. Automate the process of creating manifest files using a simple (source included) Delphi application.
How to Check if a given File Name is Valid using Delphi
If you have an application that operates on the file system and one of the tasks of the application is creating or manipulating files you might need to check if a given file name is valid - will Windows allow your code to save a file using the specified file name. A custom Delphi function IsFileNameValid does the validation.
If you have an application that operates on the file system and one of the tasks of the application is creating or manipulating files you might need to check if a given file name is valid - will Windows allow your code to save a file using the specified file name. A custom Delphi function IsFileNameValid does the validation.
Is Computer Joined to a Domain - Programmatically Check using Delphi
If you need to programmatically check if a machine running your application is a part of a domain you can exploit functions found in the netapi32.dll. The trick is in using the NetRenameMachineInDomain function which can be used to change the name of a computer in a domain.
If you need to programmatically check if a machine running your application is a part of a domain you can exploit functions found in the netapi32.dll. The trick is in using the NetRenameMachineInDomain function which can be used to change the name of a computer in a domain.
Convert a Drive Letter ("C") to a Drive Number (3)
I have an application that needs to "export" a set of files into a specified folder on a drive. Files can come from variuos sources: hard disk, usb disk, network drive, etc. DiskFree returns the number of free bytes on the specified drive but takes a drive number as a parameter. How to convert a drive letter to a drive numer...
I have an application that needs to "export" a set of files into a specified folder on a drive. Files can come from variuos sources: hard disk, usb disk, network drive, etc. DiskFree returns the number of free bytes on the specified drive but takes a drive number as a parameter. How to convert a drive letter to a drive numer...
Programmatically Check If File is In Use - Delphi Code
IsFileInUse will return true if the file is locked for exclusive access. It would fail if the file doesn't exist at all.
IsFileInUse will return true if the file is locked for exclusive access. It would fail if the file doesn't exist at all.
Get Enum Information using RTTI
In Delphi, enumaration types or Enum provides a way of to define a list of values. The values have no inherent meaning, and their ordinality follows the sequence in which the identifiers are listed. RTTI methods allow you to "dive" into Delphi enumerated types and values to get the values as string to get the number of elements, ...
In Delphi, enumaration types or Enum provides a way of to define a list of values. The values have no inherent meaning, and their ordinality follows the sequence in which the identifiers are listed. RTTI methods allow you to "dive" into Delphi enumerated types and values to get the values as string to get the number of elements, ...
Remove Duplicate Items in Delphi's TStringList
Many Delphi controls like TListBox, TComboBox and TMemo, for example, expose a property which is of the TStrings type: Items for TListBox, Lines for TMemo. When you fill in a string list with string items you might end up with a list holding duplicate strings - more than one item with the 'same' string value.
Many Delphi controls like TListBox, TComboBox and TMemo, for example, expose a property which is of the TStrings type: Items for TListBox, Lines for TMemo. When you fill in a string list with string items you might end up with a list holding duplicate strings - more than one item with the 'same' string value.
Edit and Display Boolean Fields using a CheckBox in Delphi's DBGrid
The article CheckBox inside a Delphi DBGrid provides one method of using a check box control to edit and display values for boolean fields. Rene suggest an easier approach needing only two even handlers: OnCellClick and OnCustomDrawCell for your DBGrid control...
The article CheckBox inside a Delphi DBGrid provides one method of using a check box control to edit and display values for boolean fields. Rene suggest an easier approach needing only two even handlers: OnCellClick and OnCustomDrawCell for your DBGrid control...
5 Facts you Did Not Know about Delphi
Have you ever created your own class in Delphi? Why not? It's easy. You did? Do you know that you can add your own properties and methods to a TEdit, or any class, without creating a new component? Here are some Delphi OOP specific tricks and goodies you should know of, to make your coding even more joyful :)
Have you ever created your own class in Delphi? Why not? It's easy. You did? Do you know that you can add your own properties and methods to a TEdit, or any class, without creating a new component? Here are some Delphi OOP specific tricks and goodies you should know of, to make your coding even more joyful :)
Cycle Enum Values in Delphi
An enumerated type in Delphi, or enum lets you define a list of values. The values have no inherent meaning, and their ordinality follows the sequence in which the identifiers are listed. If you want to cycle through the possible set of values defined by an enum, you can implement a so called "round robin" algorithm.
An enumerated type in Delphi, or enum lets you define a list of values. The values have no inherent meaning, and their ordinality follows the sequence in which the identifiers are listed. If you want to cycle through the possible set of values defined by an enum, you can implement a so called "round robin" algorithm.
Delphi Interceptor Classes
Have you ever needed for a specific Delphi control, like a TButton, to have just one more property or a method that is a "must have" for your current application? What most beginners do not know is that they *can* create their own custom controls derived from the existing VCL set by creating a, so called, interceptor class that has the same name as the class being extended.
Have you ever needed for a specific Delphi control, like a TButton, to have just one more property or a method that is a "must have" for your current application? What most beginners do not know is that they *can* create their own custom controls derived from the existing VCL set by creating a, so called, interceptor class that has the same name as the class being extended.
Force Display of ToolButton's PopupMenu in its OnClick Event
When a tool button is placed on a toolbar and tool button's Style is set to tbsDropDown, the button displays a down-pointing arrow. When you click the arrow the asociated popup is displayed. When you click on the actual button the popup is not displayed. Here's how to manually display a buttons pop-up menu on its OnClick event.
When a tool button is placed on a toolbar and tool button's Style is set to tbsDropDown, the button displays a down-pointing arrow. When you click the arrow the asociated popup is displayed. When you click on the actual button the popup is not displayed. Here's how to manually display a buttons pop-up menu on its OnClick event.
Keyboard Shortuct to Comment a Single Line of Code
Proper commenting of your Delphi code will increase your code readability - using understandable description of what your code is doing. Sometimes you need to comment a line of code just to see how the application will live without it: comment it fast, uncomment it fast. There's a handy keyboard shortcut to comment a single line of code: CRL + /.
Proper commenting of your Delphi code will increase your code readability - using understandable description of what your code is doing. Sometimes you need to comment a line of code just to see how the application will live without it: comment it fast, uncomment it fast. There's a handy keyboard shortcut to comment a single line of code: CRL + /.
Convert Roman Number to Arabic Numbers using Delphi
An example of a different numeral system is the system of Roman numerals, which could represent all the numbers from 1 to 1,000,000 using only seven symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000. Here's how to convert a roman number in its arabic representation.
An example of a different numeral system is the system of Roman numerals, which could represent all the numbers from 1 to 1,000,000 using only seven symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000. Here's how to convert a roman number in its arabic representation.
Override Global Screen.Cursor Change for a Cancel type Button
When you want to run a lengthy process in your application that needs no user iteraction, you can change the value of the Screen.Cursor property to have the mouse cursor display as a hourglass, for example, while the process is being executed. Here's how to have a cancel type of button that can override the global cursor shape.
When you want to run a lengthy process in your application that needs no user iteraction, you can change the value of the Screen.Cursor property to have the mouse cursor display as a hourglass, for example, while the process is being executed. Here's how to have a cancel type of button that can override the global cursor shape.
Sort a Generic List using Anonymous Comparer Method
Delphi 2009 adds Generics and Anonymous methods to the Delphi language. When you have objects in some list - one of a commonly required tasks is to sort the list: either ascending, descending or using some list-specific algorithm. Using anonymous methods you can apply the sorting function directly "inline" with the call to the Sort method.
Delphi 2009 adds Generics and Anonymous methods to the Delphi language. When you have objects in some list - one of a commonly required tasks is to sort the list: either ascending, descending or using some list-specific algorithm. Using anonymous methods you can apply the sorting function directly "inline" with the call to the Sort method.
