关闭后表单保留值
在我们的程序中,表单是这样打开的:
FormName.SomeValue = 10
FormName.ShowDialog()
而不是通常的
Dim myForm As New FormName
myForm.SomeValue = 10
myForm.ShowDialog()
(对此我们无能为力 - 这是由 Visual Studio VB6 --> VB.Net 转换器自动完成的)
问题是,当表单关闭时,它们似乎并没有真正关闭,只是隐藏 - 如果我向文本框添加一些文本并关闭/重新打开表单,文本仍然存在,而不是像平常一样清除文本框。这可能是因为表单总是使用相同的实例。
除了遍历整个程序并为每个 ShowDialog()
调用创建一个新的表单实例之外,是否有任何简单的方法来解决此问题(有数百个)< /em>?
我们考虑过重置每个表单的 Load
事件中的每个控件,但这仍然很痛苦,因此我们想首先询问是否有更简单的方法。
Throughout our program, forms are opened like this:
FormName.SomeValue = 10
FormName.ShowDialog()
rather than the usual
Dim myForm As New FormName
myForm.SomeValue = 10
myForm.ShowDialog()
(There is nothing we could do about this - this was done automatically by the Visual Studio VB6 --> VB.Net converter)
The problem is that when forms are closed, they seem to not really be closed, only hidden - if I add some text to a textbox and close/reopen the form, the text is still there, rather than the textbox being cleared like normal. This is presumably because the form always uses the same instance.
Is there any easy way to fix this other than going through the entire program and creating a new form instance for every ShowDialog()
call (there are hundreds)?
We considered resetting every control in every form's Load
event, but that would still be a pain, so we figured we'd ask if there's a simpler way first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您正在处理的内容称为表单的“默认实例”,是 VB6 时代遗留下来的。不建议使用它。您可能不想听到它,但对于您的代码库来说,最好的长期策略是以正确的方式重写表单初始值设定项,而不是在表单 Load() 事件中执行一些古怪的解决方法。您现在可能讨厌它,但下次您必须处理此代码时您会欣赏它。您甚至可以组合一个片段来为您完成大部分打字工作。
What you are dealing with is called the form's "default instance" and is a carry over from the VB6 days. It is not recommended practice to use it. You may not want to hear it, but the best long-term strategy for your code base is to rewrite the form initializers the correct way than to do some hacky workaround in the form Load() events. You may hate it now, but you will appreciate it the next time you have to work on this code. You can probably even put together a snippet to do most of the typing for you.
编辑:
如何使用Using 语句显示表单
从此处显示的代码看来,现在有一个static ShowDialog
调用已添加到您的FormName
类中。您应该能够仅编辑此方法来处理
旧表单并创建和显示新表单。这将帮助您避免在一个位置处到处更改代码。Edit:
How to display the form using the Using statement
It appears from the code displayed here that there is now astatic ShowDialog
call that was added to yourFormName
class. You should be able to edit just this method todispose
of the old form and create and display the new one. This would help you avoid changing code all over the place, just in the one location.您要求简单的方法来解决此问题:
按以下方式更改您的
ShowDialog()
过程/函数调用:You asked for easy way to fix this:
Change your
ShowDialog()
procedure/function calls in the following way:我知道这已经太晚了,但
对我有用。它重置文本框。
I know this is super late but-
works for me. It resets the textboxes.
如果问题是关于清除文本框,那么我会递归地清除所有文本框
如果您通过任何数据源绑定控件,我建议清除数据源并重新绑定
If the question is about clearing text boxes then I would have Cleared all of them RECURSIVELY
If you are binding controls by any DATASOURCE I would suggest to clear the Datasource and REBIND