分享

看实例学VFP:页框控件的使用

 lygcw9603 2011-12-07

页框(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.
this.MaxButton=.f. &&屏蔽表单的最大化按钮
this.MinButton=.f. &&屏蔽表单的最小化按钮
this.pageframe1.activepage=2 &&指定第二页为当前页
set exact on
use 网站信息表
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=150
endwith
with 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=150
endwith
this.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 top
a=thisform.pageframe1.page1.Combo1.value
b=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.setfocus
else
   messagebox("请输入要查找的内容!",16,"系统提示")
   thisform.pageframe1.page1.Text1.value=""
   thisform.pageframe1.page1.Text1.Setfocus
endif
return3、“恢复”按钮command2的click事件代码:

go top
thisform.pageframe1.page1.Text1.value=""
thisform.pageframe1.page1.Text1.Setfocus
with 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=150
endwith
thisform.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,"系统提示")
   return
else
  locate for 网站名称=b
  if .not.eof() &&此条件成立,说明要添加的内容和表中有重复的
      go top
      messagebox("不允许有重复的网站名称",16,"系统提示")
      thisform.pageframe1.page2.Text1.setfocus
      return
  endif
endif
if empty(c)
   messagebox("请输入网站网址",16,"系统提示")
   return
else
  locate for 网站网址=c
  if .not.eof()
      go top
      messagebox("不允许有重复的网站网址",16,"系统提示")
      thisform.pageframe1.page2.Text1.setfocus
      return
  endif
endif
if 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
  endif
else
  messagebox('请输入网站的编号!',16,'系统提示')
endif3、“退出”按钮command2的click事件代码:

msg=messagebox('确定要退出系统吗?',32+4,'系统提示')
if msg=6
 thisform.release
endif七、运行“表单1.scx”。

本例代码在Win2003+VFP6.0环境下调试通过。


本文来自编程入门网:http://www./Programming/vfp/200904/11324.htm

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多