BUG in VB .NET 2003?

  • Thread starter Thread starter Andy
  • Start date Start date
A

Andy

Hi to All,

I found the following issue, is this a bug?

Create a new project and, in the main form, add a listbox with a few sample
items. In the DoubleClick event of the listbox, add:

Me.Dispose()

Then run the project, double click on an item in the listbox to close the
window and then an Exception occurs.

What is the problem? I would like to close the form by double clicking on
the listbox...
Thank you,

Andy
 
Not a bug. Do not Dispose() in a click event. I am not real Windows Forms
savvy, but there is a way to close the form. You can Dispose() afterward,
but you have to make sure there is a Dispose() method coded for the class
you are disposing. Not sure if a form class has a Dispose() method without
adding IDisposable. Either way, you do not want to Dispose() the object in
the object.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Hi Andy,
You are not closing it, you are killing it, look at the message from Patrick
Cor
 
But I need to close (unload) the form from a double click event...

I searched with google and, strangely, in the first version of VB6 there was
a bug: if you tried to unload the form from a listbox's doubleclick event, a
GPF occurred. This was solved in later SP. What coincidence!

Andy
 
The point I am getting at is Dispose() is not the proper way to destroy an
object from inside the object. I would aim for close, but you are stating
you are getting the same message there. I do not understand the need to
close from doubleclick rather than set up a close button, but each app is
different.

This causes no problems for me in VS.NET 2003:

Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) _
Handles ListBox1.DoubleClick
Try
Me.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
'Finally
' MessageBox.Show("In Finally")
End Try
End Sub

unless I uncomment the Finally. This makes sense, as I have closed the form
by this point in time. I cannot run through debug on this machine, as I need
to catch someone to get permissions (aaarrrggghhhh!!!).

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Try adding

Application.DoEvents before the close method...

I do'nt know if it will work, but dispose is for garbage collection, and not
the right way to shut down a window. Close calls dispose through Finalize I
believe.
 
CJ Taylor said:
Try adding

Application.DoEvents before the close method...

I do'nt know if it will work, but dispose is for garbage collection, and not
the right way to shut down a window. Close calls dispose through Finalize I
believe.

So, what is the best method to "unload" a form? Me.Close() or Me.Dispose()
or what else? In VB6 there was Unload Me...
Thanks,

Andy
 
Andy said:
Hi to All,

I found the following issue, is this a bug?

Create a new project and, in the main form, add a listbox with a few sample
items. In the DoubleClick event of the listbox, add:

Me.Dispose()

Then run the project, double click on an item in the listbox to close the
window and then an Exception occurs.

What is the problem? I would like to close the form by double clicking on
the listbox...
Thank you,

Andy

I have a guess at what's happening, and a possibly workaround.

I think what's happening is that when your DoubleClick handler is
executing, the call stack has the context of the ListBox's event
handler, which is busy looping through the Multicastdelegate object that
holds the list of DoubleClick events. You call the Close() or Dispose()
method on the Form, which in turn disposes the ListBox control.

When your event handler returns, it goes back to the ListBox control's
code that's looping through events. But since that object has been
disposed - bang... an exception. My guess is that this situation is
explicitly handled in a button's click event handler, since that's the
way Forms are normally closed.

If this is what's happening, then I'd agree that it's a bug, and should
be fixed by MS.

However, in the meantime you can use the following code to get the
Close() method to be called after you return from your DoubleClick event
handler (watch for word wrap):

Public Class Form1
Inherits System.Windows.Forms.Form

Delegate Sub CloseDelegate() ' declare a delegate type that
matches the Close() method

'
' whatever else you need in your class
'

Private Sub ListBox1_DoubleClick(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.DoubleClick

' invoke the Close() method through an async delegate
Me.BeginInvoke(CType(AddressOf Me.Close, CloseDelegate))
End Sub
End Class

This calls the Close() method() through an async delegate, so it'll be
called at sometime after your call to BeginInvoke(). One thing to be
aware of is that I'm not sure if this is bullet-proof. It's not clear
to me that this will always complete all ListBox events before invoking
your async delegate.

To make that kind of guarantee would require a more complex set of
safeguards, probably involving timers, locks, and/or hooking the event
chain higher up the class hierarchy.
 
Andy said:
Create a new project and, in the main form, add a listbox with a few sample
items. In the DoubleClick event of the listbox, add:

Me.Dispose()

Then run the project, double click on an item in the listbox to close the
window and then an Exception occurs.

Doesn't 'Me.Close()' close the form?
 
Andy said:
So, what is the best method to "unload" a form? Me.Close() or
Me.Dispose() or what else? In VB6 there was Unload Me...
Thanks,

Use Me.Close

As already said, Dispose kills the form and this also means that no closing
or closed events are fired.
 
Hi,

If you are seeing an exception from Me.Close, something else is cause it.
This is the correct way to close a form.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
Back
Top