mdi and form disposal error

M

Matt84121

I have a really stupid question, and cannot find the answer elsewhere.
I have a mdi that uses a class. One (main) form has two textboxes which
takes input, and the input is calculated by the class and then returned
to some labels on the same form. When I open the form from a main menu
control it works fine, if I close it using the me.close command, and
then try to reopen the form, using the same mainmenu control I recieve
an error that states: "Cannot access a disposed object named".
I've tried to instantiate a second instance of the form:

dim frm1 As New frm1
frm1.mdiparent=me
frm1.show()

however, when I do this the text boxes on the main form no longer pass
the input to the class. What is an easy fix for this?
 
C

Cor Ligthert [MVP]

Matt,

Your message is confusing. There is no method to open a form and still you
write that you "reopen" a form.

This can that you mean show it again or instance it again. For us that is
impossible to see, so please write this kind of things clear.

However, probably will the method "form.isdisposed" help you as it mostly do
in this kind of cases.

I hope this helps,

Cor
 
C

CMM

Not sure I understand how you're coding this (who is maintining the
reference to the form... is it the MDIParent?). All I can say is that
frm.Close is not the opposite of frm.Show. Hide the form instead of closing
it.
 
M

Matt84121

Thanks for your time.

What if the user selects the "x" close control on the task bar? That
..close() the form correct? I attempted to go about the .hide method,
however it maintains the inputed data previously entered in the text
fields (which I don't want). Is there a way to clear all the fields
prior to showing the form after hiding it?
 
C

CMM

What if the user selects the "x" close control on the task bar? That
.close() the form correct?

Are you using VB2005? If so, in the FormClosing event, determine if the
*user* is attempting to close the form, hide it instead and cancel the
close. (This is trickier in VB2003... but there are solutions).
I attempted to go about the .hide method,
however it maintains the inputed data previously entered in the text
fields (which I don't want). Is there a way to clear all the fields
prior to showing the form after hiding it?

Look into the VisibilityChanged event and clear the textboxes there either
when the form is hidden or when it is shown.
 
M

Matt84121

C.Moya,

Yes, I'm using 2003 version of .Net. I can remove the control box, thus
removing the "X" close. Then use the Hide event. However, I'm not sure
this is the best way to go. The VisibilityChanged Event is exactly what
I was looking for thank you!
 
C

CMM

Here's how to trap for "user closing" and hide the form in VB2003.
---

Private m_userClosing As Boolean = False

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

Try
'WM_SYSCOMMAND / SC_CLOSE
If m.Msg = &H112 AndAlso m.WParam.ToInt32 = &HF060 Then
m_userClosing = True
End If
Catch
'do nothing
Finally
MyBase.WndProc(m)
End Try

End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

If m_userClosing Then
e.Cancel = True
m_userClosing = False
Me.Hide()
End If

End Sub
 

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