Hi, i’m trying to check if a form is already loaded ( visible ),<\/p>\n
if loaded to something<\/p>\n
if not do other things<\/p>\n
if ($form1.ShowDialog()) {\n\nWrite-Host -ForegroundColor Magenta \"FORM ALREADY LOADED\"\n\n$Form1.Refresh()\n}\n\nelse {\nWrite-Host -ForegroundColor Cyan \"FORM WILL BE CREATED\"\n[void] $Form1.ShowDialog()\n}\n\n<\/code><\/pre>","upvoteCount":5,"answerCount":2,"datePublished":"2017-05-31T11:12:42.000Z","author":{"@type":"Person","name":"ramses147fox","url":"https://community.spiceworks.com/u/ramses147fox"},"acceptedAnswer":{"@type":"Answer","text":"You could try something like this maybe.<\/p>\n
$form = new-object system.windows.forms.form\n\n$form.show()\n\nif($form.ishandlecreated)\n{\n write-host \"form is opened\"\n}\nelse\n{\n write-host \"form is closed\"\n}\n<\/code><\/pre>\nEDIT: That may not really work for you, depending on what you’re doing. You could try something like this also, not really sure what your form is meant to do though.<\/p>\n
Add-Type -AssemblyName System.Windows.Forms\n\nfunction checkformexists\n{\n\tif($job.iscompleted)\n\t{\n\t\t#do stuff if the form is closed\n\t}\n\telse\n\t{\n\t\t#do stuff if the form is opened\n\t}\n}\n\n#script block for the form creation\n$showform = {\n\t$form = new-object system.windows.forms.form\n\t$form.showdialog()\n}\n\n#create the runspace and add our script block to show the form\n$newrunspace = [powershell]::create().addscript($showform)\n\n#runs the new instance\n$job = $newrunspace.begininvoke()\n\n#use the function to check if the form is opened (if the job has not completed)\ncheckformexists\n<\/code><\/pre>\nBasically the idea is to run a job in a new instance, since if you use ShowDialog() in your main instance code execution will stop until the form is closed. The function checks whether the job is completed (the form is closed) or not (the form is still opened) and executes the blocks of code accordingly.
\nI’m definitely not a pro at this stuff, but hopefully that helps in some way at least.<\/p>","upvoteCount":0,"datePublished":"2017-06-07T16:25:01.000Z","url":"https://community.spiceworks.com/t/check-if-a-form-is-already-loaded/584242/2","author":{"@type":"Person","name":"frankholden8186","url":"https://community.spiceworks.com/u/frankholden8186"}},"suggestedAnswer":[{"@type":"Answer","text":"
Hi, i’m trying to check if a form is already loaded ( visible ),<\/p>\n
if loaded to something<\/p>\n
if not do other things<\/p>\n
if ($form1.ShowDialog()) {\n\nWrite-Host -ForegroundColor Magenta \"FORM ALREADY LOADED\"\n\n$Form1.Refresh()\n}\n\nelse {\nWrite-Host -ForegroundColor Cyan \"FORM WILL BE CREATED\"\n[void] $Form1.ShowDialog()\n}\n\n<\/code><\/pre>","upvoteCount":5,"datePublished":"2017-05-31T11:12:42.000Z","url":"https://community.spiceworks.com/t/check-if-a-form-is-already-loaded/584242/1","author":{"@type":"Person","name":"ramses147fox","url":"https://community.spiceworks.com/u/ramses147fox"}}]}}