Reputation: 85996
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.
Upvotes: 3
Views: 8379
Reputation: 11
I know this is super late but-
Form1.Dispose()
works for me. It resets the textboxes.
Upvotes: 1
Reputation: 12194
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()
Upvotes: 1
Reputation: 1893
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.
Upvotes: 2
Reputation: 6999
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);
}
}
Upvotes: 4
Reputation: 17428
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.
Upvotes: 1
Reputation: 8062
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
If you are binding controls by any DATASOURCE I would suggest to clear the Datasource and REBIND
Upvotes: 0