參看25樓附件的示例(已編譯為獨立可執行程序,內嵌數據庫)。
左邊是主表,右邊是子表。當光標在左邊移動時,右邊的子表記錄指針也跟著走,但光標在右邊移動,左邊的主表記錄指針不會變。這個是一對多關聯,主表不需有索引,但子表必須有索引。
該程序的代碼如下:
程序代码:
#INCLUDE "FoxPro.h"
SET PATH TO "E:\我的文檔\項目\myTools\VFP"
SET PROCEDURE TO "myTools"
myInit()
Main()
RETURN
*-----------------------------
* 程序主入口
*-----------------------------
PROCEDURE Main()
LOCAL loMainForm
loMainForm = CREATEOBJECT("_MainForm")
WITH loMainForm
.WindowState = WINDOWSTATE_MAXIMIZED
.Caption = "測試 - SET RELATION TO"
.Show
ENDWITH
READ EVENTS
ENDPROC
*-----------------------------
* 主窗口類定義
*-----------------------------
DEFINE CLASS _MainForm AS myForm
ShowWindow = 2 && 頂層窗口
ADD OBJECT Label1 AS Label WITH Caption = "主表"
ADD OBJECT Grid1 AS Grid
ADD OBJECT Label2 AS Label WITH Caption = "子表"
ADD OBJECT Grid2 AS Grid
PROCEDURE Load
* 載入數據環境
OPEN DATABASE "test"
USE "table1" IN 0
USE "table2" ORDER "t01" IN 0
SET RELATION TO t01 INTO "table2" IN "table1" && 在table1中關聯到table2,關聯的索引為t01標籤
ENDPROC
PROCEDURE Unload
* 關閉數據環境
SET DATABASE TO "test"
CLOSE DATABASES
ENDPROC
PROCEDURE Init
WITH ThisForm
*-----------------
* 批量設置控件屬性
*-----------------
.SetAll("FontName", "微软雅黑", "Label")
.SetAll("FontSize", 16, "Label")
.SetAll("FontBold", .T., "Label")
.SetAll("AutoSize", .T., "Label")
.SetAll("Height", 30, "Label")
ENDWITH
ENDPROC
*-------------------------
* 對表單內控件進行佈局,隨窗體尺寸變化而自動變化,其事件由本表單父類響應
*-------------------------
PROCEDURE Arrange
WITH ThisForm.Label1
.Top = 5
.Left = 5
ENDWITH
WITH ThisForm.Grid1
.Top = ThisForm.Label1.Top + ThisForm.Label1.Height + 5
.Left = 5
.Height = ThisForm.Height - .Top - 5
.Width = (ThisForm.Width / 2) - .Left - 5
ENDWITH
WITH ThisForm.Label2
.Top = 5
.Left = (ThisForm.Width / 2) + 5
ENDWITH
WITH ThisForm.Grid2
.Top = ThisForm.Label2.Top + ThisForm.Label2.Height + 5
.Left = (ThisForm.Width / 2) + 5
.Height = ThisForm.Height - .Top - 5
.Width = ThisForm.Width - .Left - 5
ENDWITH
ENDPROC
PROCEDURE Grid1.Init
WITH This
.RecordSourceType = 1
.RecordSource = "table1"
.HighlightStyle = 2
.ColumnCount = 1
WITH .Columns(1)
WITH .Header1
.Caption = FIELD("t02", "table1", 1)
.FontBold = .T.
ENDWITH
.ControlSource = "table1.t02"
ENDWITH
ENDWITH
ENDPROC
PROCEDURE Grid2.Init
WITH This
.RecordSourceType = 1
.RecordSource = "table2"
.HighlightStyle = 2
.ColumnCount = 1
WITH .Columns(1)
WITH .Header1
.Caption = FIELD("s02", "table2", 1)
.FontBold = .T.
ENDWITH
.ControlSource = "table2.s02"
ENDWITH
ENDWITH
ENDPROC
*-------------------------
* Grid1控件光標穩定之後的處理
*-------------------------
PROCEDURE Grid1.AfterRowColChange(tnColumnIndex)
This.Parent.Grid2.Refresh && 刷新Grid2控件中的光標位置
ENDPROC
ENDDEFINE
[
本帖最后由 TonyDeng 于 2014-4-1 03:21 编辑 ]