?
Solved

Child Dialog Closes Parent Dialog in VB.NET

Posted on 2009-05-15
8
Medium Priority
?
760 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:Munawar Hussain
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
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:
Munawar Hussain earned 2000 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:Munawar Hussain
ID: 24425478
:) my pleasure
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Finding a job can be stressful - searches, resume tweaks, and networking events can be super boring. Luckily we're here to help you land your dream job!
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
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…
Notifications on Experts Exchange help you keep track of your activity and updates in one place. Watch this video to learn how to use them on the site to quickly access the content that matters to you.
Suggested Courses

850 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