程序代碼:CLEAR ALL
CLOSE DATABASES ALL SET DEFAULT TO (Application.ActiveProject.HomeDir) Form1 = NEWOBJECT("C_Form") && 從類(lèi)定義中C_Form中生成窗體 Form1.Show && 顯示剛才生成的窗體 READ EVENTS && 激活交互事件 CLOSE DATABASES ALL CLEAR ALL RETURN && 程序結(jié)束 *----------------------------- * 程序運(yùn)行窗體的類(lèi)定義 *----------------------------- DEFINE CLASS C_Form AS Form Caption = "自定義按鈕測(cè)試" && 窗體標(biāo)題 WindowState = 2 && 啟動(dòng)時(shí)最大化 ADD OBJECT Grid1 AS Grid && 在窗體中放置一個(gè)Grid *------------------------- * 窗體載入內(nèi)存時(shí)打開(kāi)相關(guān)的數(shù)據(jù)表 *------------------------- PROCEDURE Load USE Table1 IN 0 ENDPROC *------------------------- * 窗體從內(nèi)存中卸載時(shí)關(guān)閉已打開(kāi)的數(shù)據(jù)表 *------------------------- PROCEDURE Unload USE IN Table1 ENDPROC *------------------------- * 自定義對(duì)窗體的控件進(jìn)行布局的方法 *------------------------- PROCEDURE Arrange WITH This.Grid1 .Top = 5 .Left = 5 .Width = ThisForm.Width - .Left - 5 .Height = ThisForm.Height - .Top - 5 ENDWITH ENDPROC *------------------------- * 窗體激活或獲得焦點(diǎn)時(shí) *------------------------- PROCEDURE Activate ThisForm.Arrange ENDPROC *------------------------- * 窗體改變大小時(shí) *------------------------- PROCEDURE Resize ThisForm.Arrange ENDPROC *------------------------- * 關(guān)閉窗體時(shí)釋放交互事務(wù) *------------------------- PROCEDURE Destroy CLEAR EVENTS ENDPROC *------------------------- * 表格的初始化 *------------------------- PROCEDURE Grid1.Init WITH This .RecordSourceType = 1 && 數(shù)據(jù)源為表別名 .RecordSource = "Table1" && 綁定數(shù)據(jù)源 WITH .Columns(3) && 對(duì)具體數(shù)據(jù)表中的某列細(xì)化 .RemoveObject("Text1") && 移除該欄中內(nèi)置的TextBox控件 .AddObject("Button1", "GridCommandButton") && 新增自定義控件 WITH .Button1 .ControlSource = "Table1.F03" && 綁定本欄的數(shù)據(jù)源 .TrueCaption = "Yes" && 修改預(yù)定義的文字 .FalseCaption = "No" && 修改預(yù)定義的文字 .Width = 60 && 設(shè)置控件寬度 .Height = 20 && 設(shè)置控件高度 .Visible = .T. && 讓控件可見(jiàn) ENDWITH .CurrentControl = "Button1" && 設(shè)定本欄的控制控件 .Sparse = .F. && 每次Refresh時(shí)Grid所有行中本欄的數(shù)據(jù)均刷新 .Width = 60 && 本欄的寬度與新控件匹配 ENDWITH .RowHeight = 22 && 表格行的高度與新控件匹配 ENDWITH ENDPROC ENDDEFINE *----------------------------- * 功能:嵌入Grid中的CommandButton,可與邏輯型數(shù)據(jù)綁定 * 機(jī)制:從Container中繼承,構(gòu)造一個(gè)包含隱藏TextBox和呈現(xiàn)CommandButton的 * 自定義控件。TextBox與平常Grid Cell的一樣綁定數(shù)據(jù),把需要提供的 * 數(shù)據(jù)轉(zhuǎn)交給CommandButton,以便呈現(xiàn)。 * 注意:數(shù)據(jù)綁定源須為邏輯型,否則失效或出錯(cuò)。 *----------------------------- DEFINE CLASS GridCommandButton AS Container BorderWidth = 0 && 取消容器的邊框線 *------------------------- * 自定義屬性 ControlSource = "" && 數(shù)據(jù)綁定源 TrueCaption = "是" && 當(dāng)數(shù)據(jù)值為.T.時(shí)CommandButton的文字 FalseCaption = "否" && 當(dāng)數(shù)據(jù)值為.F.時(shí)CommandButton的文字 *------------------------- *------------------------- * 內(nèi)部受保護(hù)控件 *------------------------- ADD OBJECT PROTECTED Text1 AS TextBox WITH Visible = .F. ADD OBJECT PROTECTED Button1 AS CommandButton PROCEDURE Width_Assign(tAssign) This.Button1.Width = tAssign ENDPROC PROCEDURE Height_Assign(tAssign) This.Button1.Height = tAssign ENDPROC PROCEDURE ControlSource_Assign(tAssign) WITH This.Text1 .ControlSource = tAssign .Value = EVALUATE(tAssign) ENDWITH ENDPROC PROCEDURE Text1.Value_Assign(tAssign) This.Value = tAssign WITH This.Parent.Button1 .VisualEffect = IIF(tAssign, 2, 1) .Caption = IIF(This.Parent.Text1.Value, This.Parent.TrueCaption, This.Parent.FalseCaption) ENDWITH ENDPROC *------------------------- * 點(diǎn)擊按鈕時(shí)翻轉(zhuǎn)值的真假 *------------------------- PROCEDURE Button1.Click WITH This.Parent.Text1 .Value = !.Value ENDWITH ENDPROC ENDDEFINE |
|
|