📄 ch17.htm
字号:
Change the DataSet property to Table1. <P> <DT></DT> <DD><B>5. </B>Place a DBNavigator on the panel at the top of the form. Change its DataSource property to DataSource1. <P></DL><BLOCKQUOTE> <P><HR><strong>TIP:</strong> You can set the DataSource property quickly by double-clicking the value column next to the property in the Object Inspector. Delphi will cycle through the available DataSource components each time you double-click. <HR></BLOCKQUOTE><DL> <DT></DT></DL><P>Your form now looks like Figure 17.10.</P><P><A HREF="javascript:popUp('28671710.gif')"><B>FIGURE 17.10.</B></A><B> </B><I>Theform in the initial stages.</I></P><P>Now you need to add some data components to display the table's data. As you workthrough this exercise, you might want to refer to Figure 17.7 to see the result.Do the following:</P><P><DL> <DT></DT> <DD><B>1. </B>Place a DBEdit component on the main part of the form along the left side. Change the Name property to NameEdit; change the DataSource property to DataSource1; change the DataField property to NAME (choose NAME from the drop-down list). <P> <DT></DT> <DD><B>2. </B>Place a Label component above the DBEdit you created in step 1 (use a regular Label component from the Standard page of the Component palette, not a DBText component). Change its Caption property to Name. <P> <DT></DT> <DD><B>3. </B>Repeat steps 1 and 2 and add DBEdits and Labels for the SIZE, WEIGHT, and AREA fields of the table. Make certain that you change the Name property and DataField property with respect to each field's name. <P> <DT></DT> <DD>In the next two steps, you depart from Figure 17.7 and place the component for the BMP field to the right of the components you just placed. It makes more sense for it to be there rather than below the existing components. <P> <DT></DT> <DD><B>4. </B>Place a DBImage component on the form to the right of the edit components. Change the Name property to BMPImage, and change the DataSource property to DataSource1 and the DataField property to BMP. <P> <DT></DT> <DD><B>5. </B>Place a Label component above the DBImage and change its Caption property to Picture (the field name is BMP, but the word Picture is more descriptive). <P></DL><P>Now your form looks like the one shown in Figure 17.11.</P><P><A HREF="javascript:popUp('28671711.gif')"><B>FIGURE 17.11.</B></A><B> </B><I>Theform with the data components in place.</I></P><P>Now you need to set the size of the DBImage component. Although you can't alwaysbe sure that each image will be the same size in every database, you can be certainin this case. You might guess at the size of the bitmap, but a better solution wouldbe to set the size with a live image in the component. That's easy enough to fix.Click on the Table component and set its Active property to True. The image for thefirst record in the table is displayed. Now you can size the DBImage as needed tomatch the bitmap size.</P><P>You have nearly completed the form, but there's one more thing you should do.To imitate the form that the Database Form Wizard created, you need to open the tablewhen the form is created:</P><P><DL> <DT></DT> <DD><B>1. </B>First, set the Active property of the Table to False (you just set it to True a minute ago). <P> <DT></DT> <DD><B>2. </B>Now select the form itself in the Object Inspector. (Remember that to select the form, you click on the panel and then press the Esc key on your keyboard. You can also select the form from the Component Selector at the top of the Object Inspector.) <P> <DT></DT> <DD><B>3. </B>Switch to the Events page and create an event handler for the OnCreate event. Type this line of code in the event handler: <P></DL><PRE>Table1.Open;</PRE><P>That's it. Your new form is finished. Click the Run button to test your new creation.It will behave exactly like the first form you created with the Database Form Wizard.</P><P><H2><A NAME="Heading6"></A>A Closer Look at the Data Components</H2><P>At this point, a quick look at the data components is in order. I'll give a briefoverview of each component and highlight each one's key properties and methods. Mostof the data components are derived from a standard component and have many of theproperties associated with that type of component. I will discuss only the propertiesthat are specific to the data version of each component.<B></B></P><P><H3><A NAME="Heading7"></A>Properties Common to Data Components</H3><P>All the data components have properties in common. For example, all the componentshave a DataSource property. This property is used to link a data component with adata source, which is itself linked to a dataset. You have been using the DataSourceproperty a lot in the past couple of days, so you should have a good idea of howit works.</P><P>Most data components also have a DataField property. You use this property tohook the data component to a particular field in the dataset. You saw how the DataFieldproperty is used when you built a database form from scratch. When you hook a datacomponent to a field in the dataset, the contents of that field are directly displayedin the data component. In the case of Tables (and Queries if the RequestLive propertyis True), this means that editing the data in the data control will result in changesto the data in the database.</P><P>Most data components also have a Field property. You use the Field property togain access to a component's contents programmatically. For example, to change thecontents of a DBEdit component, you can do the following:</P><P><PRE>NameEdit.Field.AsString := `Clown Fish';</PRE><P>You can also change the field's display characteristics or other TField propertiesthrough the Field property.</P><P>You can use the ReadOnly property to prevent the user from editing the data ina data component that enables editing (DBGrid and DBEdit, for example).</P><P><H3><A NAME="Heading8"></A>The DBGrid Component</H3><P>The DBGrid component displays a dataset in tabular, or spreadsheet, format. Oneof the most important properties of the DBGrid is the Columns property. This propertyenables you to change the number and order of the columns that appear in the grid.You can add, remove, and order columns using the Columns Editor.</P><P>To invoke the Columns Editor, right-click on the grid and choose Columns Editorfrom the context menu. You can also click the ellipsis button next to the Columnsproperty in the Object Inspector. Using the Columns Editor, you can add columns,remove columns, and arrange the order of columns. For example, a dataset might containa dozen fields, but you might want to view only half those fields in the DBGrid.Using the Columns Editor, you can hide the fields that you don't want to see.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Don't confuse the DBGrid Columns property (modified with the Columns Editor) with the Table component's FieldDefs property (modified with the Fields Editor). The FieldDefs property of the Table component controls which columns are actually contained in the dataset. The Columns property only affects which fields are <I>visible</I> in the grid.<HR></BLOCKQUOTE><P>The DefaultDrawing property indicates whether VCL draws the cells in the gridor the grid cells are owner-drawn. If DefaultDrawing is False, you must respond tothe OnDrawColumnCell and OnDrawDataCell events to provide drawing for the cells.</P><P>The Options property enables you to set the display and behavior options for thegrid. Using this property, you can turn off column titles, allow or disallow columnsizing, turn row and column lines on or off, and so on.</P><P>The TitleFont property enables you to set the font for the column titles. Youuse the Font property to set the font for the grid cells.</P><P>The DBGrid has only two public methods. When using an owner-drawn grid, you cancall the DefaultDrawColumnCell and DefaultDrawDataCell methods to ask VCL to drawthe cell for you. This is useful if you are owner-drawing particular columns butyou want default drawing behavior for the rest of the columns.</P><P>The DBGrid component has several events, most of which pertain to cell editingand data navigation. I won't list the events here because it is obvious from theirnames what functions they perform.</P><P><H3><A NAME="Heading9"></A>The DBNavigator Component</H3><P>The DBNavigator component enables the user to browse a dataset record by record.The navigator provides buttons for first record, next record, previous record, lastrecord, insert record, delete record, edit record, cancel edits, post edits, andrefresh. This component is nearly automatic in that most of the time all you haveto do is drop it on the form, hook it to a DataSource, and forget about it.</P><P>The ConfirmDelete property, when set to True, causes a dialog box to be displayedwhenever the user clicks the Delete button. You set the Hints property to True toenable fly-over hints for each button on the navigator. The VisibleButtons propertyis a set that enables you to control which buttons show up on the navigator. Youcan add or remove buttons at design time or runtime.</P><P>The DBNavigator has only one method of interest and one event. You can use theBtnClick method to simulate a button click on the navigator. You can use the OnClickevent to detect a click on the navigator. You rarely have to use OnClick, however,because the navigator already knows what to do when its buttons are clicked.</P><P><H3><A NAME="Heading10"></A>The DBText Component</H3><P>The DBText component is the data-aware version of the standard Label component.It provides a way of displaying data from a field without enabling the user to modifythe data. This component provides no database-specific properties, methods, or eventsother than those common to all data components.</P><P><H3><A NAME="Heading11"></A>The DBEdit Component</H3><P>The DBEdit component provides an edit control that is linked to a field in a dataset.You used a DBEdit earlier today when you created a database form from scratch. TheDBEdit component, like the DBText component, doesn't have any database-specific properties,methods, or events other than those common to all data components.</P><P><H3><A NAME="Heading12"></A>The DBMemo Component</H3><P>The DBMemo is the data version of the standard Memo component. You can use thiscomponent to display data contained in database fields that contain large amountsof text. The AutoDisplay property controls whether the data in the dataset fieldis automatically displayed when the cursor changes to a new record.</P><P>When AutoDisplay is True, the data is automatically displayed. When AutoDisplayis False, the user must double-click the DBMemo to display the data (or press Enterwhen the control has focus). To force the memo to display its contents through code,you use a related method called LoadMemo. This method is appropriate only when AutoDisplayis False.</P><P><H3><A NAME="Heading13"></A>The DBImage Component</H3><P>The DBImage component is used to display binary large object (BLOB) data thatis stored in image format. DBImage isn't necessarily a read-only component. You canchange an image by pasting an image from the Clipboard or by using the Picture propertyto load a file from disk. The following code will change the image at runtime:</P><P><PRE>DBImage1.Picture.LoadFromFile(`peregrine.bmp');</PRE><P>The main DBImage properties control how the image is displayed. They are describedas follows:</P><P><UL> <LI>The AutoDisplay property works exactly as described for the DBMemo component. <P> <LI>The LoadPicture method can be used to display the image when the AutoDisplay property is False. <P> <LI>The Picture property enables access to the image itself and works the same as it does for components such as the standard Image component. <P> <LI>The Center property determines whether the image is centered in the DBImage window. <P> <LI>The Stretch property determines whether the image will be stretched to fit the current size of the DBImage window or whether it will be displayed in its original size. If Stretch is False, part of the image will be clipped if the image is too large for the DBImage window. <P> <LI>The QuickDraw property determines whether to apply a palette to the image when it is displayed. When QuickDraw is True, no palette is used. When QuickDraw is False, a palette is used to display the image, which results in higher quality but slightly poorer performance when displaying images.</UL><P>Methods of the DBImage component include CutToClipboard, CopyToClipboard, andPasteFromClipboard. These methods do exactly what their names indicate.</P><P><H3><A NAME="Heading14"></A>The DBListBox and DBComboBox Components</H3><P>The DBListBox component is, for the most part, a standard list box. What distinguishesit is that when the user selects an item in the list box, the selected item is writtento the corresponding field in the dataset (set by the DataField property). To providethe strings the user can select from, you add strings to the Items property justas you do for a regular ListBox component. It is important to realize that the stringsfor the list box don't come from the database (that's what the DBLookupListBox isfor).</P><P>The DBComboBox works in exactly the same way as the DBListBox except for the obvious
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -