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
How to prevent child form closing after click on a button in child form.
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
-
- Posts: 15
- Last visit: Wed Jan 19, 2022 4:49 pm
Re: How to prevent child form closing after click on a button in child form.
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
See: https://info.sapien.com/index.php/guis/ ... sing-forms
-
- Posts: 15
- Last visit: Wed Jan 19, 2022 4:49 pm
Re: How to prevent child form closing after click on a button in child form.
Thanks for your reply. But what I'm care about is not the return value.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
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.
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.
-
- Posts: 15
- Last visit: Wed Jan 19, 2022 4:49 pm
Re: How to prevent child form closing after click on a button in child form.
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.
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.
-
- Posts: 15
- Last visit: Wed Jan 19, 2022 4:49 pm
Re: How to prevent child form closing after click on a button in child form.
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
}
}
-
- Posts: 15
- Last visit: Wed Jan 19, 2022 4:49 pm
Re: How to prevent child form closing after click on a button in child 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.
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.
I recommend reading the Sapien literature on how to design and use forms. It will save you a lot of confusion going forward.