⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch17.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<P><B><I></I></B><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> The page shown in Figure 17.5 appears only if you select the option	for vertical component layout on the previous page of the Database Form Wizard.<HR></BLOCKQUOTE><P>The final page of the Database Form Wizard asks you to make two choices:</P><P><UL>	<LI>The first choice, to create the new database form as the main form of the application,	is in the form of a check box near the top of the page. Check this option if this	form is going to be the main form of the application. If this form isn't the main	form, clear the check box.</UL><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> If you choose to have Delphi create the new database form as the	main form of the application, be aware that the existing main form will still be	part of the application's project. You need to remove the old main form from the	project if you don't want it anymore. To remove the old main form, click the Remove	from Project button and remove the main form's unit (Unit1.pas for a default project).	<HR></BLOCKQUOTE><UL>	<LI>The second choice asks you whether to create just the form or the form and a	data module. If you choose the first option (Form Only), the form will contain all	the data components, the DataSet component (Table or Query), and the DataSource component.	If you choose the second option (Form and DataModule), the DataSet component and	the DataSource component will be removed from the form and placed in a separate data	module.</UL><P>I'll talk more about data modules tomorrow in the section &quot;Working with DataModules.&quot; For now, make certain that the Generate a Main Form check box is checkedand that the Form Generation option is set to Form Only. Figure 17.6 shows the lastpage of the Database Form Wizard.</P><P><A HREF="javascript:popUp('28671706.gif')"><B>FIGURE 17.6.</B></A><B> </B><I>Thelast page of the Database Form Wizard.</I></P><P>Click the Finish button and the form will be created. The finished form lookslike the one shown in Figure 17.7.</P><P><A HREF="javascript:popUp('28671707.gif')"><B>FIGURE 17.7.</B></A><B> </B><I>Thefinished form.</I></P><P>Notice in Figure 17.7 that the form contains a data component for each field thatwas selected and a label for each component. The label is capitalized because that'sthe way the field names are stored in this particular table. Notice that the topof the form contains a DBNavigator component so that you can browse the records ofthe table.</P><P><H3><A NAME="Heading3"></A>The New Form in Action</H3><P>Now you are ready to click the Run button and see how the form works. When theapplication starts, the first record in the dataset is displayed. Use the buttonsof the DBNavigator to move through the dataset. Keep in mind that you are workingwith live data at this point. You can edit a record by changing a value in one ofthe edit components. To save the changes to the database, click the Post button onthe DBNavigator (the check mark). To cancel any changes to the record, click theCancel button on the DBNavigator (the X button). When you post a record or move toanother record, the changes are saved to the database.</P><P>Close the application and return to the Delphi IDE. Spend some time examiningthe components on the form and the components' properties. Later I am going to goover some of the data components, but for now just experiment a little.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> If you find yourself re-creating a database form over and over again,	consider adding the form to the Object Repository. Forms in the Object Repository	can be reused with just a few mouse clicks.<HR></BLOCKQUOTE><P><H3><A NAME="Heading4"></A>Creating a Master/Detail Form</H3><P>Next, you create a master/detail form using the Database Form Wizard. When creatinga master/detail form, the first few pages of the wizard are nearly the same as whencreating a simple form. Only the labels that describe your options are different.First, perform the following steps. After that, I explain the specifics of the DatabaseForm Wizard that pertain to master/detail forms. Here goes:</P><P><DL>	<DT></DT>	<DD><B>1. </B>Start the Database Form Wizard. On the first page, choose the option	to create a master/detail form and the option to use TTable for the dataset components.	Click the Next button.	<P>	<DT></DT>	<DD><B>2. </B>The next step is to choose the table that will serve as the master.	Select the DBDEMOS alias and then select the CUSTOMER.DB table. Click the Next button.	<P>	<DT></DT>	<DD><B>3. </B>Select just the CustNo and Company fields and add them to the Ordered	Selected Fields list box. Click the Next button.	<P>	<DT></DT>	<DD><B>4. </B>Click on the Horizontally option to lay out the components horizontally.	Click the Next button.	<P>	<DT></DT>	<DD><B>5. </B>Now choose the table that will provide the details. Select the ORDERS.DB	table and then click the Next button.	<P>	<DT></DT>	<DD><B>6. </B>Select the CustNo, OrderNo, SaleDate, and AmountPaid fields and add	them to the Ordered Selected Fields list box. Click the Next button.	<P>	<DT></DT>	<DD><B>7. </B>Choose the option to arrange the components in a grid and click the	Next button.	<P></DL><P>Now you are presented with a page you haven't seen before in the Database FormWizard. This page asks you to select the fields that will join the two tables:</P><P><DL>	<DT></DT>	<DD><B>1. </B>Click on the Available Indexes combo box at the top of the page and	choose the CustNo field. The CustNo field is designated as a secondary index, so	you'll use that field to join the two tables.	<P>	<DT></DT>	<DD><B>2. </B>Select CustNo in both the Detail Fields list box and in the Master	Fields list box. After you select CustNo in both list boxes, the Add button between	the two list boxes is enabled.	<P>	<DT></DT>	<DD><B>3. </B>Click the Add button to add the join to the Joined Fields list box	at the bottom of the page.	<P></DL><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> If you are paying attention, you might notice that this page of the	Database Form Wizard looks almost identical to the Field Link Designer you saw on	Day 16, &quot;Delphi Database Architecture,&quot; when you set up a master/detail	form by hand. Both perform the same task.<HR></BLOCKQUOTE><DL>	<DT></DT></DL><P>Figure 17.8 shows the Database Form Wizard after joining the two tables.</P><BLOCKQUOTE>	<P></BLOCKQUOTE><P><A HREF="javascript:popUp('28671708.gif')"><B>FIGURE 17.8.</B></A><B> </B><I>Thetwo tables joined on the </I>CustNo<I> field.<B></B></I></P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> The page illustrated in Figure 17.8 is slightly different if you	are using TQuery--instead of TTable--components for the datasets. Specifically, the	Available Indexes combo box isn't present when using TQuery components. The reason	this combo box isn't displayed when using TQuery components is that the join is created	using SQL statements, and indexes are not used in the same way they are with TTable	components. <HR></BLOCKQUOTE><DL>	<DT></DT>	<DD><B>4. </B>Click the Next button to move to the next page of the Database Form	Wizard. Because this page already has the settings you need, just click the Finish	button.	<P></DL><P>Now Delphi creates the form, and you are ready to try your new creation. Clickthe Run button to run the program. Use the DBNavigator to move from record to record.You will notice that the customer name is displayed in the top part of the form andthat all the orders for that customer are displayed in the table in the bottom partof the form. Figure 17.9 shows the master/detail application running.</P><P><A HREF="javascript:popUp('28671709.gif')"><B>FIGURE 17.9.</B></A><B> </B><I>Yournew master/detail form in action.</I></P><P><I></I><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> If you want to see the SQL statements for a master/detail relationship,	run the Database Form Wizard again, but this time choose TQuery components for the	datasets. After the form has been created, click on each Query component and examine	its SQL property. <HR></BLOCKQUOTE><H2></H2><H2><A NAME="Heading5"></A>Creating Database Forms by Hand</H2><P>The Database Form Wizard is a good tool for quickly laying out a form; it is particularlyconvenient for quick test programs. It will not be long, though, before you willneed to custom-design your database forms. After you gain experience creating databaseforms, you might actually prefer to create your forms by hand rather than use theDatabase Form Wizard.</P><P>There isn't a lot to know about creating database forms by hand. You drop oneor more datasets (Tables or Queries) on the form and a DataSource component for eachdataset, and then place a component on the form for each field in the dataset thatyou want to view. Sounds easy enough, and it is.</P><P>Let's build a new form that approximates the form you created earlier in the section&quot;Creating a Simple Form Using the Database Form Wizard.&quot; First, you laythe groundwork and start adding the data components. Perform these steps:</P><P><DL>	<DT></DT>	<DD><B>1. </B>Start a new application. Place a Panel component on the form and set	its Align property to alTop. Remove the caption.	<P>	<DT></DT>	<DD><B>2. </B>Place a ScrollBox component on the form below the panel (you can find	it on the Additional tab on the Component palette). Set the Align property to alClient.	<P>	<DT></DT>	<DD><B>3. </B>Place a Table component on the form. You can place it anywhere you	like, but the right side of the panel is probably the best place. Change the DatabaseName	property to DBDEMOS and the TableName property to ANIMALS.DBF.	<P>	<DT></DT>	<DD><B>4. </B>Place a DataSource component on the form next to the Table component.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -