Many Lists Little Space

List boxes take up a lot of screen space, and they offer a fixed selection. That is, a user can choose only among the items in the list box and cannot make any choice that the programmer did not specifically foresee. You can solve both problems by using a ComboBox control. A combo box is similar to an edit box, and you can often enter some text in it. It is also similar to a list box, with a drop-down arrow that displays a list box. Even the name of the control suggests that it is a combination...

Installing new DLL Wizards

Technically, new Wizards come in two different forms. Wizards may be part of components or packages, and in this case are installed the same way you install a component or a package. Other Wizards are distributed as stand-alone DLLs. In this case you should add the name of the DLL in the Windows Registry under the key Software Borland Delphi x.0 Experts. Simply add a new string key under this, choose a name you like (it doesn't really matter) and use as text the path and filename of the Wizard...

Owner Draw Menu Items

In Windows, the system is usually responsible for painting buttons, list boxes, edit boxes, menu items, and similar elements. Basically these controls know how to paint themselves. As an alternative, however, the system allows the owner of these controls, generally a form, to paint them. This technique, available for buttons, list boxes, combo boxes, and menu items, is called owner-draw. Actually in Delphi the situation is slightly more complex. The components can take care of painting...

Adding New Form Templates to the Object Repository

Just as you can add new project templates to the Object Repository, you can also add new form templates. Simply move to the form you want to add, right-click on it, and select Add to Repository from the local menu. In the dialog box that appears (see below), you can choose which form of the current project should be added to the Repository, and set the title, description, author, page, and icon, as usual. Once you have set these elements and clicked on OK, the form is added to the proper page...

The Empty Project Template

When you start a new project, it automatically opens a blank form, too. If you want to base a new project on one of the form objects or wizards, this is not what you want. To solve this problem, you can add an Empty Project template to the Gallery. The steps required to accomplish this are simple 2 Remove its only form from the project. 3 Add this project to the templates, naming it Empty Project. When you select this project from the Object Repository, you gain two advantages. You have your...

The Phrasesl Example

Delphi Programming Examples

If you've ever tried to learn a foreign language, you probably spent some time repeating the same silly and useless phrases over and over. Probably the most typical, when you learn English, is the infamous The book is on the table. To demonstrate radio buttons, the Phrasesl example creates a tool to build such phrases by choosing among different available options. This form is quite complex. If you rebuild it, remember that you must place the GroupBox components first and the radio buttons...

Working with the Form Designer

Designing forms is the core of visual development in the Delphi environment. Every component you place on a form and every property you set is stored in a file describing the form (a DFM file) and has some effect on the source code associated with the form (the PAS file). When you start a new, blank project, Delphi creates an empty form, and you can start working with it. You can also start with an existing form (using the various templates available), or add new forms to a project. A project...

Adding New Application Templates

Custom Text Cadence

Adding a new template to Delphi's Object Repository is as simple as using an existing template to build an application. When you have a working application you want to use as a starting point for further development of two or more similar programs, you can save the current status to a template, ready to use later on. Although Borland calls everything you can put in the Object Repository an object, from an object-oriented perspective this is far from true. For this reason I call the schemes you...

The Project File

In addition to the two files describing the form (PAS and DFM), a third file is vital for rebuilding the application. This is the Delphi project file (DPR). This file is built automatically, and you seldom need to change it, particularly for small programs. If you do need to change the behavior of a project, there are basically two ways to do so You can use the Delphi Project Manager and set some project options, or you can manually edit the project file directly. C Program Files Borland...

The View Menu

The View pull-down menu combines the features you usually find in View and Window menus. There is no Window menu, because the Delphi environment is not an MDI application. Most of the View commands can be used to display one of the windows of the Delphi environment, such as Project Manager, the Breakpoints list, or the Components command. Some of these windows are used during debugging others when you are writing code. Most of these windows will be described later in this chapter. It is...

Setting Project Options

From the Project Manager (or from the Project menu), you can invoke the Project Options dialog. The first page of Project Options, named Forms, lists the forms that should be created automatically at program startup (the default behavior) and the forms that are created manually by the program. You can easily move a form from one list to the other. The next page, Application, is used to set the name of the application and the name of its Help file, and to choose its icon. Other Project Options...

The Project Manager

When a project is loaded, you can choose the View Project Manager command to open a project window. The window lists all of the forms and units that make up the current project. The Project Manager's local menu allows you to perform a number of operations on the project, such as adding new or existing files, removing files, viewing a source code file or a form, and adding the project to the repository. Most of these commands are also available in the toolbar of this window. Progrann Files...

The Files Produced by the System

As you have seen, Delphi produces a number of files for each project, and you should know what they are and how they are named. There are basically two elements that have an impact on how files are named the names you give to a project and its forms, and the predefined file extensions used by Delphi for the files you write and those generated by the system. The great advantage of Delphi over other visual programming environments is that most of the source code files are plain ASCII text files....

Compiling a Project

There are several ways to compile a project. If you run it (by pressing F9 or clicking on the toolbar icon), Delphi will compile it first. When Delphi compiles a project, it compiles only the files that have changed. If you select Compile Build All, instead, every file is compiled, even if it has not changed. This second command is seldom used, since Delphi can usually determine which files have changed and compile them as required. The only exception is when you change some project options. In...

The Database Form Wizard

In this section, I'll show you a quick example of the use of the Database Form Wizard, but I won't describe the application we build in detail. In this example, we'll build a database program using some of the data already available in Delphi. Note that you have to create a project first, and then start the Database Form Wizard. So you usually end up with two forms, unless you remove the original form from the project. Fortunately, one of the Wizard's options, displayed at the end, lets you...

Creating Menus and Menu Items Dynamically

When you want to create a menu or a menu item dynamically, you can use the corresponding components, as I've done in the MenuOne3 example. As an alternative, you can also use some global functions available in the Menus unit function NewMenu Owner TComponent const AName string Items array of TMenuItem TMainMenu function NewPopupMenu Owner TComponent const AName string Alignment TPopupAlignment AutoPopup Boolean Items array of TMenuitem TPopupMenu function NewSubMenu const ACaption string hCtx...

Disabling Menu Items and Hiding Pull Down Menus

The other three new menu items of the MenuOne2 example are used to disable or hide other menu items or pull-down menus. The View Fixed Font command is used to disable the Options Font menu item procedure TFormColorText.FixedFont1Click Sender TObject begin ToggleCheck FixedFont1 Font1.Enabled not Font1.Enabled his method calls the custom ToggleCheck procedure I've written as a shortcut to the code for toggling the check mark something all these three final menu commands do . Here is its simple...

The Standard Structure of a Menu

If you've used Windows applications for some time, you have certainly noticed that the structure of an application's menu is not an invention of its programmers. There are a number of standard Windows guidelines describing how to arrange the commands in a menu. You can infer most of these rules by looking at the menus of some of the best-selling applications. An application's menu bar should start with a File pull-down, followed by Edit, View, and then some commands specific to the application....

The Application Wizard

Another interesting although less powerful tool is the Application Wizard. You can activate it from the Projects page of the Object Repository. The Application Wizard allows you to create the skeleton of a number of different kinds of applications, depending on the options you select. Your application can contain a menu bar by checking one or more of the following standard Windows menus The file menu contains items such as Open, Save and Exit. I- Edit menu The Edit menu contains Undo, Cut, Copy...

Responding to Events

When you press the mouse button on a form or a component, Windows informs your application of the event by sending it a message. Delphi responds by receiving an event notification and calling the appropriate event-handler method. As a programmer, you can provide several of these methods, both for the form itself and for the components you have placed in it. Delphi defines a number of events for each kind of component. The list of events for a form is different from the list for a button, as you...

The Project Menu

The next pull-down menu, Project, has commands to manage a project and compile it. Add to Project and Remove from Project are used to add forms or Pascal source code files to a program and to remove them from a project. The Enterprise version of Delphi includes two more commands, Web Deploy and Web Deploy, which are not available in the other editions. These features related with ActiveX and ActiveForms, some now obsolete Microsoft web technologies. The Compile command builds or updates the...

Removing a Selected String from the Other List

Once this part of the program works, we have two more problems to solve We must remove the selected string from the other list box to avoid using the same term twice in a phrase , and we must write the code for the click event on the button. The first problem is more complex, but I'll address it immediately since the solution of the second problem will be based partially on the code we write for the first one. Our aim is to delete from a list box the item currently selected in the other list...

The Dialog Wizard

Delphi's Dialog Wizard is a simple Wizard provided mostly as a demo, with its own source code. From the code of this Wizard, in theory you should be able to learn how to build other Wizards of your own. However, you can still use the Dialog Wizard as a tool to build two kinds of dialog boxes simple dialog boxes and multiple-page dialog boxes based on the Windows PageControl component. If you choose the simple dialog box, the Wizard will jump to the third page, where you can choose the button...

Handling the Input Focus

What's important for our example is that each time a component receives or loses the input focus, it receives a corresponding event indicating that the user either has reached OnEnter or has left OnExit the component. So we can add some methods to the form to take control over the input focus and display this information in a label or a status bar. Besides three edit boxes, the form has also some labels indicating the meaning of the three edit fields First name, Last name, and Password . For...

Delphi Wizards

Besides copying or using existing code, Delphi allows you to create a new form, application, or other code files, using a Wizard. Wizards or Experts allow you to enter a number of options and produce the code corresponding to your choices. One of the most important predefined wizards is the Database Form Wizard, which you can activate using the Database Form Wizard menu item or the icon in the Forms page of the Object Repository. There are also some other simple Wizards in Delphi, such as the...

Working with a CD Drive

In addition to audio and video files, the MCI interface is generally used to operate external devices. There are many examples, but the most common MCI device connected to a PC is probably a CD-ROM drive. Most CD-ROM drives can also read audio CDs, sending the output to an external speaker or a sound card. You can use the MCI interface and the Media Player component to write applications that handle such a device. Basically, you need to set the DeviceType property to dtCDAudio, making sure no...

The Code Generated by the Menu Designer

Once you have built this menu, take a look at the list of components displayed by the Object Inspector, or open the DFM file with the textual description of the form, which will also contain a textual description of the menu structure. Here is the portion of the textual description of the form related to the menu and its items object MainMenul TMainMenu object Filel TMenuItem Caption ' amp File' object Exitl TMenuItem Caption 'E amp xit' OnClick ExitlClick object Viewl TMenuItem Caption ' amp...

The Search Menu

The Search menu has some standard commands, too, such as Search and Replace, and the Find in Files command you can see its dialog box here The Find in Files command allows you to search for a string in all of the source code files of a project, all the open files, or all the files in a directory optionally including its subdirectories , depending on the radio button you check. The result of the search will be displayed in the message area at the bottom of the editor window. You can select an...

Adding a Title

Before we run the application, let's make a quick change. The title of the form is Form1. For a user, the title of the main window stands for the name of the application. Let's change Form1 to something more meaningful. When you first open Delphi, the Object Inspector window should appear on the left side of the form if it doesn't, open it by choosing View Object Inspector or pressing the F11 key The Object Inspector shows the properties of the selected component. The window contains a tab...

The LabelCo Example

The first thing to do is to place a big label in the form and enter some text. Write something long. I suggest you set the WordWrap property to True, to have several lines of text, and the AutoSize property to False, to allow the label to be resized freely. It might also be a good idea to select a large font, to choose a color for the font, and to select a color for the label itself. Push the buttons below to change the color of the text or the background of this Ijffiel, and its alignment To...

Code Templates

Unlike the Code Completion Wizard, the Code Templates Wizard must be activated manually. You can do this by typing Ctrl J to show a list of all of the templates. More often, you'll first type a keyword, such as if or array, and then press Ctrl J, to activate only the templates starting with those letters. For some keywords Borland has defined multiple templates, all starting with the keyword name such as ifA and ifB . So if you press the keyword and then Ctrl J, you'll get all the templates...

The File Menu

Our starting point is the File pull-down menu. The structure of this menu has kept changing from version to version of Delphi, with menu items for handling projects moving away, and specific commands to create new designer for example data modules coming and going. Still, this menu contains commands that operate on projects and commands that operate on source code files. The File menu structure has changed in Delphi 6, with the File New submenu, and the text here has not been updated...

Creating a Rich Editor

Although you can choose a font in the Notes program, all of the text you have written will have the same font. Windows has a control that can handle the Rich Text Format RTF . A Delphi component, RichEdit, encapsulates the behavior of this standard control. You can find an example of a complete editor based on the RichEdit component among the examples that ship with Delphi. The example is named RichEdit, too . Here, we'll only change the previous program slightly by replacing the Memo component...

The Alignment Palette

The last tool related to form design is the Alignment palette. You can open this palette with the View menu's Alignment Palette command. As an alternative, you can choose the components you want to align, and then issue the Align command from the local menu of the form. The Alignment palette features a number of commands to position the various controls, center them, space them equally, and so on. To see the effect of each button, simply move the mouse over the window and look at the fly-by...

The Paragraph Menu

Compared to the File menu, the other pull-down menus of this example are simpler. The code of the Paragraph menu is based on some properties of its items. Here is their textual description from the DFM file object Paragraph1 TMenuItem Caption ' amp Paragraph' object LeftAligned1 TMenuItem Caption ' amp Left Aligned' Checked True GroupIndex 1 RadioItem True OnClick RightAligned1Click end object RightAligned1 TMenuItem Caption ' amp Right Aligned' GroupIndex 1 RadioItem True OnClick...

The Tools Menu

The Tools menu simply lists a number of external programs and tools, just to make it easier to run them. You can use the Tools command to configure and add new external tools to the pull-down. Besides simply running a program, you can pass some parameters to it. Simple parameter lists can be passed directly on the command line, while complex ones can be built by clicking the Macros button in the lower part of the Tool Properties dialog box. The Tools menu also includes a command to configure...

The Textual Description of the Form

As I've just mentioned, along with the PAS file containing the source code, there is another file describing the form, its properties, its components, and the properties of the components. This is the DFM file, a binary or text file this latter option has been introduced with Delphi 5 . Whatever the format, if you load this file in the Delphi code editor, it will be converted into a textual description. This might give the false impression that the DFM file is indeed a text file, but this can...

Using a Check ListBox Component

Checklistbox Delphi Example

A further extension to the Phrases example is the use of the CheckListBox component, a component originally introduced by Borland in Delphi 3. This is basically a list box with a custom output or an owner-draw list box, to use the proper technical term . Each item of the list is preceded by a check box. A user can select a single item of the list, but can also click on the check boxes to toggle their status. If the component has the AllowGrayed property set to True, then each check box can be...

Bitmap Menu Items

Instead of placing a bitmap close to a menu item to indicate the status of its Checked property, as we've done in the previous section, you can actually replace the text of a menu item with a bitmap. In specific cases this can make an application easier to use. BitMenu is a very simple program. I've put a shape in the middle of a form and added a menu to set the kind of shape rectangle, rounded rectangle, or ellipse , and its color. Here is the textual description of the form and its menu...

Using Radio Menu Items

In addition to using check marks, in Windows you can use radio menu items. These provide not only a different user interface, but also different behavior basically simpler code, since the system does some of the work for us . A notable example of this new user-interface feature is the View menu of the Windows Explorer. In Delphi, simply set the RadioItem property of a MenuItem component to True and you get the new check mark for the item. If you set this property for several consecutive menu...

Shortcut Keys and Hotkeys

A common feature of menu items is that they contain an underlined letter, generally called a hotkey. This letter, which is often the first letter of the text, can be used to select the menu using the keyboard. Pressing Alt plus the underlined key selects the corresponding pull-down menu. By pressing another underlined key on that menu, you issue a command. Of course, each element of the menu bar must have a different underlined character. The same is true for the menu items on a specific...

Customizing the Menu Check Mark

As I've just mentioned, there are a number of ways to customize a menu in Windows. In this section, I'm going to show you how you can customize the check mark used by a menu item, using two bitmaps of your own. This example, NewCheck, involves using bitmaps and calling a Windows API function. First I should explain why we need two bitmaps, not just one. If you look at a menu item, it can have either a check mark or nothing. In general, however, Windows uses two different bitmaps for the checked...

Every Box Has a Beep

To show you the capabilities of the MessageBeep API function, I've prepared a simple example, Beeps. The form of this example has a RadioGroup with some radio buttons from which the user can choose one of the five valid constants of the MessageBeep function. Here is the definition of the RadioGroup, from the textual description of the form object RadioGroupl TRadioGroup Caption 'Parameters' ItemIndex 0 Items.Strings 'mb_IconAsterisk' 'mb_IconExclamation' 'mb_IconHand' 'mb_IconQuestion' 'mb_Ok'...

Allowing Multiple Selections

A list box can allow the selection of either a single element or a number of elements. We make this choice in setting up a list box by specifying the value of its Multiple property. As the name implies, setting Multiple to True allows multiple selections. There are really two different kinds of multiple selections in Windows and in Delphi list boxes multiple selection and extended selection. In the first case a user selects multiple items simply by clicking on them, while in the second case the...

The Delphi Toolbar

After you have used Delphi for a while, you'll realize that you use only a small subset of the available commands frequently. Some of these commands are probably already on the toolbar Borland's name for a toolbar some are not. If the commands you use a lot are not there, it's time to customize the toolbar so that it really helps you to use Delphi more efficiently. An alternative to using the toolbar is to use shortcut keys. Although you must remember some key combinations to use them, shortcut...

Sophisticated Input Schemes

In the last example, we saw how an Edit component can be customized for special input purposes. The components could really accept only numbers, but handling complex input schemes with a similar approach is not straightforward. For this reason, Borland has supplied a ready-to-use masked edit component, an edit component with an input mask stored in a string. For example, to handle numbers of no more than five digits, we can set the EditMask property to 99999. The character 9 stands for...

Short Circuit Evaluation

The expression if not Modified or SaveChanges then requires some explanation. By default, Pascal performs what is called short-circuit evaluation of complex conditional expressions. The idea is simple if the expression not Modified is true, we are sure that the whole expression is going to be true, and we don't need to evaluate the second expression. In this particular case, the second expression is a function call, and the function is called only if Modified is True. This behavior of or and...

Donations

I'll probably set up an account on one of those donation contribution systems, to let people who have enjoyed the book and learned from it, particularly if programming is their job and not a hobby and they do it for profit, contribute to its development. No extra material is offered to those donating to the book fund, only because I want to let anyone particularly students and people leaving in poor countries benefit from the availability of this material. Information will be available on the...

Entering Numbers

We saw in the previous example that it is very easy to use an Edit component to ask the user to input some text, although it must be limited to a single line. In general, it's quite common to ask users for numeric input, too. To accomplish this, you can use the MaskEdit component in the Additional page of the Components palette or simply use an Edit component and then convert the input string into an integer, using the standard Pascal Val procedure or the Delphi IntToStr function. This sounds...

Generic OnEnter Event Handler

The problem with this code is that we have to write four different OnEnter event handlers, copying four strings to the text of the StatusBar component. To add more edit boxes to the example, you would need to add more event handlers, copying the code over and over. And if you wanted to provide a slightly different output for example, by changing the output of the StatusBar allowing for multiple panels , you would need to change the code many times. The alternative solution is to write a single...

The Scroll Color Example

The ScrollC example the name stands for scroll color has a simple form with three scroll bars and three corresponding labels, a track bar with its own label, and some shape components to show the current color. Each scroll bar refers to one of the three fundamental colors, which in Windows are red, green, and blue RGB . Each label displays the name of the corresponding color and the current value. Scroll bars have a number of peculiar properties. You can use Min and Max to determine the range...