Help!

G

Guest

Hello:

I have a command button on FormA whose click event is coded as:

If foSearchForm Is Nothing Then
foSearchForm = New frmSearchInfo(Me)
End If
foSearchForm.Show()

where foSearchForm is a Private variable.

The first time the button is clicked, it correctly brings up the SearchForm.
If I change focus to FormA and click on the button again, it rightly brings
the SearchForm into focus.

But when I close the SearchForm and click on the button, I get the following
exception thrown:

Cannot access a disposed object named "frmSearchInfo".

What am I doing wrong?

Venkat
Cannot access a disposed object named "frmSearchInfo".
 
H

Herfried K. Wagner [MVP]

vvenk said:
I have a command button on FormA whose click event is coded as:

If foSearchForm Is Nothing Then
foSearchForm = New frmSearchInfo(Me)
End If
foSearchForm.Show()

\\\
If foSearchForm Is Nothing OrElse foSearchForm.IsDisposed Then
foSearchForm = New FrmSearchForm()
End If
foSearchForm.Show()
///
Cannot access a disposed object named "frmSearchInfo".

If a form is shown by calling its 'Show' method, it's disposed automatically
when being closed.
 
G

Guest

Hi vvenk

Just to add to Herfried's post, at the point you close a form, the form is
considered done with, and can't be used anymore. Any further attempt to use
the form results in an exception.

However, as you have noticed, your search form reference in FormA is not
(yet)nothing - if it were, then your code would create a new instance of the
search form. This is a consequence of having a reference tracing garbage
collector running on a separate thread - until the garbage collector runs,
your reference is not nothing.

One way to deal with that is to explicitly set your reference to the search
form to nothing in FormA when the search form closes.

HTH

Nigel Armstrong
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top