Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1706
  • Last Modified:

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()



0
arcross
Asked:
arcross
  • 3
  • 2
  • 2
2 Solutions
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now