Delphi 簡介

Download Report

Transcript Delphi 簡介

程式在做什麼 ?
• 接受外界的反應
– 如何接受輸入?
– Inputbox
• 進行運算
– 有哪些運算的東西?
• 輸出結果
– 如何輸出結果?
– Msgbox
程式的類型
資料處理
繪圖
工具軟體
開發軟體
作業系統
驅動程式
資料處理程式乃是程式中佔最大宗
的程式
系統架構的演進
• 集中式資料處理方式
– 銀行櫃臺
• 檔案式資料處理方式
– 報稅系統
• 主從式資料處理方式
– 不勝枚舉
• N-Tier資料處理方式
系統開發的演進
• 檔案存取方式
• 簡單的資料庫檔案+專屬高階語言,如
dBase
• 簡單的資料庫檔案+專屬高階語言+進步
的Compiler,如Clipper
• 較複雜的資料庫檔案+獨立的高階語言,
如Access + VB ==> Client/Server架構
• 先進的資料庫軟體+專屬開發語言,如
Oracle+Developer 2000==>4 GL
系統開發的演進(續)
• 先進的資料庫軟體+獨立高階語言,如
Oracle + Visual Basic
• 先進的資料庫軟體+中介軟體+獨立高階
語言,如Oracle + MTS + Visual Basic==>
N-Tier
主從架構的分工
• 前端(Client)
–
–
–
–
通常是一個高階語言,如Visual Basic
GUI
存取後端資料
計算原則
• 後端(Server)
– 資料庫伺服器,如 MS SQL Server
– 提供資料的存取
– 加強資料的控管
Delphi 簡介
Form 介紹
• 範例一:一個 form 的程式
• 專案選項的設定
– Application
– directories/conditionals
• 儲存檔案
–
–
–
–
程式的副檔名 --> .pas
專案的副檔名 --> .dpr
表格的副檔名 --> .dfm
資源的副檔名 --> .res
view source
•
•
•
•
•
•
•
program --> 專案名稱
use --> 使用到那些檔案
in -->使用到檔案的位置
begin………end;
Application.initialize
Application.CreateForm(TForm1, Form1)
Application.Run
Delphi 程式撰寫工具
• code completion
– 方法一:ctrl+space
– 方法二:輸入物件名稱及句點後,等約一秒即出現
• code templates: ctrl+J
• code parameters:輸入函數名稱及左括號後,等約一秒即
出現
• tool-tip expression evaluation:將滑鼠指到變數上,
系統即告知此變數再何處宣告或其內容為何
Delphi 程式撰寫注意事項
•
•
•
•
•
不分大小寫
每一行指令結束時要加分號;
每一個procedure都有一個begin及end;
只能有一個end.
變數使用前一定要宣告
attribute 屬性介紹
•
•
•
•
•
•
•
name、caption
windowstate (max,min,normal)
color
font
borderstyle
top、left、height、width
visible
方法(method)介紹
• show
• hide
• close
event 事件介紹
• 表單出始時觸發的事件
– Oncreate,onshow,onactivate
• 表單結束時觸發的事件
– Onclosequery ,onclose
• 按一下滑鼠所觸發的事件
– OnMousedown, OnMouseUp,Onclick
• 按一下鍵盤所觸發的事件
– Onkeydown, Onkeypress,Onkeyup
• Onclick 將form的變大10個pixel
• OnDblClick 將form的變小10個pixel
delphi 函數
• showmessage(‘hello!’);
• showmessage(form1.name);
• showmessage(‘這是表單的名字為’+
form1.name);
• showmessage(‘form的高度為’ +
IntToStr(form1.height));
• showmessage(datetimetostr(now));
加入元件
• button:
– 改變form的顏色
– mousedown, mouseup 改變form1的caption
• 加入的物件後,delphi會自動將宣告
procudrue
• 練習1(ex1dpr1)
基本輸出入函數
• Messagebox、MessageDlg
– 建立一個button,並於click事件中加入
if application.messagebox(‘確定要離開嗎?’, ‘確認’,
MB_YESNO+MB_defbutton2+
MB_iconinformation+MB_systemModal)
=IDYes then
close;
(see page 10-6)
– 問題: 如果使用者直接於功能表列上去按關閉鈕呢
– Messagebox與messageDlg功能不同之處只在
Messagebox可以設定標題的文字。
• InputBox
– 建立Button1、Label1,並於Button1的Click事件中加
程式架構
• Delphi以unit為基礎,其基本架構如下:
unit Unit1;
interface
uses
Windows, ...
type
…
end;
implementation
程式註解
• 適當的註解可以提供使用者較容易瞭解
程式,Delphi 4提供了三種註解的寫法:
– // :單行的註解
– { } : 區塊註解
– (* *) : 區塊註解
流程控制(Cont…)
• 判斷控制
– If condition then
begin
…
end
else
begin
…
end;
– Case condition of
cond1 : statement ;
cond2 : statement ;
…
else
: statement ;
end ;
• 範例
寫一個程式,當輸入的結果
顯示
>=90
A
>=80 And <90
B
>=70 And <80
流程控制
• 迴圈控制
– Repeat
…;
Until condition;
– While condition do
begin
…
end ;
– For 變數=初始值 to 結束值 do
begin
流程控制(Cont…)
• 例外處理
– try
…
...
– except
on exception do statement
…
end;
– try
…
finally
…
其他控制
• With Object1… do
begin
statement;
statement;
…
statement;
end;
• 範例
with button1 do
begin
font.color:=clgreen;
height:=50;
程式內定的答案做比對,如果
太大(太下)就秀出太大(太小),
重新再輸入一下,直到猜對為
止,當猜對時要秀出使用者共
try
Randomize; 猜了幾次。
answer:=random(100);
j:=0;
while bingo<>true do
begin
guessnum:=strtoint(inputbox('guess','please input a
number',''));
j:=j+1;
if guessnum>answer then
showmessage('too big')
運算式
•
•
•
•
算術:+ - * / div (取整數) mod (取餘數)
布林:not and or xor
字串:+
集合:+ - * <= >= = <> in
運算範例
• 範例一
a:=5;b:=3
showmessage(inttostr(a and b));
• 範例二
var a,b : set of byte
a:=[1,2,3];b:=[1,2,3,4]
if a <= b then showmessage(‘a <= b’)
else show message(‘a > b’);
資料型態
• PASCAL提供了下列各種資料型態
• 數字型態:
– Integer
4 Bytes
-2147483648~2147483647
– Cardinal
4 Bytes 0~4294967295
– ShortInt
1 Byte
-128 ~ 127
– SmallInt
2 Bytes
-32768~32767
– LongInt
4 Bytes
-2147483648~2147483647
– Int64
8 Bytes
-263 ~ 263 - 1
– Byte
1 Byte
0 ~ 255
– Word
2 Bytes
0 ~ 65535
– LongWord
4 Bytes
0~4294967295
資料型態(Cont…)
• 字元型態
– Char
– AnsiChar
– WideChar
1 Byte
1 Byte
2 Bytes
• 布林型態
– True
– False
• 列舉型態
– Type 列舉名稱 =(列舉 1, 列舉 2, 列舉 3,…)
資料型態(Cont…)
• 範圍資料型態
– Type MyNumber = -128..127
• 實數型態
–
–
–
–
–
–
Real48
6 Bytes
Single
4 Bytes
Double
8 Bytes
Extended
10 Bytes
Comp
8 Bytes
Currency
8 Bytes
922337203685447.5808
922337203685447.5807
– Real
8 Bytes
2.9*10-39 ~ 1.7*1038
1.5*10-45 ~ 3.4*1038
5*10-324 ~ 1.7*10308
3.6*10-4951 ~ 1.1*104932
-236+1 ~ 263 -1
~
5*10-324 ~ 1.7*10308
資料型態(Cont…)
• 字串型態
– ShortString
– AnsiString
– WideString
2 Bytes ~ 256 Bytes
4 Bytes ~ 2 GB
4 Bytes ~ 2 GB
• 集合型態
– 集合型態是由一群相同序數型態的元素所組成的集
合,其序數是介於 0 ~ 255
– TmyInt= 0..255 ;
– Tmyintset = Set of TmyInt ;
資料型態(Cont…)
• 陣列型態
– TmyArray = array[0..9] of Integer;
• 紀錄型態
– TMyStudent = Record
Name: String]50];
IDNO: String[20];
…
end
• 指標型態(pointer)
• 變形資料型態 (variant)
資料型態範例
• 範例一
procedure TForm1.Button1Click(Sender: TObject);
type
mycolor=(r,g,b);
var
x : mycolor;
Begin
x:=g;
case x of
r : showmessage('紅色');
g : showmessage('綠色');
b : showmessage('藍色')
變數宣告及生命週期
• 用var來宣告,如宣告 x:integer;
• 在procedure內宣告的話,屬於local變數,
於此procedure結束時,此變數就消失了
• 在implementation中宣告的話,屬於global
變數,其生命週期與unit相同; global變數
可以給定初始值
函數/程序
• 宣告function mysum(x,y:integer):integer;
– 可宣告於type 之內、外,之內代表屬於form之
member function,實作時需加 Tform1,且呼叫時
也要加form1;宣告於Type之外則屬於整個程式所
有,故呼叫時直接用函數即可。
• 實作 function mysum(x,y:integer):integer;
begin
mysum:=x+y;
end ;
於 button1 的onclick事件中輸入
showmessage(inttostr(mysum(5,3))); //宣告於Type之外
showmessage(inttostr(form1.mysum(5,3))); //宣告於
參數傳遞
• 傳值與
– 內定 為傳值傳遞
– 用var 指定為傳址傳遞
– 用 const指定為傳值傳遞,且不能改變參數
值
撰寫兩個procedure如下
procedure byval(myval : integer);
begin
myval:=myval*myval;
物件寶庫的使用
• 使用時機:對於專案常用或共用的表單
統一做成一個供大家使用
• 設計一個專案共用的表單
parent、owner、sender與 self 介
紹
•
•
•
•
•
parent:直接包含本身物件的容器物件
owner:本物件擁有者。
Sender:觸發事件的元件
self:元件本身
範例一:建立panel1及button1,button2,觀察
button1會隨panel1而移動,所以button1的parent
為panel1
• 範例二:
– 建立button1的onclick 事件如下
if sender is tbutton then
showmessage( (sender as tbutton).caption)
滑鼠事件
• OnMouseDown
–
–
–
–
MbLeft:使用者按下左鍵
Mbmiddle:使用者按下中間鍵
Mbright:使用者按下右鍵
建立button1的OnMouseDown 的事件如下
case button of
mbleft : edit1.text:=‘你按下左鍵’;
mbmiddle : edit1.text:=‘你按下中間鍵’;
mbright : edit1.text:=‘你按下右鍵’;
end;
鍵盤事件
• OnKeyDown
– 於edit1的 OnKeyDown事件中加入
if ((key=13) and (shift=[ssalt,ssctrl])) then
begin
edit2.SetFocus;
edit2.SelectAll;
end;
• OnKeyPress
– OnKeyPress的key值是一個char;
OnKeydown的key值是word;
form特有屬性及操作
• Active:判斷 form是否為active
• ActiveControl:判斷目前form的那一個物件為
active
在form1上加入edit1、edit2、timer及label1,並在Timer
的OnTimer事件中加入以下程式
timerinterval:=100;
label1.caption:=activecontrol.name+’ is active.’;
• bordericon:設定form在左上角是否出現最大、
最小、關閉及help的按鈕。
Bordericons:=[biSystemMenu,biMinimize,biMaximize]
Bordericons:=Bordericons-[biMaximize] (取消最大鈕)
Bordericons:=[] (取消所有的icon)
Canvas物件
//亂數產生三個圓
procedure TForm1.Timer1Timer(Sender: TObject);
var x,y,r,g,b:integer;
begin
if num< 3 then
begin
num:=num+1;
randomize;
with form1.Canvas do
begin
Image螢幕保護程式
procedure TForm4.Image1MouseDown(Sender:
TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
self.Close;
end;
procedure TForm4.FormActivate(Sender: TObject);
begin
image1.top:=self.top;
圓球螢幕保護程式
var
Form3: TForm3;
red:integer;
topdirection:integer=1; // 1代表往下,2 代表往上
leftdirection:integer=1; // 1代表往右,2 代表往左
implementation
{$R *.DFM}
procedure TForm3.FormMouseDown(Sender:
Snow 螢幕保護程式
procedure TForm5.FormActivate(Sender: TObject);
begin
randomize;
for i:=1 to 100 do
begin
snow[i]:=Tshape.Create(self);
snow[i].parent:=self;
snow[i].visible:=true;
snow[i].Shape:=stcircle;
snow[i].top:=random(self.Height);
文字控制物件
• TEdit:只能編輯一行。
• TMaskEdit:只能編輯一行,但能指定輸
入的格式。
• TMemo:能編輯多行。
• TRichEdit:能編輯多行、能設定文字的
字行格式。
TEdit
• Text:取得TEdit 的內容
• MaxLength:設定最多能輸入多少字元
• CharCase:強迫使用者輸入的字元為大
寫或小寫
• PasswordChar:隱藏使用者輸入的字元
建立button1、edit1及edit2,並將edit2的
PasswordChar設為*,再於button1的click事
件中加入
if edit2.text <> ‘’ then
TMaskEdit
• Editmask:設定輸入的格式
建立MaskEdit1,並設定editmask=‘!99_ll-aa;1’
! 代表去除!之前的空白字元
9 代表只能輸入數字
l 代表只能輸入字母
a 代表只能輸入字母或數字
• defaultblank
TMemo
•
•
•
•
text:Tmemo的全部內容
lines:設定Tmemo的單行內容
append:附加資料到Tmemo
LoadFromFile、SaveToFile:從檔案中存
取資料
建立edit1、memo、button1、button2、button3
及button4
於button1的click中加入
memo1.lines.savetofile('c:\memotext');
TRichEdit
• defAttribute:設定字型
• selAttribute :設定所選取字串的字型
建立edit1、RichEdit1、button1、button2、
button3及button4、於button1的click中加入
with Richedit1.defAttributes do
begin
height:= height+2;
color:=clBlue;
style:= [FsBold,FsItalic,FsUnderline];
TBitButton
• 與Tbutton相同,但可顯示小圖示
–
–
–
–
–
Kind 屬性:設定button的種類 (see page 8-6)
Glyph屬性:設定自己的小圖示
Margin屬性:設定小圖示與邊界的距離
Spacing屬性:設定小圖示與文字的距離
Layout屬性:設定小圖示與文字的排列方式
• blGlyphleft、 blGlyphRight、 blGlyphTop、
blGlyphButtom
TToolBar
• BorderWidth屬性:button與toolbar之間的垂直
距離
• Indent屬性:button與toolbar之間的水平距離
• list屬性:button的承現方式
• transparent屬性:決定toolbar 是否為透明
• showcaptions屬性:決定是否顯示button的文字
• Flat屬性:決定是否用“平平”的方式顯現
• image屬性:設定所對應的圖片串列
– 建立一個ToolBar、ImageList,並於toolbar中加入五
個按鈕及指定image 屬性為imagelist
– 按imagelist 兩下,加入“\borland\delphi5\demos\doc\
TUpDown
• Orientation:顯示的方向
(udHoriental,udVertical)
• Associate:設定要連結的物件
• Increment:每次的改變量
• Max/Min:允許的最大/最小值
• Position:updown目前的值
練習四(ex4)
TListBox
•
•
•
•
Items:資料項目
MultiSelect:決定是否可以多選
SelCount:使用者選取項目的總數
Selected:判斷某個資料項目是否被選取
建立一個Listbox1及button1,於listbox1的Items
中加入星期一至星期日,再於button1的click
中加入
str:=‘你共選取
了 ’+inttostr(listbox1.selcount)+’個項目
TCombobox
• 與ListBox相似,但只能選取一個項目
• Text屬性:選取的內容
• DropDownCount屬性:下拉時顯示的資
料項目個數
• Style屬性:決定使用者是否有輸入的權
限,當Style設定為 csDropDown與
csSimple時才允許輸入。
建立Listbox1、Combobox1及Combobox2,於
combobox1的items 中加入星期一至星期日,
TRadioButton
• checked屬性:判斷是否被選取
建立二個Radiobutton及一個button、
Radiobutton的Caption分別是‘男性’、
‘女性’,再於Button1的click事件中加
入
if Radiobutton1.checked then
showmessage(‘性別 是’+
Radiobutton1.caption)
else if Radiobutton2.checked then
TCheckbox
• 與radiobutton相似,但可複選
– 將上例的Radiobutton改成checkbox,
checkbox的Caption屬性填入delphi,VB,
Button1的click事件改為
str:='你選了 ';
if (checkbox1.checked) then
str:=str+checkbox1.caption;
if (checkbox2.checked) then
str:=str+checkbox2.caption;
label1.caption:=str;
TRadioGroup
• Items:項目的標題名稱
• Itemindex:被選取的項目
• columns:顯示的欄位數
建立Radiogroup1、Radiogroup2、button1及
label1,於Radiogroup1的items屬性中填入
Delphi,VB,FoxPro,於Radiogroup2 items屬性
中填入Informix,Oracle,Sybase,再於Button1
的click事件中加入
label1.caption:=Radiogroup1.Items[Radiogroup
•
TdriveCombobox、
TDirectoryListBox、
TFileListBox、
TFilterComboBox
建立如練習六(ex6)
Opendialog與SaveDialog
• Title屬性:對話框的標題
• InitialDir屬性:對話框的起始目錄
• Filename屬性:使用者所選的檔案名稱或
對話框開啟時預設的檔名
• Filter屬性:檔案的過濾條件
• DefaultExt屬性:對話框預設的副檔名
建立Button1、opendialog1,於Button1的Click
事件中加入
opendialog1.initialdir:='c:\temp';
OpenPictureDialog 與
SavePictureDialog
• 承續上例,建立OpenPictureDialog 1與
SavePictureDialog1,將memo物件改為
image,並將button1的Click事件改為
openpicturedialog1.initialdir:='c:\windows';
if openpicturedialog1.execute then
image1.picture.loadfromfile(openpicturedialog1.f
ilename);
FontDialog
• font屬性:記錄對話框傳回的設定值
建立Button1、richedit1、fontdialog1,於
Button1的Click事件中加入
if fontdialog1.execute then
begin
richedit2.defattributes.color:=fontdialog1.font.col
or;
richedit1.defattributes.assign(fontdialog1.font);
end;
ColorDialog
• Color屬性:記錄對話框傳回的設定值
接上例、再建立Colordialog1,於Button1的
Click事件中再加入
if colordialog1.execute then
richedit1.text:=colorToString(colordialog1.color);
form1.color:= colordialog1.color;
TPageControl
• 新增一頁:選定PageControl後按右鍵選
new Page
• ActicePage屬性:目前Active的Page
• HotTrack屬性:決定當滑鼠指到頁標題時,
標題文字是否要變顏色
• Image屬性:每頁顯示的圖型
• Style屬性:頁標題的顯示方式
• TabPosition屬性:頁標題顯示的位置
Datetimepicker
• date,time屬性
var d:Tdate;
d:=datetimepicker1.date;
showmessage(datetostr(d));
TProgressBar
•
•
•
•
Max/Min屬性:執行範圍的最大/最小值。
step屬性:每次的增加量
stepit方法:啟動Progressbar一次
自定一個RunProgress的procedure如下
(要用progress需 uses comctrls)
procedure TForm1.RunProgress(Total,StepQ:
Integer);
var
ProgressBar1: TProgressBar;
TStatusBar
• Panels屬性:在StatusBar下新增的Panel
• SimpleText:顯示的訊息。
建立一個StatusBar1,選定StatusBar1按右鍵,選擇
Panel Editor後,新增兩個Panels,將此兩個Panel的
Width設為150
於form1的mousemove事件中加入
statusbar1.panels[0].text:=‘滑鼠的位置’ +
inttostr(x) +','+inttostr(y);
statusbar1.panels[1].text:=format('滑鼠的位置
%d ,%d',[x,y]);
(format的語法請看On_line help)
動態產生元件
procedure TForm1.Button1Click(Sender: TObject);
type labelarray=array[0..9] of Tlabel;
var mylabel:labelarray;
i:integer;
begin
for i:=0 to 9 do
begin
mylabel[i]:=tlabel.Create(self);
主功能表(MainMenu)的使用
• 加入MainMenu1元件,於Items屬性快點
兩下即可加入項目
• 分別於Caption屬性中輸入showform1、
showform2、-(輸入“ - ”代表分隔線)、及
showform3。
• 於showform3中按Ctrl+ 往右鍵,可輸入
子項目
• 將formstyle設為fsMDIForm,form1及
form2的formstyle設為fsMDIChild
PopupMenu
• checked屬性
• 加入Richedit1及PopupMenu1元件,於
Items屬性快點兩下即可加入項目
• 分別於Caption屬性中輸入Bold、Italic、
Underline
• 於bold 的onclick屬性中輸入
if not (fsbold in richedit1.defattributes.style) then
begin
richedit1.defattributes.style: =
MDIForm
• 新增一個form1,並將其formstyle屬性設為
fsMDIForm,於其上加入一個MainMenu (含有
file、new等之功能表選項)。
• 新增一個form2,並將其formstyle屬性設為
fsMDIChild。
• 設定use unit1;
• 於foromclose事件中加入
procedure TForm2.FormClose(Sender: TObject; var
Action: TCloseAction);
begin
Action:=cafree;
form1child:=nil;
ShellExecute的使用
• 用來執行另一個檔案或打開一個檔案
• 需在uses 中加入 shellapi,要查用法可打
開 win32.hlp檔案取查詢(需去尋找此檔案)
• shellexecute(handle,'open','http://www.seed.
net.tw',nil,nil,sw_shownormal);
• shellexecute(handle,'open','project1.exe',nil,'
c:\n_tier',sw_shownormal);
資料庫程式設計
• 用access建立資料庫class,內含class、
teacher及publisher三個表格
• 目的:在form1上的DbEdit1如何連到class
資料庫下的class表格的class_name欄位
1.access資料庫-->BDE-->table-->datasource ->DBEdit
2.於BDE中建立資料庫別名:class1
3.建立 Table1-->database name--> class1
table name-->class
使用ADO連接資料庫
• 此種連接方式不必透過BDE, 所以client
端不必安裝BDE,但要安裝ADO 2.1或更
新板本
製作Navigator (table 的操作方
法)
•
•
•
•
•
•
•
open、close
next:下一筆
prior :上一筆
last :最後一筆
First :第一筆
append:新增一筆空白記錄至檔尾
Insert:在指標位置新增一筆空白記錄
(若欄位有 not null的限制、可於OnNewRecord
DbGrid
• Title.caption、Title.color、 Title.font、
Title.Alignment
• 選擇顯示的欄位:於column editor中add、
delete
• picklist 屬性:以combobox方式讓使用者輸入
資料
• buttonstyle屬性:每個欄位都有此屬性,當設
定它為ellipse時,可出現一個小按鈕。
此按鈕的click事件即為dbgrid的
OnEditButtonClick 事件。
table1.Edit;
資料庫查詢方式一:Locate (單
筆)
• [loCaseInsensitive]:不分大小寫搜尋
• [loPartialKey]:部分搜尋
• 找到的話會將記錄指標移到找到的記錄
上。
table1.locate(‘class_id’,’1’,
[loCaseInsensitive]);
資料庫查詢方式二:lookup (單
筆多欄)
• 會傳回一個Varaint 型態的資料。
• 找到的話不會將記錄指標移到找到的記
錄上。
搜查單一欄位
Find_result:=table1.lookup(‘class_id’,’1’,
‘class_name;class_cost;class_date’);
搜查多個欄位
Find_result := table1.lookup(‘class_id ;
class_cost’, VarArrayOf([‘1’,’3000’]) ,
資料庫查詢方式三:Setkey,
GotoKey 、GotoNearest (單筆資
料)
• 需設一個索引欄位,此欄位須再資料庫
中有定義為索引欄位,查詢速度較快。
with table1 do
begin
indexfieldnames:='class_id';
setkey;
fieldbyname('class_id').value:='3';
gotokey;
end;
資料庫查詢方式四:
FindKey(單筆資料)
• 與gotokey 類似,但可用於複合索引
( composite index)上,查詢速度亦快。
• 於button1的click事件輸入
if not table1.findkey([‘2’,’excel’]) then
showmessage(‘not find!’) ;
資料庫查詢方式五:(多筆資料)
• filter、filtered屬性:於filter中輸入過濾條
件,再將filtered屬性設為True。
於 filter中輸入 class_cost >5000 ,filtered
設為True
• OnFilterRecord事件:處理教複雜的過濾
條件
(OnFilterRecord的執行效率並不好,建議
非不得已不要使用)
TQuery(用SQL 語法來查詢)
• Query1.open, Query1.Close;
• Query1.Clear:將Query 元件內的sql 敘述
清除
• Query1.Sql.Add:
• Query1.Prepare:將Sql敘述先做語法分析、
最佳化處理及使用權限判定等。
• 於formcreate 時將table1所有欄位加入
combobox1中
• 於button1的Click事件中加入
DBGroupRadio
• 製作一個DBGroupradio來修改teacher_id
的資料。
• 新增一個DBGroupradio 並設定其連上
class表格的teacher_id,在其items屬性中
加入1,2,3。
DBCheckBox
• valueChecked:被Checked時的值
• valueUnChecked:UnChecked時的值
• 新增一個DBCheckBox 並設定其連上
teacher表格的sex欄位,在其
ValueChecked屬性中填入“ 男”,在其
valueUnChecked屬性中填入“女”。
製作一個用Combobox1來查詢
資料
• 於formcreate 中加入
table1.open;
with table1 do
for i:=0 to recordcount-1 do
begin
combobox1.items.add(fieldbyname('class_nam
e').value);
next;
DBCombobox
• 可用來顯示table中某一欄位的記錄,並
可藉此修改資料。
• 製作一個可由DBCombobox來修改資料
庫
– 將DBCombobox1連上class table的teacher_id
• 練習將上例改為dbCombobox? (會發生問
題)
DblookupListbox
• 用來顯示外來鍵所關聯的表格資料。如
在class表格中,若要輸入老師編號時,
可用DblookupListbox來顯示老師名稱,
由使用者點選老師名稱後,會自動填入
老師編的值,使用者不需記住每個老師
的編號
• 建立table1、table2,datasource1、
datasource2及在table1的field editor中加入
所有欄位,加入一個DBNagivator及
sql 語法(sql server)
• (用unc表示法連結遠端資料庫)
master/detail form
• 設定兩個表格之間的1對多的關係
• 建立table1、table2,datasource1、
datasource2、DBNavigator及Pagecontrol1,
並在Pagecontrol上加入兩頁
• 在table1的field editor中加入所有欄位於
第一頁
• 在table2的field editor中加入所有欄位於
第二頁
• 設定table2的Master source為 source1
一個程式同時在不同地方執行
• 開發完成的程式若要到其他地方執行,
除了將程式複製到Client 端之外,還需安
裝BDE及建立BDE上的別名。
• 如何取得最新的資料:refresh;
在 navigator上的click 事件中加入
if button=nbpost then table1.refresh;
//若按下確定鈕就更新資料
• 利用ODBC 連接資料庫的AP,每個Client
端需有ODBC的dsn
DataBase(一)
• table1--->Database--->BDE 別名
• Aliasname:設定與BDE連結的資料庫名
稱
• DatabaseName:與Ttable或Tquery連接的
名稱
• Name:元件名稱
• LoginPrompt:設定執行應用程式前是否
要出現密碼登錄的視窗
Database(二)
•
•
•
•
•
StarTransaction:開始進入transaction狀態
InTransaction:是否在transaction狀態
Commit:結束transaction狀態
Rollback:回復成Transaction之前的狀態
Transaction屬性:tiDirtyRead、
tiReadCommit
• 分別製作五個按鈕StartTransaction、
Commit、Rollback,open ,close撰寫程式
如下
DataModule
• 若有十個form,每個form都用到同一個
Table,可用DataModule來統一建立一個
Table,就不必於十個Form中都加入Table,
將來若有修改,只要在DataModule中修
改即可,不必於每個form中修改。
• 於file-->new-->DataModule 新增一個資料
模組
• 於DataModule1中加入database1、table1
及datasource1並設定其連結到D_class資
動態連結資料庫(自動抓取BDE
的別名)
• 建立tablel1、datasource1、combobox1、
combobox2、DBGrid1及DBnavigator1
• 設定DBGrid1及DBnavigator1的
datasource為datasource1、 datasource1的
dataset為table1
• 於form1的create事件中加入
//抓取BDE的資料庫別名給combobox1
session.getdatabasenames(combobox1.items);
• 於combobox1的onchange事件中加入
TreeView
procedure TForm1.Button2Click(Sender: TObject);
begin
Treeview1.items.AddChildFirst(treeview1.TopItem,'root');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
treeview1.Selected.Delete ;
end;
with adoquery1 do //未指定上課講師的課程
begin
first;
while not eof do
報表製作
• TQuickRep:共含頁首頁尾、表頭表尾、欄位
標題、資料內容六個bend,可於bends的屬性中
設定是否出現。
• 相關內容可快點兩下後於report settings 中設定
• TQRSysData:系統的資訊元件,如系統時間、
頁數等,可於其Data屬性中查到
• TQRDBText:顯示資料庫資料的元件,可於
DataSet、DataFeild中設定
• 分組小計的功能:製作一個列出每個老師上課
時數的報表
– 新增一個Query1,於sql屬性中加入“select
統計圖表TChart
• 做一個所有老師上課時數的統計圖表
• 新增一個chart1,並於Chart1上新增一個
series,將series的Title改為上課時數。
• 新增一個Query1,並於sql 屬性中加入
select teacher_name,sum(class_hour) as
sum_hour,sum(student_num) as sum_student
from class,teacher
where class.teacher_id=teacher.teacher_id
group by teacher_name
統計圖表TDBChart
• 同上例,將Tchart改成TDBChart,於
TDBChart1上新增一個Series1,選擇
piechart
• 設定Series1的Dataset為Query1
• 新增一個RadioGroup1,並於Items的屬性
中加入“上課總時數”及“上課總人數
• 於RadioGroup1的Click事件中加入
Series1.xlabelsSource:=query1.fields[0].fieldnam
e;
install shield
Datasset 的Cache Update 功能
• 當dataset active時,並沒有與後端的資料
庫做即時的更新,等整批的資料異動完
成時,再做一次整批的異動,以減少網
路的傳輸。
• 呼叫ApplyUpdates 即可真正的將Cache的
資料更新至資料庫
• 製作一個table1、datasource1、navigator1、
dbgrid1,並將table1的cachedUpdated設為
true,及連上class資料庫