Page 1 of 1

How to prevent child form closing after click on a button in child form.

Posted: Wed Mar 25, 2020 7:52 pm
by ivan.liao@live.com
Hi everyone here,

I just created a multi-form project. A child form used for load settings and change program running settings.
There are 3 buttons in this child form. Which are 'Reset default', 'OK', and 'Cancel'.
Both 3 buttons are corresponding to call a function to do something.
But don't know why child form will be closed and return 'CANCEL' after called my function in click event.
How can I remains in child form after I clicked 'Reset default' button. And how can I return 'OK' after click OK button?

Thanks in advance!

Here is some of the codes, I have not add other events for this child form.
# I call a function to load my settings while child form load
$frmSettings_Load={
Get-Settings
}

# here is click event for 'Reset default' to load and set my default settings, don't know why it will close the child form and return 'CANCEL'
$btnSettingDefault_Click={
Set-DefaultSettings
}

# and save my settings while click on OK button, don't know why it will close the child form and return 'CANCEL'
$btnSettingOK_Click={
Set-Settings
}

# I called this child form in main form via this, but $SettingsChanged always is 'CANCEL'
$SettingChanged = Show-ChildForm_psf

Re: How to prevent child form closing after click on a button in child form.

Posted: Wed Mar 25, 2020 8:53 pm
by jvierra
You cannot return a value like that from a child form. Use the global variables created by child forms.

See: https://info.sapien.com/index.php/guis/ ... sing-forms

Re: How to prevent child form closing after click on a button in child form.

Posted: Wed Mar 25, 2020 10:10 pm
by ivan.liao@live.com
jvierra wrote: Wed Mar 25, 2020 8:53 pm You cannot return a value like that from a child form. Use the global variables created by child forms.

See: https://info.sapien.com/index.php/guis/ ... sing-forms
Thanks for your reply. But what I'm care about is not the return value.
I just want to find a way to keep child form open after click on 'Reset default' button. So that I can continue to adjust my settings after restore default settings.

Re: How to prevent child form closing after click on a button in child form.

Posted: Wed Mar 25, 2020 10:18 pm
by jvierra
You cannot both close a child form and keep it open. When you close the form it closes and control is returned to the parent form. There is no other option.

Re: How to prevent child form closing after click on a button in child form.

Posted: Wed Mar 25, 2020 10:32 pm
by ivan.liao@live.com
jvierra wrote: Wed Mar 25, 2020 10:18 pm You cannot both close a child form and keep it open. When you close the form it closes and control is returned to the parent form. There is no other option.
I'm not close the child form in my code. I have 3 buttons in this child form, just don't know why all these button will close the form even there is no close form code inside these button click event.

I finally find a stupid way to prevent child form close.

# I have to use a variable to track my button action, set true value if I'm click on Reset default button, and add formclosing event to prevent closing if I'm click on Reset default button.
$frmSettings_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
if ($script:DefaultSettings)
{
$_.Cancel = $true
}
}

It does help to solve my problem. But just don't know why any button added in child form will close child form after its button click event.

Re: How to prevent child form closing after click on a button in child form.

Posted: Wed Mar 25, 2020 10:39 pm
by jvierra
If a button has a dialog result set then it will close the form. Be sure that you have not set a "DialogResult" on any buttons that you don't want to close a form.

Re: How to prevent child form closing after click on a button in child form.

Posted: Wed Mar 25, 2020 10:40 pm
by ivan.liao@live.com
jvierra wrote: Wed Mar 25, 2020 10:18 pm You cannot both close a child form and keep it open. When you close the form it closes and control is returned to the parent form. There is no other option.
Seems I found a better solution here.

# set difference value for DialogResult
$btnSettingOK_Click={
Set-Settings
$frmSettings.DialogResult = 'OK'
}

$btnSettingDefault_Click={
Set-DefaultSettings
$frmSettings.DialogResult = 'Ignore'
}

$btnSettingCancel_Click={
$frmSettings.DialogResult = 'Cancel'
}

# cancel the closing event if DialogResult is Ignore.
$frmSettings_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
if ($frmSettings.DialogResult -eq 'Ignore')
{
$_.Cancel = $true
}
}

Re: How to prevent child form closing after click on a button in child form.

Posted: Wed Mar 25, 2020 10:42 pm
by ivan.liao@live.com
jvierra wrote: Wed Mar 25, 2020 10:39 pm If a button has a dialog result set then it will close the form. Be sure that you have not set a "DialogResult" on any buttons that you don't want to close a form.
Thanks for your remind!
Now it does really help me to solve the problem.

Thanks again!

Re: How to prevent child form closing after click on a button in child form.

Posted: Wed Mar 25, 2020 10:44 pm
by jvierra
All of that is unnecessary and is not how Forms are designed to be used. This misunderstanding will cause you many headaches as you continue to use forms. Just remove the Dialog result from all buttons and if you want to close the form in a button then just call $fome.Close().
I recommend reading the Sapien literature on how to design and use forms. It will save you a lot of confusion going forward.