📄 ch17.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//--> </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <TITLE>Teach Yourself Borland Delphi 4 in 21 Days -- Ch 17 -- Building Database Forms</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"></H1><H1><BR>Teach Yourself Borland Delphi 4 in 21 Days</H1></CENTER><CENTER><P><A HREF="../ch16/ch16.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch18/ch18.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><CENTER><H1>- 17 -</H1><H1>Building Database Forms</H1></CENTER><UL> <LI><A HREF="#Heading1">The Database Form Wizard</A> <UL> <LI><A HREF="#Heading2">Creating a Simple Form Using the Database Form Wizard</A> <LI><A HREF="#Heading3">The New Form in Action</A> <LI><A HREF="#Heading4">Creating a Master/Detail Form</A> </UL> <LI><A HREF="#Heading5">Creating Database Forms by Hand</A> <LI><A HREF="#Heading6">A Closer Look at the Data Components</A> <UL> <LI><A HREF="#Heading7">Properties Common to Data Components</A> <LI><A HREF="#Heading8">The DBGrid Component</A> <LI><A HREF="#Heading9">The DBNavigator Component</A> <LI><A HREF="#Heading10">The DBText Component</A> <LI><A HREF="#Heading11">The DBEdit Component</A> <LI><A HREF="#Heading12">The DBMemo Component</A> <LI><A HREF="#Heading13">The DBImage Component</A> <LI><A HREF="#Heading14">The DBListBox and DBComboBox Components</A> <LI><A HREF="#Heading15">The DBCheckBox Component</A> <LI><A HREF="#Heading16">The DBRadioGroup Component</A> <LI><A HREF="#Heading17">The DBLookupListBox and DBLookupComboBox Components</A> <LI><A HREF="#Heading18">The DBRichEdit Component</A> <LI><A HREF="#Heading19">The DBCtrlGrid Component</A> <LI><A HREF="#Heading20">Other Data Components</A> </UL> <LI><A HREF="#Heading21">Summary</A> <LI><A HREF="#Heading22">Workshop</A> <UL> <LI><A HREF="#Heading23">Q&A</A> <LI><A HREF="#Heading24">Quiz</A> <LI><A HREF="#Heading25">Exercises</A> </UL></UL><P><HR SIZE="4"><CENTER><H1></H1></CENTER><P>After a relatively unexciting look at Delphi database architecture, you can moveon to the more interesting task of building a database application. The first stepin that task is to learn how to create database forms, so that's today's subject.</P><P>You'll learn how to create database forms using the Delphi Database Form Wizard.You'll also learn how to build database forms from scratch. Toward the end of theday, you will learn about the data components of Delphi. These are the componentsthat display the data from a database and enable you to edit that data--you can findthem on the Data Controls tab of the Component palette. They are often referred toas <I>data-aware</I> components. I'll just call them data components in this chapter.Let's get to it.</P><P><H2><A NAME="Heading1"></A>The Database Form Wizard</H2><P>The Delphi Database Form Wizard provides a way of creating database forms quicklyand easily. Using this wizard, you can create a database form from start to finish.You don't have to place any database components on the form. You just start the wizardand let the wizard take it from there.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> No automated process is good enough to be all things to all people. I won't pretend that the forms created by the Database Form Wizard will be everything you want or that the Database Form Wizard will do all your work for you. What the Database Form Wizard can do, though, is the initial work of setting up a database form. After the initial work is done, you can go to work customizing the form to make it look the way you want.<HR></BLOCKQUOTE><P>The Database Form Wizard enables you to create both simple forms and master/detailforms. It enables you to choose whether your dataset will be a TTable or a TQuery.The wizard enables you to select a database table and to select the fields from thattable that you want displayed on the form. It gives you a choice of layout optionsas well. After you supply the Database Form Wizard with all the information it needs,it creates the new form for you.</P><P>To start the Database Form Wizard, choose Database | Form Wizard... from the Delphimain menu. Alternatively, you can start the Database Form Wizard from the Businesspage of the Object Repository.</P><P>First I'll show you how to create a simple form, and then I'll talk about master/detailforms.</P><P><H3><A NAME="Heading2"></A>Creating a Simple Form Using the Database Form Wizard</H3><P>When the Database Form Wizard starts, it displays the page shown in Figure 17.1.</P><P><A HREF="javascript:popUp('28671701.gif')"><B>FIGURE 17.1.</B></A><B> </B><I>Pageone of the Database Form Wizard.</I></P><P>This page of the Database Form Wizard asks you to choose the type of databaseyou want to create and the dataset type you would like to use. The Form Options sectiongives you the choice of creating a simple form (with a single dataset) or a master/detailform.</P><P>I talked briefly about master/detail tables yesterday in the section "Master/DetailTables." I'll talk more about master/detail forms later today in the section"Creating a Master/Detail Form." The first page of the Database Form Wizardalso enables you to select from either a TTable-based dataset or a TQuery-based dataset.For this exercise, the default settings for creating a simple form using a TTabledataset are what you want, so you can click the Next button to move on to the nextpage.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> In some cases, you might want to implement data from more than one table on a single form but not use a master/detail relationship. The Database Form Wizard enables you to choose only from one table. What you can do, though, is run the Database Form Wizard and select the first table. Your form will be created. Change the names of the Table and DataSource components to something meaningful. Now run the Database Form Wizard again and choose the second table. When the form is displayed, select all the database components on the form and copy them to the Clipboard. Switch back to the first form, make some room on the form for the new components, and paste them from the Clipboard onto the form. Now remove the second form from the project, and you're all set.<HR></BLOCKQUOTE><P>The next page asks you to choose a table from which to obtain the data. The Driveor Alias name combo box enables you to choose a database name just as you do whensetting the DatabaseName property for a dataset component at design time. It alsoenables you to choose a directory from which to select a table. For this example,choose the DBDEMOS alias. The tables available in the database will show up in theTable Name list box. Select the ANIMALS.DBF table. The Database Form Wizard now lookslike the one in Figure 17.2. Click the Next button to go to the next page.</P><P><A HREF="javascript:popUp('28671702.gif')"><B>FIGURE 17.2.</B></A><B> </B><I>Selectinga table with the Database Form Wizard.</I></P><P>The third page of the Database Form Wizard asks you to choose the fields fromthe table that you want to include on the form. The Available Fields list box onthe left shows the fields that are in the table you have selected. The Ordered SelectedFields list box on the right contains the fields that you want on your new form.Between the two list boxes are four buttons for adding or removing fields from theOrdered Selected Fields list box.</P><P>To add a field, click the field name in the Available Fields list box and clickthe > button. Add any fields you want with this method. You can select multiplefields and click the > button to add the selected fields. To add all fields inthe table at one time, click the >> button.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> You can double-click a field name to add that field to the Ordered Selected Fields list box or double-click it again to remove it from the list box.<HR></BLOCKQUOTE><P>After the fields have been added to the Ordered Selected Fields list box, youcan change the order of the fields by drag and drop or by clicking the up and downarrow buttons below the list box. Figure 17.3 shows this page of the wizard. Fornow, add all the fields to the Ordered Selected Fields list box and click the Nextbutton to move to the next page.</P><P><A HREF="javascript:popUp('28671703.gif')"><B>FIGURE 17.3.</B></A><B> </B><I>Pagethree of the Database Form Wizard, adding fields.</I></P><P>The next page of the Database Form Wizard dialog asks you how you want the componentsfor each field arranged on the form. You have three layout choices:</P><P><UL> <LI>Horizontally <P> <LI>Vertically <P> <LI>In a grid</UL><P>Click each of the three radio buttons and watch the image to the left of the wizardas you select different options. The image changes to show you the layout for eachchoice. Select the radio button labeled Vertically and click the Next button to continue.Figure 17.4 shows this page of the wizard.</P><P><A HREF="javascript:popUp('28671704.gif')"><B>FIGURE 17.4.</B></A><B> </B><I>Selectinga layout style with the Database Form Wizard.</I></P><BLOCKQUOTE> <P><HR><BR> <strong>NOTE:</strong> When Delphi creates the database form, it chooses the component type that most closely matches the data type of the field that component represents. For example, a regular text field is represented by a DBEdit component, a memo field by a DBMemo component, and a BLOB image field by a DBImage component. Delphi makes a best guess as to the component type to place on the form for a given data type. You might have to edit the form to get exactly the component you want for a given field. <HR></BLOCKQUOTE><P>The next page asks where you want to place the labels for the fields relativeto each data component. You can put the labels on top of the components or to theleft. Notice that here, too, the image on the Database Form Wizard changes dependingon which of the two options is currently selected. For now, choose the second optionin order to place the labels on top of the components. Figure 17.5 shows this pageof the Database Form Wizard.</P><P><A HREF="javascript:popUp('28671705.gif')"><B>FIGURE 17.5.</B></A><B> </B><I>Thispage enables you to place the labels.<B></B></I></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -