Solved

Child Dialog Closes Parent Dialog in VB.NET

Posted on 2009-05-15
8
730 Views
Last Modified: 2012-05-07
Hi,
I have a program I'm writing in VB.Net and in the main form (Form1) there is a button which opens another form (Form2) as a dialog and allows the user to choose an output directory. Next to the textbox where the user may enter a file path I've placed a browse button to open a FolderBrowserDialog. What happens when the user clicks on either OK or Cancel in the browse Folder Dialog is that the parent (form2) dialog closes as if it's ok or cancel button has been pressed. Is there any way to prevent the BrowseFolderDialog Ok and Cancel Button from closing the parent dialog?
0
Comment
Question by:jdannemann
  • 3
  • 3
  • 2
8 Comments
 
LVL 12

Expert Comment

by:needo_jee
ID: 24399745
hi

I have done this and seems working without any issue


'--Parent/main form


Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Form2.ShowDialog() = Windows.Forms.DialogResult.OK Then

        End If
    End Sub
End Class


'--form2


Public Class Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim d As New OpenFileDialog
        If d.ShowDialog() = Windows.Forms.DialogResult.OK Then

        End If
    End Sub
End Class


-thanks


0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24399747
That is not normal behaviour...can you show us how you open Form2 and the also the code in Form2 where you close it?
0
 
LVL 1

Author Comment

by:jdannemann
ID: 24399922
Form 1 opens Form 2 like so:

Form2.ShowDialog()

There is no Ok button or Cancel Button on Form 2, though I do have my own buttons which read ok and cancel, and the cancelButton property for Form2 is set to none.

The Browse Dialog is opened from the browse button in form2 like so:

FolderBrowserDialog1.ShowDialog

Interestingly enough, when the Ok button on the Browse Dialog is pressed, it returns to Form2 with Windows.Forms.DialogResult.OK, but then closes Form2, and returns to Form1 with Windows.Forms.DialogResult.Cancel.

Oddly enough, this still happens even if I make my Cancel Button the Cancel button, or have no cancel button... it has to be a windows message being interpreted wrongly or sent to the wrong window... only thing I can think of.



0
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!

 
LVL 1

Author Comment

by:jdannemann
ID: 24399953
Hey needo_jee,
Thanks for the suggestion, but when the ok button is pressed in the browse dialog the dialog result is OK once returning to Form2 and Cancel once returning to Form1, but Cancel is never pressed.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 24400026
How are you closing Form2 after the FolderBrowserDialog() has been shown?
0
 
LVL 12

Accepted Solution

by:
needo_jee earned 500 total points
ID: 24400027
if you are talking about . ok is checked and cancel not then can do like this

 If d.ShowDialog() = Windows.Forms.DialogResult.OK Then

else

'do what you want

        End If


although it should work correctly and you seems having weired issue but as an alternate you may use your own flags to signal each form when to close



like in closing even of form check the closeReason , it must be "none" when it automatically try to close on child dialogue

when you want it to close .. you may call ..close method and in form closing event it would tell the reason


on the base of closeReason in form closing event force the form to keep opened by canceling the event ... like

if e.closeReason=closeReason.None
 e.cancel=true
else  *******************

'' do other things..
end if




-thanks
0
 
LVL 1

Author Closing Comment

by:jdannemann
ID: 31582102
Hey Needo_jee,
Thanks for the suggestion. I was surprised though when I found that the event arguments object returning to form2 from the browse dialog was comming up empty. Therefore, the solution you suggested didn't actually work. However (can't believe I didn't think about this sooner), I took your suggestion and put in some code for the Form closing event on form2, and the event object came back with all the nessessary properties for me to prevent the form from closing.

Thank you so much!

Private Sub ExportSettings_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If e.CloseReason = CloseReason.None Then e.Cancel = True
End Sub
0
 
LVL 12

Expert Comment

by:needo_jee
ID: 24425478
:) my pleasure
0

Featured Post

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.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Stuck in voice control mode on your Amazon Firestick?  Here is how to turn it off!!!
The Bounty Board allows you to request an article or video on any technical topic, or fulfill a bounty request to earn points. Watch this video to learn how to use the Bounty Board to get the content you want, earn points, and browse submitted bount…
Where to go on the main page to find the job listings. How to apply to a job that you are interested in from the list that is featured on our Careers page.

730 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