MDI form - close exception

Hi,
this is the situation...

Ive got a MDI form, from a menu that MDI form opens a child form:
 
dim fNew as frmNewJob= New frmNewJob
fNew.MdiParent = Me
fNew.Show()

Now, from this child i open another child of the MDI form:

dim frm as new OtherChildForm
frm.MdiParent = Me.MdiParent
frm.Show()

When this second form, opens, in the load event, im checking if that form contains any data. If not, a dialog form is shown from there:
'// in load event...

If dv.Count = 0 Then
            If MessageBox.Show("No data") = Windows.Forms.DialogResult.Yes Then
                Dim frm As New DialogForm
                If frm.ShowDialog = Windows.Forms.DialogResult.Cancel Then
                          Me.Close()              '---->>> HERE EXCEPTION
                Else
                    '// Scroll to last row
                    bs.MoveLast()
                End If
            Else
                Me.Close()            '-----> HERE EXCEPTION
            End If
 End If

The exception i get when i cancel that dialog form says:
"Value Close() cannot be called while doing the CreateHandle()"

Any ideas on this??????

Just a note, if  i dont open the the child form from the other child, its fine.

dim frm as new OtherChildForm
frm.MdiParent = Me.MdiParent      '---> IF I REMOVE THIS LINE, DONT GET THE EXCEPTION !
frm.Show()



LVL 8
arcrossAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omegaomegaDeveloperCommented:
Hello, arcross,

That's interesting.  It seems like it may be a timing issue.  You could try putting a Timer on OtherChildForm.  Move the Close method to the Tick event of the timer and where you are now doing "Me.Close" place "Timer1.Enabled = True".  

It seems as if the Timer Interval  can be as short as you like.  I set an interval of 1 ms on the timer, and it seemed to solve the problem.

Cheers,
Randy
0
arcrossAuthor Commented:
Hi randy thanks for your response.

This is a similiar problem than mine.
http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic25653.aspx

but i cant see the difference from mine.


0
appariCommented:

problem is you cannot close the form from load event.

alternative is
1. in VS 2005 use form's shown event handler instead of load event. in load event set some flag and check that flag in shown event and close it there.

2. put all the error checking code in a public function and check it from the calling form and load the second form only f condition is true, something like this

in Form1

dim fNew as frmNewJob= New frmNewJob
if fNew.CanShow then
   fNew.MdiParent = Me
   fNew.Show()
else
   fNew = nothing
end if



in Form2 ( shift the code from your current load event to the following function)

public function CanShow() as boolean
 If dv.Count = 0 Then
            If MessageBox.Show("No data") = Windows.Forms.DialogResult.Yes Then
                Dim frm As New DialogForm
                If frm.ShowDialog = Windows.Forms.DialogResult.Cancel Then
                          return false
                Else
                    '// Scroll to last row
                    bs.MoveLast()
                End If
            Else
                return false
            End If
 End If
return true
end function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

omegaomegaDeveloperCommented:
Hello, arcross,

Yes, it looks like the link that you mention is essentially the same problem as the one you are having.  Although my understanding of what you are trying to do isn't perfect, I think the solution outlined there will work for you too.  

This is just a matter of not even showing the "problem" form if it is just going to be closed anyway.  If this will work for you it is a much better solution than introducing a timer.   The solution in the link is similar to what  appari is suggesting as his second alternative.  The only difference is where the validity checking method is located.

(I don't think the first alternative will help.  I found that the problem was the same even if the code was moved to the Activate event.  Perhaps if the code could somehow be delayed until the second activate event it might succeed, but this would be even more obtuse than using a timer.)

Cheers,
Randy
0
appariCommented:
>>I don't think the first alternative will help.
shown event is new in .net 2.0 framework. it works perfectly, i am using it in my project.
0
omegaomegaDeveloperCommented:
Hello, appari,

Sorry.  I read your post too quickly and somehow thought that you were talking about the Activate event.  I don't (yet) have 2.0 and didn't know about the Shown event.  

But even so, I think that just not showing the form if it's inappropriate to do so (i.e. your second alternative) is a better solution.  The "validation" method could still reside in that form (as suggested in the OP's link).

Cheers,
Randy
0
arcrossAuthor Commented:
Thanks to you both!

I really apreaciate your help, i was going crackers with this!!!

Álvaro
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.