頁框(pageframe) 也是vfp的一個基本控制項,使用它可以製作出類似於windows對話方塊的效果,關於這個控制項的常用屬性和方法請參考vfp基礎教程-頁框和頁一文。
頁框控制項本身是一個容器,其中可以包含若干個頁(page);而頁也是容器,可以在其中添加各種對象,因此無論是在設計時添加對象或者是代碼引用,都要比別的控制項麻煩些。在使用這個控制項時需要注意:如果設計時要向某個頁中添加對象,一定要在頁框的編輯狀態下選中該頁的標籤,然後再向該頁中添加控制項;另外在不同頁之間切換時,需要處理好資料來源的問題。
剛接觸這個控制項的同志可能經常會碰到這樣一個問題:即使在頁框中只設定了兩頁,這兩頁的標題寬度也會平均分配並且佔滿整個頁框的寬度;而在標準windows對話方塊中,無論有多少頁,頁面標題都是靠左對齊排列的,如下圖:
這個問題很好解決,把頁框的TabStyle屬性值設為“1-非兩端”就可以出來這種靠左對齊的效果。
本文來設計一個頁框控制項的應用執行個體,在本例中通過頁框控制項的兩個頁實現了看執行個體學VFP:對錶中記錄進行定位與尋找操作和看執行個體學VFP:向資料表中添加記錄並驗證輸入資料是否合法這兩個例子的功能,運行時如下圖:
製作步驟如下:
一、建立表單,將其caption屬性值設定為“編程入門網-頁框控制項”,AutoCenter屬性值設定為.t.,儲存為“表單1.scx”。
二、向表單上添加一個頁框控制項pageframe1,將其pagecount屬性值設為2,TabStyle屬性值設為“1-非兩端”。
三、右擊頁框控制項選“編輯”命令,在頁框的編輯狀態下選中page1,將其caption屬性值設定為“尋找記錄”,按照看執行個體學VFP:對錶中記錄進行定位與尋找操作一文向page1添加相同的控制項並為各個控制項設定相同的屬性。
四、在頁框的編輯狀態下選中page2,將其caption屬性值設定為“添加記錄”,按照看執行個體學VFP:向資料表中添加記錄並驗證輸入資料是否合法一文向page2添加相同的控制項並為各個控制項設定相同的屬性。
五、設定完畢後的表單設計器如下圖:
六、添加事件代碼:
(一)表單的init事件代碼:
&& coded by www.bianceng.cnthis.MaxButton=.f. &&屏蔽表單的最大化按鈕this.MinButton=.f. &&屏蔽表單的最小化按鈕this.pageframe1.activepage=2 &&指定第二頁為當前頁set exact onuse 網站資訊表this.pageframe1.page1.combo1.value="編號"with this.pageframe1.page1.grid1 .width=290 .height=100 .left=0 .top=0 .recordsource="網站資訊表" .deletemark=.f. .visible=.t. .readonly=.t. .ColumnCount=3 .Column1.Header1.Caption="編號" .Column1.Header1.BackColor=RGB(255,255,190) .Column2.Header1.BackColor=RGB(255,255,190) .Column2.Header1.Caption="網站名稱" .Column3.Header1.BackColor=RGB(255,255,190) .Column3.Header1.Caption="網站網址" .Column1.width=75 .Column2.width=80 .Column3.width=150endwithwith this.pageframe1.page2.grid1 .width=290 .height=100 .left=0 .top=0 .recordsource="網站資訊表" .deletemark=.f. .visible=.t. .readonly=.t. .ColumnCount=3 .Column1.Header1.Caption="編號" .Column1.Header1.BackColor=RGB(255,255,190) .Column2.Header1.BackColor=RGB(255,255,190) .Column2.Header1.Caption="網站名稱" .Column3.Header1.BackColor=RGB(255,255,190) .Column3.Header1.Caption="網站網址" .Column1.width=75 .Column2.width=80 .Column3.width=150endwiththis.pageframe1.page1.grid1.Setall("DynamicBackColor","RGB(224,225,255)","Column")this.pageframe1.page2.grid1.Setall("DynamicBackColor","RGB(224,225,255)","Column")
(二)表單的unload事件代碼:
close all
set exact off
(三)page1(“尋找記錄”)中各對象的事件代碼:
1、page1的Activate事件代碼:
this.refresh
2、“尋找”按鈕command1的click事件代碼:
if empty(thisform.pageframe1.page1.Text1.value)=.f.go topa=thisform.pageframe1.page1.Combo1.valueb=alltrim(thisform.pageframe1.page1.Text1.value) if a="編號" locate for 編號=b if eof() messagebox("資料庫中不存在您所要查詢的記錄",16,"系統提示") else go recno() endif endif if a="網站名稱" locate for 網站名稱=b if eof() messagebox("資料庫中不存在您所要查詢的記錄",16,"系統提示") else go recno() endif endif if a="網站網址" locate for 網站網址=b if eof() messagebox("資料庫中不存在您所要查詢的記錄",16,"系統提示") else go recno() endif endif thisform.pageframe1.page1.grid1.setfocuselse messagebox("請輸入要尋找的內容!",16,"系統提示") thisform.pageframe1.page1.Text1.value="" thisform.pageframe1.page1.Text1.Setfocusendifreturn
3、“恢複”按鈕command2的click事件代碼:
go topthisform.pageframe1.page1.Text1.value=""thisform.pageframe1.page1.Text1.Setfocuswith thisform.pageframe1.page1.grid1 .width=290 .height=100 .left=0 .top=0 .recordsource="網站資訊表" .visible=.t. .readonly=.t. .ColumnCount=3 .Column1.Header1.Caption="編號" .Column1.Header1.BackColor=RGB(255,255,190) .Column2.Header1.BackColor=RGB(255,255,190) .Column2.Header1.Caption="網站名稱" .Column3.Header1.BackColor=RGB(255,255,190) .Column3.Header1.Caption="網站網址" .Column1.width=75 .Column2.width=80 .Column3.width=150endwiththisform.pageframe1.page1.grid1.Setall("DynamicBackColor","RGB(224,225,255)","Column")thisform.pageframe1.page1.refresh
(四)page2(“添加記錄”)中各對象的事件代碼:
1、page2的Activate事件代碼:
this.refresh
2、“添加”按鈕command1的click事件代碼:
a=alltrim(thisform.pageframe1.page2.Text1.value)b=alltrim(thisform.pageframe1.page2.Text2.value)c=alltrim(thisform.pageframe1.page2.Text3.value)if empty(b) &&此條件成立則說明該文字框是空的 messagebox("請輸入網站名稱",16,"系統提示") returnelse locate for 網站名稱=b if .not.eof() &&此條件成立,說明要添加的內容和表中有重複的 go top messagebox("不允許有重複的網站名稱",16,"系統提示") thisform.pageframe1.page2.Text1.setfocus return endifendifif empty(c) messagebox("請輸入網站網址",16,"系統提示") returnelse locate for 網站網址=c if .not.eof() go top messagebox("不允許有重複的網站網址",16,"系統提示") thisform.pageframe1.page2.Text1.setfocus return endifendifif empty(a)=.f. locate for 編號=a if .not.eof() go top messagebox("不允許有重複的網站編號",16,"系統提示") thisform.pageframe1.page2.Text1.setfocus return else msg=messagebox('確定要添加記錄嗎?',32+4,'系統提示') if msg=6 append blank go bottom replace 網站資訊表.編號 with alltrim(thisform.pageframe1.page2.Text1.value),; 網站資訊表.網站名稱 with alltrim(thisform.pageframe1.page2.Text2.value),; 網站資訊表.網站網址 with alltrim(thisform.pageframe1.page2.Text3.value) messagebox('儲存記錄成功!',64,'系統提示') thisform.pageframe1.page2.Text1.value="" thisform.pageframe1.page2.Text2.value="" thisform.pageframe1.page2.Text3.value="" thisform.pageframe1.page2.refresh endif endifelse messagebox('請輸入網站的編號!',16,'系統提示')endif
3、“退出”按鈕command2的click事件代碼:
msg=messagebox('確定要退出系統嗎?',32+4,'系統提示')if msg=6 thisform.releaseendif
七、運行“表單1.scx”。
本例代碼在Win2003+VFP6.0環境下調試通過。
查看全套“菜鳥也學VFP”教程