Delphi's TXmlDocument component can be used to either read (and process) an existing XML document or to construct a new, empty XML document. When using the TXMLDocument to create XML documents you might receive a nasty EOleException: This operation can not be performed with a Node of type PCDATA. The problem is in the doNodeAutoIndent option set. Here's how to have the XML formated without the do…
Introduced in Delphi 8, code folding is a feature of the Delphi IDE which lets you collapse (hide) and expand (show) your code to make it easier to navigate and read. By default, when you close and reopen your project the code that you collapsed would appear expanded - your code folding preferebces are not saved. Here's how to "fix" this.
The RTL's RenameFile changes the name of a specified file. The implementation of the RenameFile function calls the MoveFile API function. MoveFile can rename folders, therefore Delphi's RenameFile can also rename folders and not only files.
By design, all console mode applications have the same icon (depends on the Windows version). For a normal GUI application you can change the application icon using Project - Options - Application - Application Settings - Icon. For console mode applications this section is disabled - and thus all console mode application have a default icon. Here's how to specify a custom icon for a console mode Delphi application.
The TForm's OnCreate event is called when the form is created. Usually, you would write an OnCreate event handler to perform special processing when the form is created - such as setting any startup information (initial control property values, for example) for form operation. If you are using VFI you might need to have code that executes after all OnCreate event handler has been called for all inherited forms. Here's how.
An MDI application typically displays more than one document or child window within a single parent window. A question: I have two types of MDI Child forms in my application. How might I go about counting each one? One is TFrmStatus the Other is FrmChannel. How do I know how many TFrmStatus children are alive? Here's the answer.
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.
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.
On procedureal pointers and callback functions. On value parameters in function calls. On pointers and simple types. On everything.
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, ...
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.
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.
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:
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.
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 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 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.
Your challenge is to create the fastest algorithm to fill in an array with unique integer numbers.
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.
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.
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.
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 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.
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...
IsFileInUse will return true if the file is locked for exclusive access. It would fail if the file doesn't exist at all.
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, ...
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.
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...
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 :)
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.
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.
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.
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 + /.
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.
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.
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.