在表单之间翻转时如何保留表单的控件值 [英] How do I retain a form's control values when flipping between forms

查看:21
本文介绍了在表单之间翻转时如何保留表单的控件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要两种形式才能输入特定交易的所有信息.我希望能够在这两个表单之间来回切换,保留在每个表单上输入的内容,直到单击保存"按钮.

我认为我应该能够使用 Form2.Show、Me.Hide 和 Form1.Show、Me.Hide.我第一次进入 Form2 时,它会通过 Form2 加载事件(这是合理的),但是关于 Form1 上的控件内容的任何知识都丢失了.即使 Form1 是隐藏的(并且没有关闭),其控件的内容也不见了.为什么?

第二次进入 Form2 时,加载事件不会触发,因为 Form2 处于隐藏状态,此时所有 Form1 控件内容都可用.因此,在 Form1 和 Form2 之间来回翻转时,它会在我第二次进入 Form2 后按照我想要的方式工作.但是,我第一次和每次都需要它来工作.

这几天我一直在试图理解这一点.我已经注释掉了几乎每一行代码,通过代码,谷歌搜索直到我脸色发青(已经有很多关于这个的文章),但我仍然无法弄清楚为什么会发生这种行为.

谁能解释这种现象?或者最好告诉我我需要做些什么来完成这项工作.

我在进入 Form2 的 Form1 按钮后面有这个代码

如果 Form2 什么都没有,那么将 Form2 调暗为新 Form2万一Form2.Show()我.隐藏()

而这个代码后面的Form2按钮返回Form1

Form1.Show我.隐藏

解决方案

这就是您可能缺少的全部内容:

Class Form1Private f2 As Form2 ' 这是 Form1 对' form2 实例

稍后当您单击转到 form2 时,您的原始代码只需要稍作调整:

如果 f2 什么都没有,那么f2 = New Form2(Me) ' 将声明的变量设置为新实例万一F2.Show()我.隐藏()

在这种情况下,Form1 使用 您之前展示的技巧 使用构造函数:

Sub New(frm As Form1) ' 这仅在 Form2 中f1 = frm结束子

您在 Form1 中不需要这个,因为他/它正在创建自己的 f2 对象引用.

原始代码中的主要问题是:将 Form2 变暗为新 Form2.您每次都在创建一个新的 Form2(我怀疑它驻留在事件或子中).那些新的 instances 无法知道之前实例中的控件值.如图所示声明 F1F2 赋予它模块/表单级别的 Scope.

Dim 声明一个变量及其类型.f1 是 Form1 类型.如果是对象变量则不创建对象

New 创建一个对象类型(引用类型)的实例.这与类中的 Sub New 方法直接相关.当你使用 New 时,Sub New 被调用,所以任何需要的特殊东西都可以在那里发生.Integer 之类的值类型不需要创建或实例化,只需声明即可.

在哪里声明(Dim)一个变量决定了它的Scope.如果在 Sub 中执行此操作,则变量或对象仅存在于该子中.如果您在表单/类级别执行此操作,则它具有表单/类级别范围.

It takes two forms to be able to enter all the info on a particular transaction. I want to be able to flip back and forth between these two forms retaining what was entered on each until the 'Save' button is clicked.

I think I should be able to use Form2.Show, Me.Hide and then Form1.Show, Me.Hide. The first time I go to Form2 it goes thru the Form2 load event (that’s reasonable) but any knowledge of the control contents on Form1 have been lost. Even though Form1 is hidden (and not closed) the contents of its controls are gone. Why?

The second time I go to Form2 the load event does not fire because Form2 is hidden and at this point all the Form1 control contents are available. So, in flipping back and forth between Form1 and Form2 it works as I want it to after I go to Form2 the second time. But, I need it to work the first time and every time.

For days I’ve been trying to understand this. I’ve commented out nearly every line of code, stepped thru code, googled till I’m blue in the face (there has been a lot written about this), and I still can’t figure out why this behavior occurs.

Can anyone explain this phenomenon? Or better yet tell me what I need to do to make this work.

I have this code behind the Form1 button that goes to Form2

If Form2 Is Nothing Then
    Dim Form2 As New Form2
End If
Form2.Show()
Me.Hide()

And this code behind the Form2 button to return to Form1

Form1.Show
Me.Hide

解决方案

This is all you are probably missing:

Class Form1
     Private f2 As Form2       ' this is Form1's reference to the
                               ' form2 instance

Later when you click to go to form2, your original code just needs a small tweak:

If f2 Is Nothing Then
    f2 = New Form2(Me)             ' set declared variable to new instance   
End If
F2.Show()
Me.Hide()

In this case Form1 is passing the reference using the trick you were shown before using the constructor:

Sub New(frm As Form1)         ' this is in Form2 only
   f1 = frm               
End Sub

You dont need this in Form1 because he/it is creating his own f2 object reference.

The main problem in your original code, was: Dim Form2 As New Form2. You are creating a new Form2 each time (I suspect that resides in an event or sub). Those new instances can't know the control values in the previous instances. Declaring F1 or F2 as shown gives it module/form level Scope.

Dim declares a variable and its Type. f1 is of Type Form1. It does not create an object if it is an object variable

New creates an instance of an object Type (reference types). This directly relates to the Sub New method in the class. When you use New, Sub New is called so anything special that is needed can take place there. Value types like Integer do not need to be created or instanced, only declared.

Where you declare (Dim) a variable determines its Scope. If you do this in a Sub, the variable or object only exists in that sub. if you do it at the form/class level, it has Form/Class level scope.

这篇关于在表单之间翻转时如何保留表单的控件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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