Solved

Child Dialog Closes Parent Dialog in VB.NET

Posted on 2009-05-15
8
723 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 85

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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
This story has been written with permission from the scammed victim, a valued client of mine – identity protected by request.
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.
Articles on a wide range of technology and professional topics are available on Experts Exchange. These resources are written by members, for members, and can be written about any topic you feel passionate about. Learn how to best write an article t…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now