Solved

PUZZLE:  DialogResult behaves wierd

Posted on 2004-08-19
12
367 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
ID: 11845376

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
ID: 11845776


Correction

          dim sdb as new SaveFile Dialog


(NOT SaveDialogBox)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 11845965
Are you setting the DialogResult on the dialog form?

Bob
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 7

Expert Comment

by:natloz
ID: 11845967
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
ID: 11845985
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
ID: 11846287
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
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11846781
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
ID: 11846793
Maybe you are using 2005
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11846934
nope... .Net 2003
0
 
LVL 7

Expert Comment

by:natloz
ID: 11847545
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
ID: 11847833
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
ID: 11851820
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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