Solved

PUZZLE:  DialogResult behaves wierd

Posted on 2004-08-19
12
360 Views
Last Modified: 2010-04-23
Easy Demo:  just cut and paste...

DialogResult seems to behave differently within the context of a program, and within the context of a program afte the program has caused a fatal error....

Try this out.  Its simple cut and paste.... Just create a new vb.net windows app...
Set Main as the startup object and add the following code to a new module...
On Form2, put some button, and when you click on that button,
    raise an exception:  Throw New Exception("xxx")      <-- click event of the button....
 
Run the program and click on the button...


Public sub Main()
   dim s as Form1

   Try
      s = new Form1
      s.showdialog()            
      s.dispose
      s = nothing

   Catch  ex as exception

          dim sdb as new SaveDialogBox
          dim DR as DialogResult

         DR = sdb.ShowDialog      '<-- Choose a file that already exists and hit save...

        Messagebox.show(DR)     '<-- Shows '2' meaning Cancel, even if you selected to save

   end try
end sub



The first observation, as mentioned in the code comments is that if you choose save on a file that already exists, then DialogResult equals cancel.

The second observation is that if you choose a file that already exists, and the dialog result box tells you "Hey, buddy, thsi file already exists", and then you hit the cancel button, Dialog result box is not suppose to exit.  It is suppose to continue to let you choose another file.  That's exactly how it behaves, unless the dialog box is a part of code that has been caught due to try...catch...

IS THIS A MICROSOFT BUG OR AM I DOING SOMETHING WRONG?
0
Comment
Question by:kamleshmistry
  • 3
  • 3
  • 3
  • +3
12 Comments
 

Author Comment

by:kamleshmistry
Comment Utility

Correction....

On Form1, put some button, and when you click on that button,
    raise an exception:  Throw New ....


ETC....

( I said form2)
0
 

Author Comment

by:kamleshmistry
Comment Utility


Correction

          dim sdb as new SaveFile Dialog


(NOT SaveDialogBox)
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
Are you setting the DialogResult on the dialog form?

Bob
0
 
LVL 7

Expert Comment

by:natloz
Comment Utility
What is it you are confused about?

I tried the code...and it does indeed look like a BUG perhaps...if you don't use the TRY/CATCH exception and just put the dialog in the Button click event of the form directly...all works just fine...

I would say BUG with a capital BEE
0
 
LVL 7

Assisted Solution

by:natloz
natloz earned 100 total points
Comment Utility
I can see why you are confused...ignore my first sentence in previous post.
0
 
LVL 8

Accepted Solution

by:
wguerram earned 150 total points
Comment Utility
Yes, it looks like it is a BUG

I tried in vs 2005 beta and it is fixed.

a workaround for that problem is checking the

sdb.FileNames.Length

if Length = 0 means cancel was pressed otherwise return the number of files selected
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
Hi all.. can n e body tell me what the problem is ??? .... here I am totally confused now... What is the bug you are talking about ?? I tried your code... I am not getting any error with the Save Dialog....  may be I am not getting your point
-Baan
0
 

Author Comment

by:kamleshmistry
Comment Utility
Maybe you are using 2005
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
nope... .Net 2003
0
 
LVL 7

Expert Comment

by:natloz
Comment Utility
Baan...it only is a bug if the code for the Dialog is within the exception as in his example...

When Sub Main() is called and the form1 is instantiated it throws the exception...
returns to SubMain and Catches the exception.....

Once you pick a file in the Save Dialog...and hit OK...then it prompts if you want to Overwrite...hit NO...it should return to the dialog box to pick another file...it doesn't however...it closes and returns to the Catch....

If you select a file and hit OK then Yes to Overwrite...the message box returns a value of 2 which is incorrect as well..
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
Comment Utility
damn... I got it now... lets see... let me have some fun with that.. lol
0
 
LVL 10

Assisted Solution

by:eternal_21
eternal_21 earned 200 total points
Comment Utility
An example as simple as this should demonstrate your point, kamleshmistry:

  Module Module1

    Public Sub Main()

      Dim sdb As New SaveFileDialog
      sdb.ShowDialog()

    End Sub

  End Module

You can correct this problem as follows:

  Module Module1

    Public Sub Main()

      Dim sdb As New SaveFileDialog
      sdb.ShowDialog(new Form)

    End Sub

  End Module

So to correct your code, I would suggest you use:

### VB.NET ###

  Dim s As Form1

  Try

      s = New Form1
      s.ShowDialog()
     
  Catch

      Dim sdb As New SaveFileDialog
      Dim DR As DialogResult

      DR = sdb.ShowDialog(s)
      MessageBox.Show(DR)

  Finally

      s.Dispose()
      s = Nothing

  End Try

###
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now