保留在胜利表格上输入的数据 [英] retaining data entered on win forms

查看:98
本文介绍了保留在胜利表格上输入的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能够输入特定交易的所有信息需要两种形式。我希望能够在这两个表单之间来回切换,保留每个表单上输入的内容,直到单击保存按钮。我想我应该可以使用Form2.Show,Me.Hide然后Form1.Show,Me.Hide。



我第一次尝试回到从Form2上的后退按钮的Form1,先前在Form1上输入的信息已消失。为什么?但是,如果我再次填写,请转到Form2,然后再次单击返回按钮保留信息。



显然正在执行的代码当我单击Form2上的后退按钮时,第二次不同于我第一次单击该按钮时执行的代码。怎么会这样?



几天来我一直试图理解这一点。我已经注释掉几乎每一行代码,通过代码进行搜索,直到我脸上都是蓝色的(已经写了很多关于此的内容),我仍然无法弄清楚为什么会出现这种情况。 />


有谁能解释这种现象?我可以通过在全局变量和重新填充控件中存储数据来设计一些解决方法,但这是很多不必要的工作。

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 try to go back to Form1 from a ‘Back’ button on Form2 the information previously entered on Form1 has disappeared. Why? However, if I fill it in again, go to Form2, and then click the ‘Back’ button again the info is retained.

Obviously the code that is being executed when I click the ‘Back’ button on Form2 the second time is different than the code that is executed the first time I click that same button. How can this be?

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? I can devise some workaround by storing data in globals and repopulating controls but this is a lot of work that should be unnecessary.

推荐答案

不要这样做 - 这意味着两种形式必须互相了解。



我这样做:



在Form2中,创建一个名为HideMe的事件。

Form1中有一个名为form2的类级变量。从这个null开始。

当你按下按钮时,检查form2:如果它为null,则创建一个新的Form2实例并将其分配给form2变量。为Form2实例的FormClosing和HideMe事件添加处理程序。

无论是否,请在form1上调用Hide并在form2上调用。



当form2 FormClosing事件被触发时,关闭你的form1

当触发HideMe事件时,调用form2.Hide和form1 Show。
Don't do it like that - it means that the two forms have to know about each other.

I'd do it like this:

In Form2, create an event which says "HideMe".
In Form1 have a class level variable called form2. Start with this null.
When you press the button, check form2: if it is null then create an new instance of Form2 and assign it to the form2 variable. Add a handler for the "FormClosing" and "HideMe" events of the Form2 instance.
Whether it was or not, call Hide on form1 and Show on form2.

When the form2 FormClosing event gets fired, close your form1
When the "HideMe" event is fired, call form2.Hide, and form1 Show.


这篇关于保留在胜利表格上输入的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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