pb編程通俗快速入手
張金柱
第一章 一般使用
1、 程序的開始,application的open事件。
退出程序例程:halt為退出函數
int surequit
surequit = 2
surequit=messagebox("退出系統","退出前請確認已保存好數據",question!,okcancel!, 2)
if surequit = 1 then halt
2、 變量定義有效范圍:
◎ declare-globe 全局變量,整個程序均有效
◎ declare-instance 局部變量,可在一個對象中有效(如:窗體,app等)
◎ 模塊中定義的變量,可在當前模塊中有效
定義:(pb中大小寫不敏感)
constant string ls_homecity = "boston" 常量
int a 整型
char c 字符型
boolean b 布爾型
string a 字符串型
string a[1000] 1000的數組,0~1000
string a[3 to 30] 數組元素為3到30,最初一個為a[3],最后一個為a[30]
string a [100,3to 30] 二維數組,一維為0~100,二維為3 到30
string a[] 變長數組,賦值時(如a[30]=333)自動分配內存。
得到上界upperbound,下界lowerbound。
操作:
a=”aaaa”+”vvvbb”+b+c
string(b) 將b轉換成字符串型
integer(“333333333333”) 將“333333333333”字符串轉換成數333333333333
a=a+b*c^d/e/f%g
◎注意:減號前后要空格,用于避免pb7.0之前版本的bug。
3、 函數定義:
第一行是返回類型和函數名,第二行是變量類型和變量名,tab鍵加形參。
return 是函數的返回,同時如c語言一樣,return則函數運行結束
4、 打開窗口
open(窗口名)
openwithparm(窗口名,參數,父窗口<僅對子窗體和彈出窗體有效>)
5、 mdi窗體中打開子窗體
層疊樣式打開 opensheet(子窗體,父窗體名,1,layered!)
平鋪樣式打開 opensheet(子窗體,父窗體名,1, original!)
如果在父窗體的窗體級函數下編寫,可用 opensheet(子窗體,this,1, original!)
第三個參數表示新開窗體在父窗體菜單中的位置。
6、 控件基本屬性
控件名.x x坐標
控件名.y y坐標
控件名.width 寬
控件名.height 高…… (其他可參見編輯器中的屬性窗口)
7、 調整窗體模式
this.windowstate=maximized!
窗體.windowstate=maximized! 窗體最大化(其他可參見編輯器中的屬性窗口)
8、 ini文件讀取及寫入
【舉例】有名為“mnr.ini”的ini文件,(路徑在程序所在目錄)有數據如下:
[action]
preload=yes
可以用如下語句讀取:
a=profilestring("mnr.ini","action","preload","aaa")
a為變量,”aaa”為無此數據項時默認數據
可以用如下語句寫入(寫入值為yes):
setprofilestring("mnr.ini","action","preload","yes")
9、 對于整個程序中都要用到的變量
可以先定義一個結構,再在globe declare中定義該結構類型變量,管理起來方便些
10、 主要語法單元
條件判斷
if 怎樣 then
怎樣做
else if 怎樣 then
怎樣做
end if
choose case 變量
case is <3
…
case 4 to 7
…
case else
…
end choose
循環到a大于5
do
a=a+1
loop until a > 5
do while a <= 5
a = a + 1
loop
do
a=a+1
loop until a > 5
do until a > 5
a = a + 1
loop
跳出循環exit,繼續下一輪循環continue
11、 消息框
messagebox(標題,信息) 其他看幫助,查索引messagebox
12、取得當前時間now()
sle_begintime.text=string(now(),"yyyy-mm-dd 10:00:00")
如現在是 2002-7-14 17:22:21秒,則輸出為 2002-7-14 10:00:00
13、列表框應用
ddlb_bsc.reset() 清空列表框內容
i=1
do while i<=upperbound(winbsc)
if winbsc[i]="" then exit;
ddlb_bsc.additem(winbsc[i]) 增加內容項到列表框
i=i+1
loop
第二章 應用數據庫
1、 datawindow控件的使用
先創建一個datawindow對象dw_hwtj,再在窗體中添加一個datawindow控件dw_rep,最后將datawindow的dataobject屬性設置成dw_hwtj。
a)我寫的選行函數,datawindow控件的click事件上用
if row=0 then return
if nowrow<>row and nowrow>=0 then
this.selectrow(nowrow,false)
end if
this.selectrow(row,true)
nowrow=row
b)我寫的排序函數,在datawindow控件的雙擊事件上用
if dwo.type = "column" then
ls_columnname = dwo.name
end if
if nowstate='a' then
this.setsort(ls_columnname+" d")
nowstate='d'
else
this.setsort(ls_columnname+" a")
nowstate='a'
end if
this.sort()
(效果,在某一列上雙擊一次,正向排序,再一次,反向排序)
c)規定排序
dw_rep.setsort("bscmc a,xqh a,xqmc a")
dw_rep.sort()
(先按bscmc,再按xqh,再按xqmc正向排序)
d)保存到xls文件
dw_rep.saveas('',excel5!,true)
e)讀寫:
寫w_netrep_cell.dw_rep.object.tchpzzs[i] =a
讀a=w_netrep_cell.dw_rep.object.tchpzzs[i]
2、 建立與數據庫的連接
1)定義
transaction localdb
2)設置
mytrans.dbms=”as”
mytrans.database=”sda”
mytrans.logpass=”def”
mytrans.servername=”dbo”
mytrans.logid=”abc”
mytrans.dbparm=””
mytrans.lock=””
mytrans.userid=”abc”
mytrans.autocommit =true
3)連接
連接 connect using localdb;
斷開 disconnect using localdb;
4)從庫中查詢多行數據:(查詢語句被固化)
connect using localdb; //連接
if localdb.sqlcode<0 then
messagebox("連接本地數據庫失敗...",localdb.sqlerrtext)
return 1
end if
declare my_cursor cursor for //定義游標
select bsc from bsc order by bsc using localdb; //執行查詢
i=1;
open my_cursor; //打開游標
do while not localdb.sqlcode = 100 //直到結束
if i>upperbound(winbsc) then exit
fetch my_cursor into :winbsc[i]; //提取數據項
i=i+1
loop
close my_cursor; //關閉游標
disconnect using localdb; //斷開
5)從庫中查詢一行數據:(查詢語句被固化)help中例程,“:“后加一個字符串是指標志為該字符串的變量
select employee.emp_lname, employee.emp_fname
into :emp_lname, :emp_fname
from employee
where employee.emp_nbr = :emp_num
using emp_tran ;
6)在數據更新,刪除,增加,建表等操作中使用的:
注意查閱:dynamic sql format 1 statement ……dynamic sql format 4 statement,其中講得很清楚(看sample)
直接執行一條語句:
execute immediate sqlstatement {using transactionobject} ;
help中講得很清楚,這里就不多講了。
7)非固化的查詢語句:這個對提高軟件靈活性很重要,但很簡單,只貼上help中的sample一個:
declare my_cursor dynamic cursor for sqlsa
string sql1, sql2
sql1 = "select emp_id from department where salary > 90000"
sql2 = "select emp_id from department & <――這是分行連接符號,空格后一個&符號
where salary > 20000"
if deptid = 200 then
prepare sqlsa from :sql1 using sqlca ;
else
prepare sqlsa from :sql2 using sqlca ;
end if
open dynamic my_cursor ;
小結:
內容不多,但主要的東西都在這里了,其他的要在實踐中領會,這些知識對上手和標準的mis系統等開發,知識面及深度已經足夠了。sql語句幫助中的已夠用,詳細講太多,恕不贅述。
2002-7-14