VCL Button Types

VCL contains several types of buttons that you can use in your applications. Although not all of them are based on the standard Windows button control, I will still address all the button types here. Before you look at the specific button components, though, let's cover some of the basics. NOTE When setting a button's Caption property, use the ampersand (& ) just as you would when setting the Caption property of menu items. The character after the ampersand will be underlined and will be the...

Source Code Units

Earlier I mentioned that most applications of any size have several source files, which are called units. Each time you create a new form, Delphi does the following Creates a unit (.pas file) for the class definition Adds the new form information to the project source Initially Delphi assigns a default name of Form1 to the form and Unit1.pas to the form's unit. The second form created for the project would have a default name of Form2, and so on. Each time you create a new form, Delphi creates...

The Cursor Property

The Cursor property controls the cursor that is displayed when the user moves the mouse cursor over the component. Windows automatically changes cursors for some components. For example, Windows changes the cursor to an I-beam when the cursor is moved over an Edit, Memo, or RichEdit component. To let Windows manage the cursor, leave the Cursor property set to crDefault. If you have specialized windows (components), you can specify one of the other cursors. When the mouse is moved over that...

Aligning Components

Regardless of whether you have the Snap to Grid option turned on, you sometimes need to align components after placing them. Aligning components could mean aligning several components along a common edge, centering components on the form, or spacing components. There are two ways to align components Use the Alignment palette and Alignment dialog box. Modify a component's Align property. The following sections explain these two methods. NOTE You might have noticed the Alignment property for some...

The Component Selector

Normally, you select a component by clicking the component on a form. The Component Selector provides an alternative way of selecting a component to view or modify. The Component Selector is a drop-down combo box that is located at the top of the Object Inspector window. NOTE Usually the quickest way to select a component is by clicking the component on the form. Choosing the component from the Component Selector is convenient if the component you are looking for is hidden beneath another...

Creating a Main Menu

The Menu Designer enables you to quickly build any menu. The menu structure for a main menu consists of a MainMenu component, which is represented by the VCL class TMainMenu. Each item on the menu is a MenuItem component that is encapsulated in the TMenuItem class. You don't need to be too concerned about the intricacies of how these classes work together because the Menu Designer makes creating menus easy. With that brief overview, let's add a main menu to the ScratchPad application. The first...

Compiling Resource Files

After you create a resource file, you need to compile it with the resource compiler. You can do this in one of two ways Compile the resource file manually from the command line. Add a batch file target to your project group. Either way you end up with a .res file that you link to your application (I'll discuss that in just a bit). Project groups are discussed in detail tomorrow. To compile a resource file from the command line, simply open a command prompt box in Windows and enter a line...

Files Used in Delphi Projects

Delphi manages a project through the use of several support files. To illustrate, let's create a simple application to get a look at some of what goes on when Delphi builds an executable file for your program. Perform the following steps 1. Before you begin, create a fresh directory on your hard drive. (You can name the directory anything you like.) 2. First choose File Close All from the main menu so that you are starting from scratch. Now choose File New Application from the main menu. A...

PopUp Menus Context Menus

I am not quite done with the discussion of menus. In Delphi, you can create pop-up menus as easily as you can a main menu. A nice feature of Delphi is that you can assign a particular pop-up menu to a component via the component's PopupMenu property. When the cursor is placed over the component and the secondary mouse button is clicked, that pop-up will automatically be displayed. Writing event handlers for pop-up menus is exactly the same as writing event handlers for main menus. A common...

Object Pascal Language Overview

Before you can learn about the RAD features of Delphi, you need to learn the basics of the Object Pascal language. This part of the book will probably not be the most exciting for you, but you need a basic understanding of Object Pascal before you move on. It would be nice if presenting the Object Pascal language could be handled sequentially. That's not the case, though, because all the features you will learn about are intertwined. I'll take the individual puzzle pieces one at a time and...

The for Loop

The for loop is probably the most commonly used type of loop. It takes two parameters the starting value and ending value. If the loop is to count up, the to keyword is used. If the loop is to count backward, then the downto keyword is used. for initial_value to end_value do begin statements The for loop repeatedly executes the block of code indicated by statements until the ending value end_value is reached. The state of the loop is initialized by the statement initial_value. The variable...

Qa

Q Must I use a project group even if I have just one project A No. You don't need a project group for a single project. You can use the default project group instead. Q When I start my application, one of my dialog boxes is displayed instead of my main form. What gives A You have accidentally set the main form for the application to be the dialog form. Go to the Project Options dialog box, click the Forms tab, and select your main form from the Main Form combo box on the top of the page. Run...

Experimenting with Dockable Windows

Rather than trying to put the relationships between the various windows into words, I think an exercise is in order. I'll start with the most basic docking operations and move to the more complex. This exercise doesn't take long and should prove very enlightening. Here goes 1. Create a new application and switch to the Code Editor. Notice the Code Explorer is docked to the left side of the Code Editor. 2. Click the grip at the top of the Code Explorer window and drag to the right. Notice that...

The Memo Component

The Memo component encapsulates a multiline edit control. The Lines property is the most significant property in a Memo component. As I mentioned earlier in the discussion on TStrings, the Lines property enables you to save the contents of the Memo component to disk, load the Memo with text from a file, or access the memo's lines individually. The ScrollBars property is unique to the Memo component. This property enables you to specify whether your component has a horizontal scrollbar, a...

The Properties Page

The Properties page of the Object Inspector displays all the design-time properties for the currently selected control. The Properties page has two columns The Property column is on the left side of the Properties page and shows the property name the Value column is on the right side of the Properties page and is where you type or select the value for the property. If the selected component has more properties than will fit in the Object Inspector, a scrollbar will be provided so that you can...

Creating and Using Project Groups

Project groups are a great benefit for complex projects, but using project groups is not mandatory. You don't have to use project groups with every project. The Project Manager has a default project group called ProjectGroupl that is used when you don't specifically open or create a project group. Try this 1. Choose File Close All to close any open projects or project groups. 2. Choose File New Application to create a new application. 3. Choose View Project Manager to display the Project...

The Inherit Button

The Inherit method of usage is similar to Copy, but with one important distinction The new object is still tied to the base object. If you modify the base object, the newly created object will be updated to reflect the changes made to the base object. The inverse is not true, however. You can modify the new object without it having any effect on the base object. To illustrate this type of object usage, consider the following scenario Frequently, information managers create a spreadsheet in a...

Comments in Code

Before getting into the Pascal language in detail, let me talk briefly about commenting code. Comments are lines of text in your source code that are there for documentation purposes. Comments can be used to describe what the code does, to supply copyright information, or simply to make a note to yourself or other programmers. Comments can be designated in as many as three different ways. The following are all valid comments lines Don't forget to free this memory Copyright c TurboPower Software...

Writing Code for the File Open and File Save As Menu Items

Now you are ready to write the code to implement the File Open and File Save As menu items. Delphi provides a slick way of writing menu handlers with a minimum amount of fuss. You haven't created the MDI child form yet, but you know enough about it to write the code for the menu handlers. Keep in mind that the application won't compile until you create the MDI child form. Here you go 1. On the main form, choose File Open from the menu. An event handler is created for that menu item and the Code...

The Breakpoint List Window

The Delphi IDE keeps track of the breakpoints you set. These breakpoints can be viewed through the Breakpoint List window. To view the breakpoint list, choose View Debug Windows Breakpoints from the main menu. The Breakpoint List window is displayed as shown in Figure 10.1. FIGURE 10.1. The Breakpoint List window. The Breakpoint List window has four columns The Filename Address column shows the filename of the source code unit in which the breakpoint is set. The Line Length column shows the...

The Bit Btn Component

The BitBtn component is a perfect example of how a component can be extended to provide additional functionality. In this case, the standard Button component is extended to enable a bitmap to be displayed The BitBtn component has several properties in addition to what the Button component provides. All these properties work together to manage the bitmap on the button and the layout between the bitmap and the button's text. They are explained in the following sections. The Glyph Property The...

Step Over and Trace Into

When you stop at a breakpoint, you can do many things to determine what is going on with your code. You can set up variables to watch in the Watch List, inspect objects with the Debug Inspector, or view the call stack. You can also step through your code to watch what happens to your variables and objects as each code line is executed. As you continue to step through your code, you will see that the line in your source code to be executed next is highlighted...

Result False

Standard response if all went well. if Result then function TAirplane.GetStatus var StatusString string Integer begin StatusString Format s, Altitude d, Heading d, Speed d', Name, Altitude, Heading, Speed Result Status end function TAirplane.GetStatus Integer begin function TAirplane.GetSpeed Integer begin function TAirplane.GetHeading Integer begin function TAirplane.GetAltitude Integer begin function TAirplane.GetName string begin ANALYSIS Look first at the class declaration in the interface...

The Version Info Page

The Version Info page enables you to set the version information for your applications. Version information is stored in your program's EXE file or in a DLL or ActiveX file. It is used by installation programs to determine whether a file being installed is older or newer than the file it is replacing. Version info has other uses as well. You can view the version information for a file from Windows Explorer. Just right-click the file and choose Properties from the Explorer context menu. When the...

Setting the Tab Order

New Term The tab order refers to the order in which components receive input focus when the user presses the Tab key on the keyboard. Delphi forms automatically support component navigation using the Tab key. This means that you can move forward from component to component using Tab and backward using Shift Tab. NOTE There are two types of visual components. Windowed components are components that accept keyboard focus, which means that the component can be clicked with the mouse or tabbed to...

Table lists the Modal Result constants that VCL defines Table Vcl Modal Result Constants

NOTE You don't have to use one of the predefined ModalResult constants for your buttons you can use any value you like. Let's say, for example, you have a custom dialog box that could be closed by using a variety of buttons. You could assign a different ModalResult value to each button 100, 150, and 200, for example , and you would then know which button closed the dialog box. Any nonzero number is valid, up to the maximum value of an Integer. The book's code contains a program called ButtnTst...

The Find and Replace Dialog Boxes

The Find and Replace dialog boxes provide users the capability to enter text to search for and text to replace the found text with, and a variety of search and replace options. The Find dialog box is encapsulated in the VCL component FindDialog, and the Replace dialog box is represented by the ReplaceDialog component. The Replace dialog box, which contains everything found on the Find dialog box plus the extra replace features, is shown in Figure 7.13. FIGURE 7.13. The Replace dialog box. Major...

The Delphi Main Menu and Toolbar

The Delphi main menu has all the choices necessary to make Delphi work. Because programming in Delphi is a highly visual operation, you might not use the main menu as much as you might with other programming environments. Still, just about anything you need is available from the main menu if you prefer to work that way. I'm not going to go over every item on the main menu here because you will encounter each item as you work through the next several chapters. The Delphi toolbars provide a...

Local Functions and Procedures

A local function or procedure is a subroutine that is contained within another subroutine. Here's an example procedure TForm1.Button1Click Sender TObject var X Integer A local procedure. procedure Test begin Memo1.Lines.Add Local Function, X IntToStr X end begin Memo1.Lines.Add Main Function, X IntToStr X Test end Note that the procedure called Test is contained within the var section of the Button1Click procedure. A procedure declared in this way is called a local procedure because it is local...

The Speed Button Component

The SpeedButton component was designed to be used with the Panel component to build toolbars. It is different from the Button and BitBtn components in that it is not a windowed component. This means that a speed button cannot receive input focus and cannot be tabbed to. On the other hand, the SpeedButton component has several features in common with the BitBtn component. The way in which the Glyph property is handled by the SpeedButton component is exactly the same as with the BitBtn component,...

Dialog Boxes the Delphi

In Delphi, dialog boxes are simply another form. You create a dialog box just like you do a main window form or any other form. To prevent the dialog box from being sized, you can change the BorderStyle property to bsDialog or bsSingle. If you use bsDialog, your dialog box will have only the close box button on the title bar, which is traditional for dialog boxes. Other than that, you don't have to do anything special to get a form to behave like a dialog box. All Delphi forms have tabbing...

Dock Sites

You can't talk about dockable windows without talking about dock sites. Sure, you can undock a window and drag it around on the screen, dropping it wherever you want. That just makes for a bunch of scattered windows all over your screen, though. In order for dockable windows to make sense, you have to have a place to dock them. In the Delphi IDE that usually means the Code Editor window. The Code Editor has three dock sites. One dock site is along the left side of the Code Editor window. This...

Creating a Dialog Form

You can now add an About box to the multiple-forms project you created earlier. If you don't have that project open, choose File Open Project from the main menu or click the Open Project button on the toolbar and locate the file. You should have saved it with the project name of Multiple. TIP Delphi keeps a list of the files and projects you have used most recently. Choose File Reopen to view the MRU most recently used list. The MRU list is divided into two parts. The top part shows the...

Creating Applications with the Application Wizard

The Application Wizard is a useful tool that can help you quickly set up the shell of an application. To create a new application using the Application Wizard, choose File New from the main menu. When the Object Repository appears, click the Projects tab and then double-click the Application Wizard icon. NOTE The New Application item on the main menu creates a new application based on the current default project setting. It doesn't start the Application Wizard as you might expect. Let's walk...

Styles [fsBold fsItalic

You don't specifically have to create a TFontStyles variable to change a font's style. You can just work with the property directly--for example, Memo.Font.Style Memo.Font.Style fsBold, fsItalic A set is declared using the set keyword. The TFontStyles property is declared in the VCL source file GRAPHICS.PAS like this TFontStyle fsBold, fsItalic, fsUnderline, fsStrikeOut TFontStyles set of TFontStyle The first line here declares an enumeration type called TFontStyle. An enumeration is a list of...

Setting and Clearing Breakpoints

To set a breakpoint, click in the editor window's gutter to the left of the line on which you want to pause program execution the gutter is the gray margin along the Code Editor window's left edge . The breakpoint icon a red circle appears in the gutter and the entire line is highlighted in red. To clear the breakpoint, click on the breakpoint icon and the breakpoint is removed. You can also press F5 or choose Toggle Breakpoint from the Code Editor context menu to toggle a breakpoint on or off....

The Mask Edit Component

The MaskEdit component is an Edit component with an input filter, or mask, attached. The MaskEdit does not represent a Windows control per se, but rather is just a VCL extension of a standard edit control. A mask is used to force input to a specific range of numbers or characters. In addition, the mask can contain special characters that are placed in the edit control by default. For example, a date is commonly formatted as follows An edit mask for a date can already have the slashes in place...

Properties

Properties are elements of a component that control how the component operates. Many components have common properties. All visual components, for example, have a Top and a Left property. These two properties control where the component will be positioned on a form both at design time and at runtime. All components have an Owner property, which VCL uses to keep track of the child components a particular parent form or component owns. Properties and the Object Inspector A picture is always worth...

Function Get Heading Integer

Function GetName string end implementation constructor TAirplane.Create AName string AKind Integer begin inherited Create Name AName Airliner Ceiling 35000 Commuter Ceiling 20000 PrivateCraft Ceiling 8000 end end procedure TAirplane.TakeOff Dir Integer begin Heading Dir Status TakingOff end Speed 0 Heading 0 Altitude 0 Status OnRamp end function TAirplane.SendMessage Msg Integer var Response string Spd Integer Dir Integer Alt Integer Boolean begin Do something based on which command was sent....