Solved

PUZZLE:  DialogResult behaves wierd

Posted on 2004-08-19
12
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Technology Partners: 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!

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

628 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