开发者

Forms retaining values after closing

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 Stu开发者_如何转开发dio 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.


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);
   }
}


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.


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.


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()


I know this is super late but-

Form1.Dispose()

works for me. It resets the textboxes.


  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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜