Go Premium for a chance to win a PS4. Enter to Win

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

PUZZLE: DialogResult behaves wierd

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
kamleshmistry
Asked:
kamleshmistry
  • 3
  • 3
  • 3
  • +3
3 Solutions
 
kamleshmistryAuthor Commented:

Correction....

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


ETC....

( I said form2)
0
 
kamleshmistryAuthor Commented:


Correction

          dim sdb as new SaveFile Dialog


(NOT SaveDialogBox)
0
 
Bob LearnedCommented:
Are you setting the DialogResult on the dialog form?

Bob
0
Industry Leaders: 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!

 
natlozCommented:
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
 
natlozCommented:
I can see why you are confused...ignore my first sentence in previous post.
0
 
wguerramCommented:
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
 
DotNetLover_BaanCommented:
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
 
kamleshmistryAuthor Commented:
Maybe you are using 2005
0
 
DotNetLover_BaanCommented:
nope... .Net 2003
0
 
natlozCommented:
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
 
DotNetLover_BaanCommented:
damn... I got it now... lets see... let me have some fun with that.. lol
0
 
eternal_21Commented:
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

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!

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