Script doesnt run 1ste time completely

2 days ago i posted a topic about disabling ALT+F4 and Escape keys when running a Form script in powershell. Apparently the codes i used did work but it only works the second time i run it. so i open powershell ISE (in admin mode). run the script and the the ALT+F4 doenst work. i run the script again and then i does work. below i copied a code form some i used partitally for my own script. anyone an idea how to solve this?


[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Restart Required"
$objForm.Size = New-Object System.Drawing.Size(400,300) 
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Topmost = $True
$objForm.MinimizeBox = $false
$objForm.MaximizeBox = $false
$objForm.FormBorderStyle = "Fixed3d" 
#$objForm.ControlBox = $false
$objForm.ShowInTaskbar = $false
$objForm.Add_Shown({$objForm.Activate()})
$objForm.add_formclosing($form1_FormClosing)
$objform.add_KeyDown($form1_KeyDown)
[void] $objForm.ShowDialog()

$objform.KeyPreview = $True


$form1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]

    if ($_.Alt -eq $true -and $_.KeyCode -eq 'F4') {
        $script:altF4Pressed = $true;           
    }
}

$form1_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.FormClosingEventArgs]

    if ($script:altF4Pressed)
    {
        if ($_.CloseReason -eq 'UserClosing') {
            $_.Cancel = $true
            $script:altF4Pressed = $false;
        }
    }
}

Based on the sheer amount of answers you received for your other question, you can probably guess that such a topic is not that common. I’d say you’re trying to use the wrong tool for the job. :wink:

I don’t know the processes you’re dealing with, but it feels strange for me to disable some keys to force users to do something or to avoid them to do something. And I’d expect solutions like that not to be very robust or reliable. Who are those users? Instead of forcing them you may educate them to make them understand the need of the settings you want to do.

Another option would be to re-launch the GUI immediately if not all required information have been provided.

just when i posted this, short after i found the solution. its a thing for students who need to fill in some information. but even when you tell those kids what to do and how, there are some who are still doing it wrong. like closing the form when they need to fill in the info :wink: so thats why i want to disable altF4 and escape. thx for your reply anyway.

Cool. Great to hear. You could post your solution here. That could help others having the same or a similar issue.

added this

$objForm.add_FormClosing({$_.Cancel=$true})    
[void] $objForm.ShowDialog()
$OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Add_FormClosing({$_.Cancel=$false});$objForm.Close()})

i got the answer from this website

How to disable Alt-F4 in a powershell form? (microsoft.com)

1 Like

Thanks for sharing. :+1:t4: :slightly_smiling_face: