-
Oct 29th, 2011, 06:48 PM
#1
Thread Starter
New Member
Form.Show() wont work after closed
Using VB Express 2010 in windows XP Pro 64.
A ListBox shows the project forms names.
On ListBox_DoubleClick
Code:
For Each F As Form In xFormCollection
If F.Name = ListBox.SelectedItem.ToString Then
Try
F.Show()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
Next
Works fine the first time but after closing the form opened by the DC, if I try to open it again, throws an exception "Cannot access a disposed object".
Since I can still access any other form not previously showed I guess that the closing changes a status that allowed the form to be .Show, there is
no F.Load , F.Activate after the exception doesn't seem to do anything. What should I do?
-
Oct 29th, 2011, 07:01 PM
#2
Re: Form.Show() wont work after closed
Hide the form instead of closing it. BB
-
Oct 29th, 2011, 07:33 PM
#3
Member
Re: Form.Show() wont work after closed
Originally Posted by boops boops
Hide the form instead of closing it. BB
To elaborate on BB's answer...
Yes, you can hide it instead of closing it. However, I would still close it, but instead of using the default instance, I'd create a new instance of the form each time.
The reason you're getting the exception is the exact reason the exception explained. You opened object and then you got rid of it (closing = disposed). Once that object's been disposed, it's gone.
But if you create a new instance each time, then the next time you open that form, it will be a different instance from the one you closed. So something like this should work.
VB.NET Code:
For Each F As Form In xFormCollection
If F.Name = ListBox.SelectedItem.ToString Then
Try
F = New Form
F.Show()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
Next
-
Oct 31st, 2011, 12:53 AM
#4
Thread Starter
New Member
Re: Form.Show() wont work after closed
Thanks boops boops and JhonDorian.
A new instance of a Form wouldnt have the controls and functionality. Couldnt create as New F since F is not a class and if as XF AS New Form = F then the same dispose occurs in F by closing XF. So, for now Im using :
Code:
e.Cancel = True
Me.Hide()
on the closing event but, how come a button click event xf.show() doesnt have the object disposed issue.
-
Oct 31st, 2011, 02:11 AM
#5
Re: Form.Show() wont work after closed
Your xFormCollection should either get updated when a form is closed (thus, removed from the collection) or you should prevent the form from being closed.
Originally Posted by At--o
how come a button click event xf.show() doesnt have the object disposed issue.
Can you elablorate?
-
Oct 31st, 2011, 09:07 AM
#6
Re: Form.Show() wont work after closed
There is definitely some confusion going on here. I agree with the ideas posted here, but they appear not to work, so there is some part of this puzzle that we have not seen, yet. Creating a new instance is what makes most sense to me, unless you are doing things with the current instance which you want to retain. For instance, if the current instance has data displayed in a certain fashion, you would want to hide/show that, since closing will get rid of the instance. Creating a new instance is problematic, though, since you appear to have a collection of forms. Form is the base class that all the forms in the collection have derived from. You can't create a new form and expect it to be useful, as you have noted. Instead, you would need to create a new instance of the proper form type, which you may not even know, unless your collection is all different instances of the same type of form.
As for why this appears not to have the issue in the button click, I would say that it is because you are showing two different instances in the different places, without realizing it. Showing us the code that appears not to have that issue might be sufficient to clear it up....but it might not, too.
My usual boring signature: Nothing
-
Nov 2nd, 2011, 03:16 AM
#7
Thread Starter
New Member
Re: Form.Show() wont work after closed
Code with no issue
Code:
Public Class frmMain
Inherits aaMyForm
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frm2.Show()
End Sub
End Class
-
Nov 2nd, 2011, 09:54 AM
#8
Re: Form.Show() wont work after closed
Yeah, that could be an issue. Frm2 looks an awful lot like a default instance, and those are evil because of the confusion they cause...combined with the fact that they are utterly unnecessary.
The default instance is a single instance of each type of form in your project that is quietly made available to you when the program starts. You are using the default instance if you didn't specifically create an instance using New. If that is the case, get away from using the default instances. They mostly create confusion.
By the way, as a member of the class of AT9, I like your avatar. What does the user name mean?
My usual boring signature: Nothing
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|