error: "cannot access a disposed object named 'form1'

G

Guest

Hi,

Here's my code

'in form

WithEvents formReplace As New Form

Private Sub cmdFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFind.Clic
formReplace.Show(
End Su

'in form
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Clic
Me.clos
End Su

What happens is on the first time that i click on the button in form 1 (cmdFind), form 2 will appear. after closing form2 and click on the button in form 1 again to show form 2, this error appears: "cannot access a disposed object named 'form1

Any ideas? thanks :
 
E

EricJ

try
Private Sub cmdFind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdFind.Click
formReplace = new Form2
formReplace.Show()
End Sub

hope it helps

eric

ed said:
Hi,

Here's my code:

'in form 1

WithEvents formReplace As New Form2

Private Sub cmdFind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdFind.Click
formReplace.Show()
End Sub

'in form2
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCancel.Click
Me.close
End Sub

What happens is on the first time that i click on the button in form 1
(cmdFind), form 2 will appear. after closing form2 and click on the button
in form 1 again to show form 2, this error appears: "cannot access a
disposed object named 'form1'
 
C

Cor Ligthert

Hi Ed,

As alternative from Erics solution you can use the form.showdialog

(Otherwise the user can access both in the same time, however you have
almost no control about it)

The solution is the same as Eric showed however instead of show, showdialog.

Cor
 
E

EricJ

hi cor :)
as you might have noticed i'm back on winforms

but on the subject, some thoughts
a find or find an replace form shouldn't be dialog you need to be able to go
back .. also to open multiple find and replace forms.
personally i wouldn't declare form 2 like this
WithEvents formReplace As New Form2

but keep it local
Private Sub cmdFind_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdFind.Click
dim formReplace as new form2
' formReplace = new Form2
formReplace.Show()
End Sub

I think this is better for the GC

Eric
 
C

Cor Ligthert

Hi EricJ,

I saw also that you have a new keyboard, with a Caps key

:)

Somewhere is adviced for the showdialog to dispose it always however I do
not know why and therefore I set it not anymore in my samples.

I think that when you want what you say, you should not close a form however
hide it. I made a sample for that, but I think for you I can tell it as
well, the basic is that is handle the visible-change from the forms in the
main form.

Looks very nice in my opinon. And of course not one GC problem.

Cor
 
C

Cor Ligthert

Hi Eric

Hi I even did not see that you did not use upercases in your text, I saw it
with Pieter and I tought, he, first I thought then he write only to Belgians
with upercases, but than I saw it was probably copied.

:))

(I did not think about that special threathment for Belgians of course)

Cor
 
E

EricJ

And I was wondering where the Caps key was coming from ;)

and if you where referring to the
Environment.GetFolderPath(Environment.SpecialFolder.System)
that was indeed copied straight from the VS Help ;) a wonderfull place with
lots of info ;) (to bad usually a cat can't find her cittens there, you'll
know the expression)

Eric

Ps i still owe you a chinese dinner, come and get it :)
 

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