📄 ch08.htm
字号:
<H4>The Use Button</H4><P>The Use option is not common. When you use an object, you are opening that objectdirectly for editing. Select this option when you have saved an object in the Repositoryand you want to make permanent changes to that object. In the section about the Inheritoption, I said that changes made to a base form would be reflected in all inheritedforms. If you wanted to make changes to a base form, you would open it in the ObjectRepository with the Use option.</P><P><H4>Using the Object Repository</H4><P>Exactly what takes place when you select an object from the Object Repositorydepends on several factors. The factors include the type of object selected, whethera project is currently open, and the usage type you select (Copy, Inherit, or Use).</P><P>If you have an application open and you choose to create a new application fromthe Object Repository, you are prompted to save the current project (if necessary)before the new project is displayed.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> Choosing File | New Application from the main menu is a shortcut for starting a new application. It is equivalent to choosing New from the main menu and then choosing the Application object from the Object Repository. Similarly, the New Form item on the main menu is a shortcut for its equivalent in the Object Repository. <HR></BLOCKQUOTE><P>Creating a new form from the Object Repository is treated differently based onwhether a project is open at the time. If a project is open, the new form is addedto the application as a form/unit pair. If no project is open, a new form and unitare created as a standalone form. A form created outside of a project must be addedto a project before it can be used at runtime. Use this option when creating a newbase form to add to the Object Repository.</P><P>If you choose to create a new unit or text file, the new file is simply createdin the Code Editor (and, in the case of a new unit, added to the current project).You might create a new text file for several reasons. For example, let's say youwant to implement a configuration file (an .ini file) in your application. You couldcreate a new text file in the Object Repository to initially create the configurationfile. Create a new unit any time you want to start a new source file for your applicationthat is not associated with a form (an include file, for example).</P><P>Choosing a new DLL results in a new project being created with the project setup for a DLL target. Creating a new component or thread object results in a dialogbox being presented that asks for more information about the object you are creating.</P><P><H4>The Object Repository Views</H4><P>The actual Object Repository window is a Win32 list view control similar to theright side of Windows Explorer (where the files are listed). As such, it has severalviews that you can choose from: Large Icons, Small Icons, List, and Details. By default,the view is set to Large Icons. To change the Object Repository view, right-clickon the Object Repository and choose the view you want from the Object Repositorycontext menu. Figure 8.2 shows the Object Repository with the Forms page selectedand the view set to Details.</P><P><A HREF="javascript:popUp('28670802.gif')"><B>FIGURE 8.2.</B></A><B> </B><I>TheObject Repository in Details view.</I></P><P>The Object Repository context menu also shows several sorting options. You cansort by object name, description, date, or author.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> When the Object Repository is in the Details view, you can click a column header (Name, Description, Date, or Author) to instantly sort by that category. <HR></BLOCKQUOTE><P><H3><A NAME="Heading4"></A>Creating New Objects from the Object Repository</H3><P>Certainly the most basic use of the Object Repository is creating a new objectusing an object from the Repository. To illustrate, you can create a simple applicationwith a main form, an About dialog box, and a second form. Follow these steps:</P><DL> <DT></DT> <DD><B>1. </B>Ensure that no other application is open. Choose File | New from the main menu. The Object Repository is displayed. <P> <DT></DT> <DD><B>2. </B>Click the Application icon and click OK to create a new application. A new application is created and a blank form is displayed. <P> <DT></DT> <DD><B>3. </B>Place two buttons on the form. Change the Caption property of one of the buttons to About... and the Caption property of the other button to Display Form2. Change the Name properties if desired. <P> <DT></DT> <DD><B>4. </B>Choose File | New from the main menu. The Object Repository is displayed again. <P> <DT></DT> <DD><B>5. </B>Click the Forms tab in the Object Repository. <P> <DT></DT> <DD><B>6. </B>Choose the About box object. Ensure that the Copy radio button is selected and click OK to create a new About Box form. The About box is displayed. Change any properties as needed. <P> <DT></DT> <DD><B>7. </B>Modify the About box as desired. (Enter your own information, change the icon, size, position, and so on.) <P> <DT></DT> <DD><B>8. </B>Select File | New from the main menu again. The Object Repository is displayed for the third time. <P> <DT></DT> <DD><B>9. </B>Click the Forms tab and choose the Dual list box object. Click OK to close the Object Repository. A dual list box form is displayed. (I had you choose this one just so you could see it.) <P> <DT></DT> <DD><B>10. </B>Write event handlers for the two buttons that display the About box and the second form as required. Don't forget to add the units for the About box and the second form in the uses clause of your main form. <P> <DT></DT> <DD><B>11. </B>Compile, run, and test the program. <P></DL><P>No, this program doesn't do anything, but it does illustrate how you can use theObject Repository to quickly prototype an application. As time goes on, you willadd your own custom objects to the Object Repository and then you can really be effective!Let's look at that next.</P><P><H3><A NAME="Heading5"></A>Adding Objects to the Object Repository</H3><P>The Object Repository wouldn't be nearly as effective if you couldn't add yourown objects to it. But you can add your own objects and you should. Adding frequentlyused objects to the Object Repository makes you a more efficient and, therefore,a more valuable programmer. There is no point in reinventing the wheel.</P><P>After you create an application, form, or other object, save it to the Repositoryso that you can reuse it whenever you want. Of course, you don't want to save everyform you ever create in the Object Repository, just the ones you will reuse mostoften.</P><P>You can set out to create an object with the express purpose of adding it to theRepository, or you can add an object to the Repository during the normal course ofapplication development. (The term <I>object</I> is pretty broad, so I'll use a specificexample in order for this to make sense.) Let's say that you create an About boxform while creating an application. Suddenly it dawns on you that you'd like to savethis About box to use in all your programs. After all, it has your company name,logo, and all the copyright information all laid out just the way you like it, soit'd be a shame to have to re-create the same About box for every application youwrite. No problem--just add it to the Repository.</P><P>To add a form to the Object Repository, first save the form (if you don't savethe form, you will be prompted to save it before continuing). Next, right-click anywhereon the form and choose Add To Repository from the Form Designer context menu. Whenyou do, the Add To Repository dialog box is displayed as shown in Figure 8.3.</P><P><A HREF="javascript:popUp('28670803.gif')"><B>FIGURE 8.3.</B></A><B> </B><I>TheAdd To Repository dialog box.</I></P><P><PRE><I></I></PRE><P>The Forms list box on the left side of this dialog box lists the current formsas well as any other objects in the application (such as data modules). First, selectthe form that you want to add to the Object Repository.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> The active form in the Form Designer will already be selected in the Forms list box in the Add To Repository dialog box. <HR></BLOCKQUOTE><P>Now enter the object's title. This is the title that will appear below the iconin the Object Repository. The Description field is used to give further informationabout the object. This description is displayed when the Object Repository view isset to display all object details (refer to Figure 8.2). The Author field is whereyou type your name as the author of the object. You can enter your personal name,a company name, or any other identifying name.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Most of the prebuilt objects in the Object Repository that come with Delphi have "Borland" as the author name (the exceptions are the QuickReport and TeeChart objects). <HR></BLOCKQUOTE><P>The Page field is used to select the Object Repository page where the new objectwill be placed. You can choose from one of the existing pages or simply type thename of a new page in the Page field. If a page with the name you type doesn't exist,Delphi will create a new page with that name. Near the bottom of the dialog box isa button labeled Browse that you can use to select the icon used to represent theobject.</P><BLOCKQUOTE> <P><HR>You can choose icons from the Borland Shared Files\Images\Icons directory or the Delphi 4\Objrepos directory. The icons in the Delphi 4\Objrepos directory are the icons used by Delphi for the items it places in the Object Repository. <HR></BLOCKQUOTE><P>After you fill in all the fields and select an icon, click OK to add the objectto the Repository. The object is added to the Object Repository on the page you specified.You can now reuse that object any time you want. As you can see, adding an objectto the Object Repository is nearly as easy as using an object.</P><BLOCKQUOTE> <P><HR>When you add an object to the Object Repository, Delphi makes an entry in the Object Repository file that describes the object. This information includes the pathname where the form and source file for the object are located. If you move or delete an object's form or source file, you will not be able to use the object from the Object Repository. <HR></BLOCKQUOTE><H3></H3><H3><A NAME="Heading6"></A>Adding Projects to the Object Repository</H3><P>Adding projects to the Object Repository is not much different than adding individualforms. To add a project to the Object Repository, choose Project | Add to Repositoryfrom the main menu. The Add To Repository dialog box is displayed just like it iswhen you add objects to the Repository, except the Forms list box is not displayed.Fill in any required information (title, description, author, and so on) and clickOK and the project is added to the Repository.</P><P>After you are familiar with Delphi, you should create an application shell thathas the features you use most often in your applications. Each time you start a newstandard application, make a copy of the shell from the Object Repository. This wayyou can have your menus, toolbar, About box, and other standard dialog boxes allset up and ready to go in a matter of seconds. After the new application has beencreated, it can then be modified as with any project. You can add new forms, deleteany unwanted forms, and so on.</P><P><H3><A NAME="Heading7"></A>Object Repository Housekeeping</H3><P>You can manage the pages and objects in the Object Repository by using the ObjectRepository configuration dialog box.</P><P>To view the Object Repository configuration dialog box, choose Tools | Repositoryfrom the main menu or, if you have the Object Repository open, choose Propertiesfrom the Object Repository context menu. The configuration dialog box is displayedas shown in Figure 8.4.</P><P>This dialog box enables you to delete objects and pages from the Object Repository,move objects from one page to another, change the order of pages in the Object Repository,and more. The list of pages in the Object Repository is displayed in the list boxlabeled Pages on the left side of the dialog box. When you select one of the pagesin the Pages list, the list box on the right (labeled Objects) displays the objectscontained on that page.</P><P><A HREF="javascript:popUp('28670804.gif')"><B>FIGURE 8.4.</B></A><B> </B><I>TheObject Repository configuration dialog box.</I></P><BLOCKQUOTE> <P><HR><BR> <strong>NOTE:</strong> The Pages list box has two important items of note. First, notice that the New page, which is always the first page displayed when the Object Repository is invoked, is not listed here. (The ActiveX and Multitier pages aren't listed in the Pages list box, either.) The New page is fixed and cannot be altered. Also notice that there is an item labeled [Object Repository]. This item is actually a list of all items on all pages of the Repository. <HR></BLOCKQUOTE><H4><BR>Managing Objects</H4><P>Before you can edit, delete, or move an object, you must first select it. To selectan object, click the object in the Objects list box. After you select an object,you can edit it by clicking the Edit Object button. Editing an object enables youto change the object's name, description, and author information, as well as thepage on which the object is displayed.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> To quickly edit an object, double-click it in the Objects list box. <HR></BLOCKQUOTE><P>You can delete an object by selecting it and then clicking the Delete Object button.You are prompted for confirmation before the object is removed from the page andfrom the Repository.</P><BLOCKQUOTE> <P><HR>When an object is deleted from the Object Repository, it is removed from the Object Repository file and no longer shows up on any page in the Object Repository. However, the actual form file and source file that describe the object are not deleted from your hard drive. <HR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -