Solved

Child Dialog Closes Parent Dialog in VB.NET

Posted on 2009-05-15
8
721 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Saved searches can save you time by quickly referencing commonly searched terms on any topic. Whether you are looking for questions you can answer or hoping to learn about a specific issue, a saved search can help you get the most out of your time o…

707 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

12 Experts available now in Live!

Get 1:1 Help Now