Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Child Dialog Closes Parent Dialog in VB.NET

Posted on 2009-05-15
8
Medium Priority
?
756 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
[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
  • 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
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 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

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.

Question has a verified solution.

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

This story has been written with permission from the scammed victim, a valued client of mine – identity protected by request.
With the shift in today’s hiring climate (http://blog.experts-exchange.com/ee-blog/5-tips-on-succeeding-in-the-new-gig-economy/?cid=Blog_031816), many companies are choosing to hire freelancers to get projects completed efficiently and inexpensively…
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.
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.

609 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