关闭后表单保留值

发布于 2024-10-10 00:46:25 字数 585 浏览 2 评论 0原文

在我们的程序中,表单是这样打开的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

陌上芳菲 2024-10-17 00:46:25
public class MyForm: Form{

   private static MyForm myForm = null;

   public static DialogResult ShowDialog(bool newForm){
          if(newForm)
          {
                if(myForm != null) 
                    myForm.Dispose();
                myForm= new MyForm();
          }
          return myForm.ShowDialog();
   }

   public static DialogResult ShowDialog(){
          return ShowDialog(true);
   }
}
public class MyForm: Form{

   private static MyForm myForm = null;

   public static DialogResult ShowDialog(bool newForm){
          if(newForm)
          {
                if(myForm != null) 
                    myForm.Dispose();
                myForm= new MyForm();
          }
          return myForm.ShowDialog();
   }

   public static DialogResult ShowDialog(){
          return ShowDialog(true);
   }
}
影子的影子 2024-10-17 00:46:25

您正在处理的内容称为表单的“默认实例”,是 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.

维持三分热 2024-10-17 00:46:25

编辑:
如何使用Using 语句显示表单

Using formName AS New FormName
    formName.SomeValue = 10
    formName.ShowDialog()
End Using

从此处显示的代码看来,现在有一个static ShowDialog 调用已添加到您的FormName 类中。您应该能够仅编辑此方法来处理旧表单并创建和显示新表单。这将帮助您避免在一个位置处到处更改代码。

Edit:
How to display the form using the Using statement

Using formName AS New FormName
    formName.SomeValue = 10
    formName.ShowDialog()
End Using

It appears from the code displayed here that there is now a static ShowDialog call that was added to your FormName class. You should be able to edit just this method to dispose 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.

半城柳色半声笛 2024-10-17 00:46:25

您要求简单的方法来解决此问题:

按以下方式更改您的 ShowDialog() 过程/函数调用:

   AS PROCEDURE                       |   AS FUNCTION
                                      | 
    FormName.ShowDialog()             |    r = FormName.ShowDialog()
    FormName.ShowDialog()             |    r = FormName.ShowDialog()
                                      |
   CHANGE TO                          |   CHANGE TO
                                      |
    Call New FormName.ShowDialog()    |    r = New FormName.ShowDialog()
    Call New FormName.ShowDialog()    |    r = New FormName.ShowDialog()

You asked for easy way to fix this:

Change your ShowDialog() procedure/function calls in the following way:

   AS PROCEDURE                       |   AS FUNCTION
                                      | 
    FormName.ShowDialog()             |    r = FormName.ShowDialog()
    FormName.ShowDialog()             |    r = FormName.ShowDialog()
                                      |
   CHANGE TO                          |   CHANGE TO
                                      |
    Call New FormName.ShowDialog()    |    r = New FormName.ShowDialog()
    Call New FormName.ShowDialog()    |    r = New FormName.ShowDialog()

甜柠檬 2024-10-17 00:46:25

我知道这已经太晚了,但

Form1.Dispose()

对我有用。它重置文本框。

I know this is super late but-

Form1.Dispose()

works for me. It resets the textboxes.

初心 2024-10-17 00:46:25
  1. 如果问题是关于清除文本框,那么我会递归地清除所有文本框

     对于控件中的每个控件
          如果 Control 是 TextBox 类型
          控制.清除
      下一个
    
  2. 如果您通过任何数据源绑定控件,我建议清除数据源并重新绑定

  3. 覆盖 ShowDialog() 方法。
  1. If the question is about clearing text boxes then I would have Cleared all of them RECURSIVELY

      For Each Control in Controls
          If Control is type of TextBox
          Control.Clear
      Next
    
  2. If you are binding controls by any DATASOURCE I would suggest to clear the Datasource and REBIND

  3. Override the ShowDialog() Method.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文