注册 登录
编程论坛 VFP论坛

表单事件方法

tjdeming 发布于 2023-08-28 19:51, 1255 次点击
只有本站会员才能查看附件,请 登录
在单个表单中的四个页框都会用到下面三个变量
请教如何设置:即不用在每个页框中写以下同样的语句。谢谢!!!
nText= thisform.text1.value
st=
只有本站会员才能查看附件,请 登录

et=
nText = thisform.text1.value
     st =
     et =


[此贴子已经被作者于2023-8-28 19:57编辑过]

30 回复
#2
laowan0012023-08-28 20:25
在form1.activate里,
PRIVATE ntext,et,st 之前这句是错误的,更正如下:
public ntext,et,st
在四个页框里直接引用变量

[此贴子已经被作者于2023-8-29 14:26编辑过]

#3
tjdeming2023-08-29 08:08
回复 2楼 laowan001
谢谢你,但没有成功。
只有本站会员才能查看附件,请 登录
#4
tjdeming2023-08-29 08:14
4个页框都有这样的语句,能简化吗?
只有本站会员才能查看附件,请 登录
#5
laowan0012023-08-29 09:20
以下是引用tjdeming在2023-8-29 08:08:46的发言:

谢谢你,但没有成功。

没有成功,是报错了吗?
#6
tjdeming2023-08-29 09:30
回复 5楼 laowan001
没有报错,设置的变量没有反应
#7
laowan0012023-08-29 10:29
以下是引用tjdeming在2023-8-29 09:30:55的发言:

没有报错,设置的变量没有反应

应该有什么反应?
#8
sam_jiang2023-08-29 10:37
回复 7楼 laowan001
他的变量是private型的,怎么可能起作用?

设置成public还差不多。
#9
tjdeming2023-08-29 10:54
回复 8楼 sam_jiang
换了public,也没用
#10
tjdeming2023-08-29 10:56
回复 7楼 laowan001
Ntext变量的值应该传递给page1的click
#11
tjdeming2023-08-29 10:58
回复 7楼 laowan001
Ntext变量的值传递给page1的click
#12
sam_jiang2023-08-29 11:31
回复 9楼 tjdeming
page的click事件里,是不是有应该对page里要用的地方赋值?
this.xxxx.value=ntext
this.xxxx.value=srq
...
#13
tjdeming2023-08-29 12:53
回复 12楼 sam_jiang
原先是有的
#14
laowan0012023-08-29 14:23
以下是引用sam_jiang在2023-8-29 10:37:20的发言:

他的变量是private型的,怎么可能起作用?

设置成public还差不多。

是我搞错了,应该是public
#15
laowan0012023-08-29 14:25
以下是引用tjdeming在2023-8-29 10:58:32的发言:

Ntext变量的值传递给page1的click


更正一下,应该是public(不是PRIVATE),在page1的click里可以直接使用变量Ntext
#16
tjdeming2023-08-29 15:21
回复 15楼 laowan001
谢谢你!变量没有引用过来
#17
sam_jiang2023-08-29 16:12
建议你,每个page的click事件调用pageframe的click事件,然后把你每个page的click事件写进pageframe的click里面,这样就不用每个页面都写那几句了。

每个page的click代码就下面这句:
this.parent.click

你原来的代码,写到pageframe里去,这样可以省掉很多行代码
#18
z126892023-08-29 17:43
以下是引用sam_jiang在2023-8-29 16:12:45的发言:

建议你,每个page的click事件调用pageframe的click事件,然后把你每个page的click事件写进pageframe的click里面,这样就不用每个页面都写那几句了。

每个page的click代码就下面这句:
this.parent.click

你原来的代码,写到pageframe里去,这样可以省掉很多行代码


学习了
#19
tjdeming2023-08-29 19:29
回复 17楼 sam_jiang
我的错在哪儿呢?
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
只有本站会员才能查看附件,请 登录
#20
sam_jiang2023-08-29 20:02
回复 19楼 tjdeming
你把文件发上来,ntext的作用域是public吗?
#21
tjdeming2023-08-29 20:43
回复 20楼 sam_jiang
只有本站会员才能查看附件,请 登录

#22
tjdeming2023-08-29 20:46
增加了代码
只有本站会员才能查看附件,请 登录
#23
tjdeming2023-08-29 20:53
回复 17楼 sam_jiang
文件已上传,现在变量能引用过来了。我取消了this.parent.click,也能成功引用,不懂其中的道理。请你指点一下,label4的赋值没有成功的原因。谢谢你!
#24
laowan0012023-08-30 11:02
以下是引用tjdeming在2023-8-29 15:21:52的发言:

谢谢你!变量没有引用过来


不能够啊,page1.click 这样试下
messagebox(ntext)
#25
sam_jiang2023-08-30 13:12
以下是引用tjdeming在2023-8-29 20:53:30的发言:

文件已上传,现在变量能引用过来了。我取消了this.parent.click,也能成功引用,不懂其中的道理。请你指点一下,label4的赋值没有成功的原因。谢谢你!


说实话不知道你要完成什么任务,只是要传递那3个值给page的caption?

**************************************************
*-- Form:         form1 (d:\documents\visual foxpro 项目\表单变量\表单1.scx)
*-- ParentClass:  form
*-- BaseClass:    form
*-- Time Stamp:   08/29/23 08:38:00 PM
*
DEFINE CLASS form1 AS form


    Top = 88
    Left = 85
    Height = 329
    Width = 509
    DoCreate = .T.
    Caption = "Form1"
    Name = "Form1"


    ADD OBJECT pageframe1 AS pageframe WITH ;
        ErasePage = .T., ;
        PageCount = 4, ;
        Top = 108, ;
        Left = 36, ;
        Width = 444, ;
        Height = 204, ;
        Name = "Pageframe1", ;
        Page1.Caption = "Page1", ;
        Page1.Name = "Page1", ;
        Page2.Caption = "Page2", ;
        Page2.Name = "Page2", ;
        Page3.Caption = "Page3", ;
        Page3.Name = "Page3", ;
        Page4.Caption = "Page4", ;
        Page4.Name = "Page4"


    ADD OBJECT text1 AS textbox WITH ;
        Alignment = 3, ;
        Value = 0, ;
        Height = 25, ;
        InputMask = "", ;
        Left = 97, ;
        Top = 12, ;
        Width = 96, ;
        Name = "Text1"


    ADD OBJECT label1 AS label WITH ;
        AutoSize = .T., ;
        Caption = "输入编号", ;
        Height = 16, ;
        Left = 24, ;
        Top = 20, ;
        Width = 50, ;
        Name = "Label1"


    ADD OBJECT combo1 AS combobox WITH ;
        RowSourceType = 1, ;
        RowSource = "202301,202302,202303,202304,202305,202306,202307,202308,202309,202310,202311,202312", ;
        Height = 25, ;
        Left = 96, ;
        Top = 60, ;
        Width = 97, ;
        Name = "Combo1"


    ADD OBJECT combo2 AS combobox WITH ;
        RowSourceType = 1, ;
        RowSource = "202301,202302,202303,202304,202305,202306,202307,202308,202309,202310,202311,202312", ;
        Height = 25, ;
        Left = 276, ;
        Top = 60, ;
        Width = 97, ;
        Name = "Combo2"


    ADD OBJECT grid1 AS grid WITH ;
        ColumnCount = 3, ;
        Height = 144, ;
        Left = 72, ;
        RecordSource = "表1", ;
        RecordSourceType = 1, ;
        Top = 144, ;
        Width = 336, ;
        Name = "Grid1", ;
        Column1.ControlSource = "表1.num", ;
        Column1.Width = 62, ;
        Column1.Name = "Column1", ;
        Column2.ControlSource = "表1.ny", ;
        Column2.Width = 62, ;
        Column2.Name = "Column2", ;
        Column3.ControlSource = "表1.hj", ;
        Column3.Width = 62, ;
        Column3.Name = "Column3"


    ADD OBJECT form1.grid1.column1.header1 AS header WITH ;
        Caption = "Num", ;
        Name = "Header1"


    ADD OBJECT form1.grid1.column1.text1 AS textbox WITH ;
        BorderStyle = 0, ;
        Margin = 0, ;
        ForeColor = RGB(0,0,0), ;
        BackColor = RGB(255,255,255), ;
        Name = "Text1"


    ADD OBJECT form1.grid1.column2.header1 AS header WITH ;
        Caption = "Ny", ;
        Name = "Header1"


    ADD OBJECT form1.grid1.column2.text1 AS textbox WITH ;
        BorderStyle = 0, ;
        Margin = 0, ;
        ForeColor = RGB(0,0,0), ;
        BackColor = RGB(255,255,255), ;
        Name = "Text1"


    ADD OBJECT form1.grid1.column3.header1 AS header WITH ;
        Caption = "Hj", ;
        Name = "Header1"


    ADD OBJECT form1.grid1.column3.text1 AS textbox WITH ;
        BorderStyle = 0, ;
        Margin = 0, ;
        ForeColor = RGB(0,0,0), ;
        BackColor = RGB(255,255,255), ;
        Name = "Text1"


    ADD OBJECT label2 AS label WITH ;
        Caption = "起始时间", ;
        Height = 13, ;
        Left = 24, ;
        Top = 65, ;
        Width = 61, ;
        Name = "Label2"


    ADD OBJECT label3 AS label WITH ;
        Caption = "终止时间", ;
        Height = 13, ;
        Left = 204, ;
        Top = 65, ;
        Width = 61, ;
        Name = "Label3"


    ADD OBJECT command1 AS commandbutton WITH ;
        Top = 36, ;
        Left = 432, ;
        Height = 25, ;
        Width = 61, ;
        Caption = "关闭", ;
        Name = "Command1"


    ADD OBJECT label4 AS label WITH ;
        Caption = "Label4", ;
        Height = 25, ;
        Left = 240, ;
        Top = 12, ;
        Width = 157, ;
        Name = "Label4"


    PROCEDURE Activate
        PUBLIC nText,ST,ET
        *!* public 声明的变量要释放,不然表单关闭了,它们依然存在。
         nText=0
         nText= thisform.TEXT1.value &&此值本来就是0,跟上一句是重复的
         st = &&此时值为空
         et = &&此时值为空
         *!* 注意到你对3个变量赋值
    ENDPROC


    PROCEDURE pageframe1.Click
        *!* 不明白为什么又要再赋值一次,如果要赋值,那么上面activate事件就是多余的。
        nText = thisform.text1.value
         st =
         et =
    ENDPROC


    PROCEDURE pageframe1.Page1.Click
        *this.parent.click

        thisform.pageframe1.page1.caption=STR(NTEXT)
         
         ThisForm.Refresh
    ENDPROC


    PROCEDURE pageframe1.Page2.Click
        *this.parent.click
        thisform.pageframe1.page2.caption=st
    ENDPROC


    PROCEDURE pageframe1.Page3.Click
        *this.parent.click
        thisform.pageframe1.page3.caption=ET
    ENDPROC


    PROCEDURE pageframe1.Page4.Click
        *this.parent.click
        thisform.pageframe1.page4.caption=ET+ST
    ENDPROC


    PROCEDURE text1.LostFocus
        thisform.label4.caption=str(ntext)
         ThisForm.Refresh
    ENDPROC


    PROCEDURE command1.Click
        *!*退出前释放public申明过的变量
        quit
    ENDPROC


ENDDEFINE
*
*-- EndDefine: form1
**************************************************

猜想你是想用这3个变量作为每个page的caption,
那么你可以直接这样写:
pageframe1.Click里代码:
this.page1.caption = transf(thisform.text1.value) &&修改一下
this.page2.caption =
this.page3.caption =
this.page4.caption =
每个page的click代码只有一句:
this.parent.click()
activate 事件的代码可以不用了。

label4的赋值没有成功的原因是,你在textbox的lostfocus事件里赋值的,它没有得到焦点自然也就不会触发lostfocus。

这样解决你的困惑没有呢??

[此贴子已经被作者于2023-8-30 13:19编辑过]

#26
tjdeming2023-08-30 20:27
回复 25楼 sam_jiang
程序代码:
**************************************************
*-- Form:         form1 (d:\documents\visual foxpro 项目\表单变量\表单1.scx)
*-- ParentClass:  form
*-- BaseClass:    form
*-- Time Stamp:   08/29/23 08:38:00 PM
*

PUBLIC oform1

oform1=NEWOBJECT("form1")
oform1.Show
RETURN


DEFINE CLASS form1 AS form


    Top = 88
    Left = 85
    Height = 329
    Width = 509
    DoCreate = .T.
    Caption = "Form1"
    Name = "Form1"


    ADD OBJECT pageframe1 AS pageframe WITH ;
        ErasePage = .T., ;
        PageCount = 4, ;
        Top = 108, ;
        Left = 36, ;
        Width = 444, ;
        Height = 204, ;
        Name = "Pageframe1", ;
        Page1.Caption = "Page1", ;
        Page1.Name = "Page1", ;
        Page2.Caption = "Page2", ;
        Page2.Name = "Page2", ;
        Page3.Caption = "Page3", ;
        Page3.Name = "Page3", ;
        Page4.Caption = "Page4", ;
        Page4.Name = "Page4"


    ADD OBJECT text1 AS textbox WITH ;
        Alignment = 3, ;
        Value = 0, ;
        Height = 25, ;
        InputMask = "", ;
        Left = 97, ;
        Top = 12, ;
        Width = 96, ;
        Name = "Text1"


    ADD OBJECT label1 AS label WITH ;
        AutoSize = .T., ;
        Caption = "输入编号", ;
        Height = 16, ;
        Left = 24, ;
        Top = 20, ;
        Width = 50, ;
        Name = "Label1"


    ADD OBJECT combo1 AS combobox WITH ;
        RowSourceType = 1, ;
        RowSource = "202301,202302,202303,202304,202305,202306,202307,202308,202309,202310,202311,202312", ;
        Height = 25, ;
        Left = 96, ;
        Top = 60, ;
        Width = 97, ;
        Name = "Combo1"


    ADD OBJECT combo2 AS combobox WITH ;
        RowSourceType = 1, ;
        RowSource = "202301,202302,202303,202304,202305,202306,202307,202308,202309,202310,202311,202312", ;
        Height = 25, ;
        Left = 276, ;
        Top = 60, ;
        Width = 97, ;
        Name = "Combo2"


    ADD OBJECT label2 AS label WITH ;
        Caption = "起始时间", ;
        Height = 13, ;
        Left = 24, ;
        Top = 65, ;
        Width = 61, ;
        Name = "Label2"


    ADD OBJECT label3 AS label WITH ;
        Caption = "终止时间", ;
        Height = 13, ;
        Left = 204, ;
        Top = 65, ;
        Width = 61, ;
        Name = "Label3"


    ADD OBJECT command1 AS commandbutton WITH ;
        Top = 36, ;
        Left = 432, ;
        Height = 25, ;
        Width = 61, ;
        Caption = "关闭", ;
        Name = "Command1"


    ADD OBJECT label4 AS label WITH ;
        Caption = "Label4", ;
        Height = 25, ;
        Left = 240, ;
        Top = 12, ;
        Width = 157, ;
        Name = "Label4"


*!*        PROCEDURE Activate                                            
*!*            PUBLIC nText,ST,ET
*!*            *!* public 声明的变量要释放,不然表单关闭了,它们依然存在。
*!*            * nText=0
*!*             nText= thisform.TEXT1.value &&此值本来就是0,跟上一句是重复的
*!*            st = *!*            et = *!*             ** 注意到你对3个变量赋值
*!*        ENDPROC


    PROCEDURE pageframe1.Click
        ** 不明白为什么又要再赋值一次,如果要赋值,那么上面activate事件就是多余的。
       * nText = thisform.text1.value
       *  st =
       *  et =
        PUBLIC nText,ST,ET
        *!* public 声明的变量要释放,不然表单关闭了,它们依然存在。
        * nText=0
         nText= thisform.TEXT1.value &&此值本来就是0,跟上一句是重复的
        st = = ** 注意到你对3个变量赋值
    ENDPROC


    PROCEDURE pageframe1.Page1.Click
      * this.parent.click()

        thisform.pageframe1.page1.caption=STR(NTEXT)
        
         ThisForm.Refresh
    ENDPROC


    PROCEDURE pageframe1.Page2.Click
       * this.parent.click()
        thisform.pageframe1.page2.caption=st
         ThisForm.Refresh
    ENDPROC


    PROCEDURE pageframe1.Page3.Click
      *  this.parent.click()
        thisform.pageframe1.page3.caption=ET
         ThisForm.Refresh
    ENDPROC


    PROCEDURE pageframe1.Page4.Click
      * this.parent.click
        thisform.pageframe1.page4.caption=ET+ST
         ThisForm.Refresh
    ENDPROC


*!*        PROCEDURE text1.LostFocus
*!*            thisform.label4.caption=str(ntext)
*!*            ThisForm.Refresh
*!*        ENDPROC


    PROCEDURE command1.Click
        **退出前释放public申明过的变量
        CLEAR MEMORY   
        clear
        quit
    ENDPROC


ENDDEFINE
*
*-- EndDefine: form1
**************************************************

非常感谢!!!
晚上弄了很长时间,总算能运行了。主要是想学习下变量的引用,简化语句。

*!*退出前释放public申明过的变量
        CLEAR MEMORY 这样写对不对?   
#27
sam_jiang2023-08-30 20:44
clear memory 可以的。

release all也可以。
#28
tjdeming2023-08-30 20:49
在表单中增加了一个命令按钮后:
 PROCEDURE command2.Click
        if ntext<>0   &&&提示找不到(ntext)变量
        (str(ntext))  
       endif
     
  ENDPROC
问题在哪儿?
程序代码:
PROCEDURE command2.Click
if ntext<>0   &&&提示找不到(ntext)变量
     =allt(str(ntext))
  endif
ENDPROC



[此贴子已经被作者于2023-8-30 20:56编辑过]

#29
tjdeming2023-08-30 20:50
回复 27楼 sam_jiang
好!谢谢你。
只有本站会员才能查看附件,请 登录


[此贴子已经被作者于2023-8-30 21:00编辑过]

#30
sam_jiang2023-08-31 15:36
判断变量存在与否有专门的函数的。

if type([ntext])="U" && vartype(ntext)="U"
    return .f.
else
    *!* your code here
endif
#31
tjdeming2023-08-31 17:10
回复 30楼 sam_jiang
这个nText变量没有引用过来
1