Sapien Powershell表单关闭按钮事件

时间:2018-04-18 09:35:19

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v4.0

可能涉及的人,

这是我使用Sapien powershell工作室的第一周,如果我遇到一些非常容易做的事情,我会提前抱歉这项技术。

我正在创建一个GUI表单我想禁用红色x并在表单上放置关闭按钮,但我似乎无法让关闭按钮工作,请参阅我在下面使用的代码。

$ buttonClose_Click = {

$ formADSubmissionForm.Close = $ true

2 个答案:

答案 0 :(得分:0)

$buttonClose_Click={

$formADSubmissionForm.Close}

If you want to hide the x and all the controls at the top of the form try

$formADSubmissionForm.ControlBox = $False;

好的,请参阅下面的代码以获取简单的表格。如果单击开始按钮,它将关闭表单。

# Settings

$registryPath = "HKCU:\Software\CC\Scripts"
$regroot = "HKCU:\Software\CC"
$Name = "Canceled"
$sleepTime = 10  # minimise time in seconds
$value = 1

Function CancelUpgrade {
  IF(!(Test-Path $registryPath))

{

  New-Item -Path $registryPath -Force | Out-Null

  New-ItemProperty -Path $registryPath -Name $name -Value $value `


}

 ELSE 
   {
    $value = (Get-ItemProperty -path $registryPath).$Name
    $value++
    Set-ItemProperty -Path $registryPath -Name $name -Value $value  | Out-Null

}

# Minimise form 
  $form.WindowState = 1
  start-sleep $sleepTime
  $form.WindowState = 0

  If ((Get-ItemProperty -path $registryPath).$Name -gt 2)
    {
    $Buttoncn.Enabled = $false

    }
}

$Form = New-Object System.Windows.Forms.Form
### Hyperlink
$Form.AutoScaleMode = "DPI"
$LinkLabel = New-Object System.Windows.Forms.LinkLabel
$LinkLabel.Location = New-Object System.Drawing.Size(30,50)
$LinkLabel.Size = New-Object System.Drawing.Size(200,20)
$LinkLabel.LinkColor = "BLUE"
$LinkLabel.Text = "Click here for more information"
$LinkLabel.add_Click({[system.Diagnostics.Process]::start("http://bbc.co.uk ")})
$Form.Controls.Add($LinkLabel)

###### Add Buttons

$Buttonhlp = New-Object System.Windows.Forms.Button 
$Buttonhlp.Location = New-Object System.Drawing.Size(50,100) 
$Buttonhlp.Size = New-Object System.Drawing.Size(50,30) 
$Buttonhlp.Text = "Help" 

$Buttonhlp.Add_Click({
[system.Diagnostics.Process]::start("http://bbc.co.uk")})

$Buttonst = New-Object System.Windows.Forms.Button 
$Buttonst.Location = New-Object System.Drawing.Size(120,100) 
$Buttonst.Size = New-Object System.Drawing.Size(50,30) 
$Buttonst.Text = "Start" 
$Buttonst.Add_Click({
  $form.Close()
  IF((Test-Path $registryPath)){
  Remove-Item -Path $regroot -Recurse
  }
})

$Buttoncn = New-Object System.Windows.Forms.Button 
$Buttoncn.Location = New-Object System.Drawing.Size(220,100) 
$Buttoncn.Size = New-Object System.Drawing.Size(50,30) 
$Buttoncn.Text = "Cancel" 
$Buttoncn.Add_Click({
   CancelUpgrade

})

### Text box
$outputBox = New-Object System.Windows.Forms.TextBox #creating the text box
$outputBox.Location = New-Object System.Drawing.Size(10,20) #location of the 
text box (px) in relation to the primary window's edges (length, height)
$outputBox.Size = New-Object System.Drawing.Size(100,150) #the size in px of 
the text box (length, height)
$outputBox.MultiLine = $True #declaring the text box as multi-line
#$outputBox.ScrollBars = "Vertical" #adding scroll ba0 if required
$outputBox.Text = "This is some text "
$outputBox.BackColor = "White"
$outputBox.Enabled = $false



#Panel
$label = new-object system.windows.forms.label
$label.location = New-Object System.Drawing.Point(10, 10)
$Label.Size = New-Object System.Drawing.Size(270, 30)
$label.Text = " This is some text"
$label.add_click({[system.Diagnostics.Process]::Start("WWW.BBC.co.uk")})

$form.Text = "Hello"

$Form.Controls.Add($label)
$Form.Controls.Add($Buttoncn)
$Form.Controls.Add($Buttonst)
$Form.Controls.Add($Buttonhlp)
$Form.ShowDialog()

答案 1 :(得分:0)

您需要添加eval function "()"。试试这个

$formADSubmissionForm.Close()