views:

690

answers:

3

Hi, I'm developing a WinForms app in VB.NET, that handles sets of style data, and when the user clicks on another set's label, it prompts through a dialog "You are leaving this style preset to edit another one. keep changes on this one? [Yes] [No]"

But, I'm facing the problem that, when the user clicks either option, and the dialog closes, everything has to be refreshed, and loading the form again seems a good option.

I've tried putting a public sub on a module, that does this:

Public Sub CloseOpenStyleDlg()
    KeepOrDiscardPrompt.Close()
    StylesDlg.Close()
    StylesDlg.ShowDialog()
End Sub

But as soon as that sub is called from the prompt, it crashes the application. (doesn't show an error in debug, simply crashes) How should I, from a given dialog, close the dialog, it's parent, and re-open it's parent? (which triggers all the Dialog_Load() code of the parent)

Thanks in advance! :)

A: 

When a form is closed, all resources created within the object are closed and the form is disposed.

If you want to reuse the Window instance use StylesDialog.Hide() function instead.

Claudiu
But, hide does call the Form_Load() event again? Intuitively, I supposed it was something like making it invisible but without closing.
Camilo Martin
I didn't test it but I suppose Form_Load() is not called when you are calling Show again.
Claudiu
Hiding then showing a form will not raise the Load event. If you have initialization code that needs to be called again it can be removed from the Form_Load method and put in another method and called explicitly.
Snarfblam
That's what Hide function does it sets the form visibility to false.If you call Close you will not be able to reopen to same instance of the form as it is disposed.
Claudiu
A: 

You need to instantiate the dialog again. If I take your code for example:

Public Sub CloseOpenStyleDlg()
    KeepOrDiscardPrompt.Close()
    StylesDlg.Close()
    StylesDlg = new StylesDlg()
    StylesDlg.ShowDialog()
End Sub
Jon
Thanks =) I re-arranged my code outside Form_Load(), and from now on I'll abide to best-practices almost always (badly-written code is such a hell to mantain!), but your example solved my doubt on how to re-open a form, so it's choosen =)
Camilo Martin
A: 

what is KeepOrDiscardPrompt

asd
I've solved this one some time ago, but it was basically along the lines of "Do you want to keep this style or discard it (and go back to the former one)?".
Camilo Martin