在两个窗口窗体之间传递值
我有两个窗口表单,即form1和form2。在form1中,用户必须输入一些值。此页面中有下一个按钮。通过单击下一个按钮,form2被打开,我隐藏表单1。在Form2中,也有一些输入字段.这里我使用构造函数方法访问form1的一些值
在任何情况下,如果form 1中输入的值是错误的,用户单击form2中的后退按钮并转到form1,修改值并单击下一步转到form2。
问题是,当我第二次修改 form1 中的值并单击“下一步”转到 form2 时,我得到了 form1 的旧值。
请建议。
I have two windows form say form1 and form2.In form1 user have to input some values.There is next button in this page .By clicking on next button form2 get opened and i am hide form 1.In Form2 also there are some input fields.here i am accessing some values of form1 by using constructor method
In any situation if the values enetered in form 1 is wrong user click the back button in form2 and go to form1,Modify the values and click next to come to form2.
The problem is that when second time i modify the values in form1 and click next to go to form2,there i am getting old values of form1.
Please suggest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
表格 1:
表格 2:
当存在常规值时,这将起作用。
FORM 1:
FORM 2:
This will work when there is regular values.
只要清除form2的值,然后在修改form1时重新加载即可,就可以得到新的值。
Just clear the values of form2 and reload when they modify form1.Then you may get the new values.
您可能想要创建一个类来保存表单之间共享的值。当单击 form1 上的下一个按钮时,应验证输入 - 如果发现错误,不要让用户前进到下一个屏幕。如果没有发现错误,则将值保存到类中并通过构造函数将其传递给 form2。当 form2 加载时,让它从值填充它的控件。
you probably want to create a class that holds the values that are shared between the forms. when the next button on form1 is clicked, the input should be validated - if errors are found, don't let the user advance to the next screen. if no errors are found, save the values to a class and pass it to form2, via the constructor. when form2 loads, have it populate it's controls from the values.
在 Windows 窗体中传递值的最简单方法是仅在一种窗体中创建会话变量并在第二种窗体中使用它们。
The Easiest way of passing values in windows forms is to just create Session variables in one form and use them in second form.
这是在窗口窗体之间传递值的简单解决方案。以下示例从 Form1 文本框获取值,并将该值作为字符串值发送到 Form2 标签(使用 Visual Studio 2012 Windows 窗体应用程序编写的示例)。如果删除注释的话,这里实际上就没有多少代码了。
致谢:以上内容部分源自以下内容:https://www.youtube.com /watch?v=PI3ad-TebP0
Here is a simple solution to pass values between windows forms. The following example takes a value from a Form1 textBox and sends the value to a Form2 label as a string value (Example written using Visual Studio 2012 Windows Form Application). There really isn't much code here if you remove the comments.
Credits: The above was derived in part from the following, https://www.youtube.com/watch?v=PI3ad-TebP0
在两个表单之间传递数据可以通过不同的方式完成,但最简单的方法可能是使用一个对象来保存两个表单上的数据和公共属性,并使用该对象来设置数据。例如:
然后在实例化表单的“程序”中,您有类似的内容
那样显示 Form2
在 Form1 的按钮单击中,您应该只需要验证输入的数据并像Sidenote
听起来很像您正在尝试构建某种向导功能。我建议您在谷歌上搜索一下,因为可能已经存在一些可以帮助您的预定义控件:http://www.google.com/search?hl=en&sourceid=chrome&ie=UTF-8&q=wizard+winforms
Passing data between two forms can be done in different ways but probably the simplest one is to have an Object that holds the data and public properties on both forms that use that object for setting the data. For example:
Then in your "Program" where you instantiate the form, you have something like
In the button click of Form1 you should just have to validate the entered data and to show Form2 like
Sidenote
It very much sounds like you're trying to build some kind of wizard functionality. I'd suggest you to google a bit as there might exist already some predefined controls that help you: http://www.google.com/search?hl=en&sourceid=chrome&ie=UTF-8&q=wizard+winforms