Using Variables to open child forms

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
This topic is 1 year and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
leejcooper
Posts: 3
Last visit: Wed Feb 01, 2023 9:02 am

Using Variables to open child forms

Post by leejcooper »

Hi

I am building a multi form project and have a child form that is loaded with radiobuttons that if a user selects a button that becomes a variable of $Site which will link to another child form. The problem i am having is when the use selects the continue button the message box displays fine with the $SiteName variable but the child form does not open. I am stuck as to what what code to use so that the Show-$Site.psf opens the correct form as i have tried different commands but nothing seems to work.

Code as below

$buttonContinue_Click= {
#TODO: Place custom script here
if ($radiobuttonABC.Checked)
{

$Sitename = 'ABC'
$Site = 'formABC'
$msg = "You selected ISE Site $Sitename. Are you sure you wish to proceed?"
$result = [System.Windows.Forms.MessageBox]::Show($msg, 'Warning', 'YesNo')
if ($result -eq 'Yes')
{


#$formxxxxxxx.Close()
Show-"$Site".psf
}
Thank you in appreciation
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 3:14 pm
Answers: 39
Been upvoted: 30 times

Re: Using Variables to open child forms

Post by brittneyr »

If you are using the generated form reference function, it named like the following:
Show_FileName_psf

You may find the following article helpful:
https://www.sapien.com/blog/2022/08/03/ ... -projects/
Brittney
SAPIEN Technologies, Inc.
User avatar
leejcooper
Posts: 3
Last visit: Wed Feb 01, 2023 9:02 am

Re: Using Variables to open child forms

Post by leejcooper »

Thanks Brittany, so I substitute the form name with the variable will that work?
User avatar
leejcooper
Posts: 3
Last visit: Wed Feb 01, 2023 9:02 am

Re: Using Variables to open child forms

Post by leejcooper »

Hi Brittney, sorry spelt your name wrong in last post, I have tried what you suggested but all i get is :Error the term Show_$Site_psf is not recognised as the name of a cmdlet, function script file or operable program.
I have 10 radiobuttons on the form so need to have it open the form from the variable otherwise have to add the message box 10 times which works but trying to avoid this.
Any help will be much appreciated.
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 3:14 pm
Answers: 39
Been upvoted: 30 times

Re: Using Variables to open child forms

Post by brittneyr »

While I believe it is better to directly call the function, you can dynamically call function using & or invoke-expression on a string. With this method, you may have to slightly modify how you call the function to be entirely a string.

For more information, the following links may be helpful:
https://learn.microsoft.com/en-us/power ... rshell-7.3
https://renenyffenegger.ch/notes/Window ... call/index
Brittney
SAPIEN Technologies, Inc.
This topic is 1 year and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked