表单实例化策略:这是一个好主意吗? [英] Strategy for form instantiation: is it a good idea?

查看:104
本文介绍了表单实例化策略:这是一个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


你们有没有想要评论我实施的方法

实例化一个表格?一个描述和一个例子如下。


干杯,

RV

jmclopesAThotmail.com用你知道的东西取代AT; - )


在发现访问2000支持表单属性(我是一个新手,好吧:-))之后,我很满意用一些面向对象的方法组织我的

代码。类似的东西:


- 实例化表格

- 设置表格属性

- 显示表格

- 阅读表格属性

- 做任何需要


然后我意识到(至少我没有找到怎样)我无法打开

形式,程序执行将停止,直到表格

关闭。使用以下代码,显示表单,但

程序继续执行,当它结束时,表单被

销毁:


Sub my_proc()

Dim f As New Form_my_form

f.Message =" Hello form!" ''留言'是我添加的财产

表格


''用户可以在文本框中输入一些文字,

''表单返回的属性消息

f.visible = true


MsgBox f.Message

设置f = Nothing

End Sub


所以我解决了这个问题如下。这个想法是在表单显示后显示,当窗体可见时,过程循环。当用户按下该表单中的OK时,表单是不可见的。

''-------- my_proc()的新版本 - ---------

Sub my_proc()

Dim f As New Form_my_form

f.Message =" Hello form !" ''留言'是我添加的财产

表格


''用户可以在文本框中输入一些文字,

''表单返回的属性消息

f.Modal = True''可以将表单设置为模态以具有弹出窗口

行为

f.visible = true

虽然f.Visible

DoEvents

Wend


MsgBox f.Message

设置f =没什么

结束子


''--- ----- MY_FORM程序-----------

Private Sub btnOk_Click()

Me.Visible = False

End Sub


Private Sub Form_Unload(取消为整数)

''不要卸载表单,只需隐藏它以便属性

''仍可供来电者使用

如果Me.Visible那么

''只有在表格没有被隐藏时取消卸载

Me.Visible = False

取消= 1

结束如果

En d Sub

解决方案

我见过几个从代码中实例化表单很有用的情况,

和那些只有当同时显示同一表格的多个副本时

。简单的方法通常是最好的...而简单的方法是设计你的表单并在设计视图中设置其属性,然后使用

DoCmd.OpenForm打开通常只需要一份副本。

访问不是代码密集型的典型编程语言,而且它是非常强大的点之一。


如果有这个帖子的后续内容,我希望有人可以接听,因为我好几天就要离开新闻组了。


Larry Linson

Microsoft Access MVP

" Raposa Velha" < JM ****** @ hotmail.com>在消息中写道

news:69 ************************** @ posting.google.c om ...

大家好!

您是否想要评论我实施的方法来实例化表单?一个描述和一个例子如下。

欢呼,
RV
jmclopesAThotmail.com用你知道的东西替换AT ;-)

发现后访问2000支持表单属性(我是新手,好吧:-)),我很高兴用一些面向对象的方法组织我的代码。类似的东西:

- 实例化表格
- 设置表格属性
- 显示表格
- 阅读表格属性
- 做任何需要
然后我意识到(至少我没有找到怎样)我无法打开
表单,程序执行将停止,直到表格
关闭。使用以下代码,显示表单,但
过程继续执行,当它结束时,表单被销毁:

Sub my_proc()
Dim f As New Form_my_form
f.Message =" Hello form!" ''消息它'是我添加到
表单的属性

''用户将在文本框中输入一些文本,
''表单返回的属性消息
f.visible = true

MsgBox f.Message
设置f = Nothing
End Sub

所以我解决了这个问题如下。这个想法是在显示表单后,程序在表单可见时循环。当用户按下该表单中的OK时,表单不可见。

''-------- my_proc()的新版本------ -----
Sub my_proc()
Dim f As New Form_my_form
f.Message =" Hello form!" ''消息它'是我添加到
表单的属性

''用户将在文本框中输入一些文本,
''表单返回的属性消息
f.Modal = True''可以将表单设置为模态以具有弹出窗口
行为
f.visible = true
而f.Visible
DoEvents
Wend

MsgBox f.Message
设置f =无任何结束子


''----- --- MY_FORM PROCEDURES -----------
Private Sub btnOk_Click()
Me.Visible = False
End Sub

私人子Form_Unload(取消为整数)
''不要卸载表单,只需将其隐藏,以便调用者仍可使用属性
如果Me.Visible那么''如果表格没有隐藏,只取消卸载
Me.Visible = False
取消= 1
结束如果
End Sub


Raposa Velha写道:

大家好!

您是否想要评论我实施的方法来实例化表单?说明和示例如下。




是。

有什么意义?


您已经发现了使用New关键字实例化表单的技术的许多限制之一。一般来说,你可以找到另一种方法来做同样的事情,并避免这个问题。


在极少数情况下技术确实增加了足够的价值为

弥补增加的复杂性和不稳定性(例如,如果代码重置,表单将关闭

),至少确保你在
事件驱动的方式,这样你就不需要停止代码并等待

表单关闭。相反,使用表单上的事件来触发后续

操作。


请注意,您可以通过使用表单引用
本身,所以当打开它的程序退出时它不会关闭(

替代方案是某种凌乱的开放实例集合)。只需确保

表单在Form_Close上将其自引用设置为Nothing,所以所有资源都会被释放。


2004年12月28日10:52:56 -0800, jm******@hotmail.com (Raposa Velha )写道:

大家好!

您是否有人想评论我实施的方法来实例化表格?一个描述和一个例子如下。

欢呼,
RV
jmclopesAThotmail.com用你知道的东西替换AT ;-)

发现后访问2000支持表单属性(我是新手,好吧:-)),我很高兴用一些面向对象的方法组织我的代码。类似的东西:

- 实例化表格
- 设置表格属性
- 显示表格
- 阅读表格属性
- 做任何需要
然后我意识到(至少我没有找到怎样)我无法打开
表单,程序执行将停止,直到表格
关闭。使用以下代码,显示表单,但
过程继续执行,当它结束时,表单被销毁:

Sub my_proc()
Dim f As New Form_my_form
f.Message =" Hello form!" ''消息它'是我添加到
表单的属性

''用户将在文本框中输入一些文本,
''表单返回的属性消息
f.visible = true

MsgBox f.Message
设置f = Nothing
End Sub

所以我解决了这个问题如下。这个想法是在显示表单后,程序在表单可见时循环。当用户按下该表单中的OK时,表单不可见。

''-------- my_proc()的新版本------ -----
Sub my_proc()
Dim f As New Form_my_form
f.Message =" Hello form!" ''消息它'是我添加到
表单的属性

''用户将在文本框中输入一些文本,
''表单返回的属性消息
f.Modal = True''可以将表单设置为模态以具有弹出窗口
行为
f.visible = true
而f.Visible
DoEvents
Wend

MsgBox f.Message
设置f =无任何结束子


''----- --- MY_FORM PROCEDURES -----------
Private Sub btnOk_Click()
Me.Visible = False
End Sub

私人子Form_Unload(取消为整数)
''不要卸载表单,只需将其隐藏,以便调用者仍可使用属性
如果Me.Visible那么''如果表格没有隐藏,只取消卸载
Me.Visible = False
取消= 1
结束如果
End Sub



Hello to all!

Does any of you want to comment the approach I implement for
instantiating a form? A description and an example follow.

Cheers,
RV
jmclopesAThotmail.com replace the AT with the thing you know ;-)

After discovering that access 2000 support form properties (I''m a
newbie, alright :-) ), I was pleased with the idea of organizing my
code with some object oriented approach. Something like:

- instantiate a form
- set form properties
- display the form
- read form properties
- do whatever required

Then I realized (at least I didn''t find how) that I couldn''t open the
form in a way that the procedure execution would stop until the form
is closed. With the following code, the form is displayed but the
procedure continues its execution and, when it ends, the form is
destroyed:

Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" '' Message it''s a property I added to
the form

''The user would enter some text in a textbox,
''which the form returns with the property Message
f.visible=true

MsgBox f.Message
Set f = Nothing
End Sub

So I solved this problem as follows. The idea is after the form is
displayed, the procedure loops while the form is visible. The form is
made not visible when the user presses the OK in that form.
''-------- NEW VERSION of my_proc() -----------
Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" '' Message it''s a property I added to
the form

''The user would enter some text in a textbox,
''which the form returns with the property Message
f.Modal = True '' one can set the form to modal to have a popup
behaviour
f.visible=true
While f.Visible
DoEvents
Wend

MsgBox f.Message
Set f = Nothing
End Sub


''-------- MY_FORM PROCEDURES -----------
Private Sub btnOk_Click()
Me.Visible = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
''Don''t unload the form, just hide it so the properties
''are still available to the caller
If Me.Visible Then
''only cancel the unload if the form isn''t hidden
Me.Visible = False
Cancel = 1
End If
End Sub

解决方案

I have seen few cases where it was useful to instantiate a form from code,
and those only when multiple copies of the same form were to be displayed
concurrently. The simple way is often best... and the simple way is to
design your form and set its properties in design view, then use
DoCmd.OpenForm to open the single copy that is usually all that is needed.
Access is not your typical programming language that is code intensive, and
that is one of its very strong points.

If there''s followup to this thread, I hope someone can pick it up, because I
am going to be out of the newsgroups for several days.

Larry Linson
Microsoft Access MVP
"Raposa Velha" <jm******@hotmail.com> wrote in message
news:69**************************@posting.google.c om...

Hello to all!

Does any of you want to comment the approach I implement for
instantiating a form? A description and an example follow.

Cheers,
RV
jmclopesAThotmail.com replace the AT with the thing you know ;-)

After discovering that access 2000 support form properties (I''m a
newbie, alright :-) ), I was pleased with the idea of organizing my
code with some object oriented approach. Something like:

- instantiate a form
- set form properties
- display the form
- read form properties
- do whatever required

Then I realized (at least I didn''t find how) that I couldn''t open the
form in a way that the procedure execution would stop until the form
is closed. With the following code, the form is displayed but the
procedure continues its execution and, when it ends, the form is
destroyed:

Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" '' Message it''s a property I added to
the form

''The user would enter some text in a textbox,
''which the form returns with the property Message
f.visible=true

MsgBox f.Message
Set f = Nothing
End Sub

So I solved this problem as follows. The idea is after the form is
displayed, the procedure loops while the form is visible. The form is
made not visible when the user presses the OK in that form.
''-------- NEW VERSION of my_proc() -----------
Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" '' Message it''s a property I added to
the form

''The user would enter some text in a textbox,
''which the form returns with the property Message
f.Modal = True '' one can set the form to modal to have a popup
behaviour
f.visible=true
While f.Visible
DoEvents
Wend

MsgBox f.Message
Set f = Nothing
End Sub


''-------- MY_FORM PROCEDURES -----------
Private Sub btnOk_Click()
Me.Visible = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
''Don''t unload the form, just hide it so the properties
''are still available to the caller
If Me.Visible Then
''only cancel the unload if the form isn''t hidden
Me.Visible = False
Cancel = 1
End If
End Sub



Raposa Velha wrote:

Hello to all!

Does any of you want to comment the approach I implement for
instantiating a form? A description and an example follow.



Yes.
What''s the point?


You have discovered one of the many limitations of the technique of
instantiating forms using the New keyword. In general, you can find another
way to do the same thing, and avoid the problem.

In the rare cases when the techniquer really does add enough value to
compensate for the added complexity and instability (e.g. the form will close
if the code gets reset), at least make sure you write your code in an
event-driven manner so that you don''t need your code to halt and wait for the
form to close. Instead, use events on the form to trigger the follow-up
action.

Note that you can bootstrap a form reference by having the form reference
itself, so it doesn''t close when the procedure that opens it exits (the
alternative is some sort of messy open instances collection). Just make sure
the form sets its self-reference to Nothing on Form_Close, so all resources
are freed up.

On 28 Dec 2004 10:52:56 -0800, jm******@hotmail.com (Raposa Velha) wrote:

Hello to all!

Does any of you want to comment the approach I implement for
instantiating a form? A description and an example follow.

Cheers,
RV
jmclopesAThotmail.com replace the AT with the thing you know ;-)

After discovering that access 2000 support form properties (I''m a
newbie, alright :-) ), I was pleased with the idea of organizing my
code with some object oriented approach. Something like:

- instantiate a form
- set form properties
- display the form
- read form properties
- do whatever required

Then I realized (at least I didn''t find how) that I couldn''t open the
form in a way that the procedure execution would stop until the form
is closed. With the following code, the form is displayed but the
procedure continues its execution and, when it ends, the form is
destroyed:

Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" '' Message it''s a property I added to
the form

''The user would enter some text in a textbox,
''which the form returns with the property Message
f.visible=true

MsgBox f.Message
Set f = Nothing
End Sub

So I solved this problem as follows. The idea is after the form is
displayed, the procedure loops while the form is visible. The form is
made not visible when the user presses the OK in that form.
''-------- NEW VERSION of my_proc() -----------
Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" '' Message it''s a property I added to
the form

''The user would enter some text in a textbox,
''which the form returns with the property Message
f.Modal = True '' one can set the form to modal to have a popup
behaviour
f.visible=true
While f.Visible
DoEvents
Wend

MsgBox f.Message
Set f = Nothing
End Sub


''-------- MY_FORM PROCEDURES -----------
Private Sub btnOk_Click()
Me.Visible = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
''Don''t unload the form, just hide it so the properties
''are still available to the caller
If Me.Visible Then
''only cancel the unload if the form isn''t hidden
Me.Visible = False
Cancel = 1
End If
End Sub




这篇关于表单实例化策略:这是一个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆