程序代码:
CLEAR ALL CLOSE DATABASES ALL goForm = CREATEOBJECT("my_Form") && 表单实体goForm由类my_Form生成 goForm.Show READ EVENTS CLOSE DATABASES ALL CLEAR ALL RETURN *----------------------------- * 表单类定义 *----------------------------- DEFINE CLASS my_Form AS Form *------------------------- * 属性设置 *------------------------- Caption = "表格测试程序" && 表单标题文字 ShowWindow = 0 && 表单在_SCREEN窗体内 MinWidth = 800 && 表单最小宽度 MinHeight = 600 && 表单最小高度 AutoCenter = .T. && 表单启动时自动居中 ADD OBJECT Grid1 AS Grid WITH DeleteMark = .F., HighlightStyle = 2 ADD OBJECT Label1 AS Label WITH Height = 25, AutoSize = .T. ADD OBJECT Label2 AS Label WITH Caption = "修改本列内容请留意观察现象", Height = 25, Width = 200, Visible = .F. *------------------------- * 自定义方法:排列表单内控件 *------------------------- PROCEDURE Arrange WITH ThisForm.Label1 .Top = ThisForm.Height - .Height - 5 .Left = 20 ENDWITH WITH ThisForm.Label2 .Top = ThisForm.Label1.Top .Left = ThisForm.Width - .Width - 5 ENDWITH WITH ThisForm.Grid1 .Top = 5 .Left = 5 .Height = ThisForm.Label1.Top - .Top - 5 .Width = ThisForm.Width - .Left - 5 ENDWITH ENDPROC *------------------------- * 表单装载入内存时激发 *------------------------- PROCEDURE Load *--------------------- * 随便打开一个数据表进行测试 *--------------------- USE unicode_gbk ALIAS test EXCLUSIVE IN 0 ENDPROC *------------------------- * 表单被激活时激发 *------------------------- PROCEDURE Activate ThisForm.Arrange && 调用自定义方法 ENDPROC *------------------------- * 表单被改变尺寸时激发 *------------------------- PROCEDURE Resize ThisForm.Arrange && 调用自定义方法 ENDPROC *------------------------- * 关闭表单时激发 *------------------------- PROCEDURE Destroy CLEAR EVENTS && 停止事务处理 ENDPROC *------------------------- * 表单从内存卸载时激发 *------------------------- PROCEDURE Unload USE IN test ENDPROC *------------------------- * 用自编代码覆盖表格Grid1的默认Init事件 *------------------------- PROCEDURE Grid1.Init WITH This .RecordSourceType = 1 .RecordSource = "test" WITH .Columns(1) && 对表格的第1列设置 .Width = 100 && 列宽度 .ControlSource = "test.gbk" && 绑定表格列的数据源 BINDEVENT(.Text1, "DblClick", This, "my_DblClick") && 绑定鼠标双击事件 BINDEVENT(.Text1, "InteractiveChange", This, "my_InteractiveChange") && 绑定内容变化事件 ENDWITH WITH .Columns(2) && 对表格的第2列设置 .Width = 100 && 列宽度 .ControlSource = "test.unicode" && 绑定表格列的数据源 *----------------------- * 如果愿意,可以同样设置该列的响应事件 *----------------------- BINDEVENT(.Text1, "DblClick", This, "my_DblClick") && 绑定鼠标双击事件 ENDWITH ENDWITH ENDPROC *------------------------- * 自定义响应单元格双击事件的代码 * 注意:如果设置了响应Click事件,则会屏蔽DblClick事件 *------------------------- PROCEDURE Grid1.my_DblClick MESSAGEBOX("您在单元格(" + ALLTRIM(STR(This.ActiveRow)) + "," + ALLTRIM(STR(This.ActiveColumn)) + ")双击了鼠标", 64, ThisForm.Caption) ENDPROC *------------------------- * 自定义响应单元格内容改变事件的代码 *------------------------- PROCEDURE Grid1.my_InteractiveChange ThisForm.Label1.Refresh ENDPROC *------------------------- * 表格光条稳定在某行后激发 *------------------------- PROCEDURE Grid1.AfterRowColChange(tnColumn) ThisForm.Label1.Refresh && 刷新Label1的内容 ThisForm.Label2.Visible = (tnColumn == 1) && Label2在光标处于第1列时才可见 ENDPROC *------------------------- * 标签Label1的Refresh事件 *------------------------- PROCEDURE Label1.Refresh This.Caption = "单元格(" + ALLTRIM(STR(ThisForm.Grid1.ActiveRow)) + ",1)的内容:" + ThisForm.Grid1.Columns(1).Text1.Value ENDPROC ENDDEFINE
授人以渔,不授人以鱼。