Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PUZZLE:  DialogResult behaves wierd

Posted on 2004-08-19
12
Medium Priority
?
382 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 400 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 600 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 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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