PowerShell 按钮单击事件脚本块不起作用 [英] PowerShell button click event scriptblock not working

查看:30
本文介绍了PowerShell 按钮单击事件脚本块不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发带有多个 TabPage 的 GUI.每个 TabPage 中有几个按钮应该做".

I am working on a GUI with several TabPages. There are within each TabPage several buttons that should 'do' s.th.

所以我定义了 $..OnClick={..} 在这个例子中写 s.th.- 但它不起作用.请问怎么了?

So I defined the $..OnClick={..} which in this example write s.th. - but it doesn't work. Please what's wrong?

以下是一个完整示例,显示对话框中的按钮 Check 1,但该按钮根本不起作用:

Here is a full example that shows the button Check 1 in a dialog, but the button isn't working at all:

function guibutton ($window,$todo,[string]$name="Butty",[string]$text="Butty",
                    [int]$x=300,[int]$y=200,[int]$w=75,[int]$h=23) {
  $Button = New-Object System.Windows.Forms.Button
  $Button.Location = New-Object System.Drawing.Size($x,$y)
  $Button.Size = New-Object System.Drawing.Size($w,$h)
  $Button.Name = $name
  $Button.Text = $text
  $Button.Add_Click( { $todo } )
  $window.Controls.Add($Button)
}


#Generated Form Function
function GenerateForm {

  #region Import the Assemblies
  [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
  [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null

  #region Generated Form Objects
  $MainMenu = New-Object System.Windows.Forms.Form
  $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

  #Buttons
  $TestTabA = New-Object System.Windows.Forms.Button
  $EXIT = New-Object System.Windows.Forms.Button
  $TabControl = New-Object System.Windows.Forms.TabControl

  #Misc Items
  $ProgressBar = New-Object System.Windows.Forms.ProgressBar
  $StatusBar = New-Object System.Windows.Forms.StatusBar

  #Tabs
  $TabControl = New-object System.Windows.Forms.TabControl
  $TabTestA = New-Object System.Windows.Forms.TabPage

  #----------------------------------------------
  #Generated Event Script Blocks
  #----------------------------------------------
  #Provide Custom Code for events specified in PrimalForms.
  #Unknown
  $handler_MainMenu_Load =
  $OnLoadForm_StateCorrection= { $MainMenu.WindowState = $InitialFormWindowState }

  #Buttons
  $TestTabA_OnClick = { $TabControl.SelectTab($TabTestA) }
  $EXIT_OnClick={
     Write-Host "Bye-Bye"
     $MainMenu.close()
  }

  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 474
  $System_Drawing_Size.Width = 665

  $MainMenu.Font = New-Object System.Drawing.Font("Mistral",8.25,0,3,0) # Schriftart festlegen
  $MainMenu.ClientSize = $System_Drawing_Size
  $MainMenu.DataBindings.DefaultDataSourceUpdateMode = 0
  $MainMenu.Name = "MainMenu"
  $MainMenu.Text = "Test GUI"
  $MainMenu.TopMost = $True
  $MainMenu.add_Load($handler_form1_Load)

  # == FIX-Trade Button == #
  $TestTabA.Name = "TestTabA"
  $TestTabA.Text = "Test TabA"
  $TestTabA.TabIndex = 2
  $TestTabA.UseVisualStyleBackColor = $True
  $TestTabA.add_Click($TestTabA_OnClick)
  $TestTabA.DataBindings.DefaultDataSourceUpdateMode = 0
  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 12
  $System_Drawing_Point.Y = 69
  $TestTabA.Location = $System_Drawing_Point
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 35
  $System_Drawing_Size.Width = 100
  $TestTabA.Size = $System_Drawing_Size
  $MainMenu.Controls.Add($TestTabA)


  # == EXIT all Button == #
  $EXIT.Name = "EXIT"
  $EXIT.Text = "EXIT"
  $EXIT.TabIndex = 7
  $EXIT.UseVisualStyleBackColor = $True
  $EXIT.add_Click($EXIT_OnClick)
  $EXIT.DataBindings.DefaultDataSourceUpdateMode = 0
  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 12
  $System_Drawing_Point.Y = 274
  $EXIT.Location = $System_Drawing_Point
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 35
  $System_Drawing_Size.Width = 100
  $EXIT.Size = $System_Drawing_Size

  $MainMenu.Controls.Add($EXIT)

  # == Tab Control == #
  $TabControl.Name = "TabControl"
  $TabControl.TabIndex = 4
  $TabControl.SelectedIndex = 0
  $TabControl.DataBindings.DefaultDataSourceUpdateMode = 0
  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 118
  $System_Drawing_Point.Y = 70
  $TabControl.Location = $System_Drawing_Point
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 374
  $System_Drawing_Size.Width = 535
  $TabControl.Size = $System_Drawing_Size

  # Add the following to the TabControl property-setting section:

  $TabSizeMode = New-object System.Windows.Forms.TabSizeMode
  $TabSizeMode = "Fixed"
  $TabControl.SizeMode =$TabSizeMode
  $TabControl.ItemSize = New-Object System.Drawing.Size(0, 1)
  $TabAppearance = New-object System.Windows.Forms.TabAppearance
  $TabAppearance = "Buttons"
  $TabControl.Appearance = $TabAppearance
  $MainMenu.Controls.Add($TabControl)


  $Check1Button_OnClick  = { Write-Host "Check1-Click, mach was.. " }
  # == Trade Tab == #
  $TabTestA.DataBindings.DefaultDataSourceUpdateMode = 0
  $TabTestA.Name = "TestTabA"
  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 4
  $System_Drawing_Point.Y = 22
  $TabTestA.Location = $System_Drawing_Point
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 205
  $System_Drawing_Size.Width = 445
  $TabTestA.Size = $System_Drawing_Size
  $TabTestA.TabIndex = 2
  $TabTestA.Text = "Tab2"
  $TabTestA.UseVisualStyleBackColor = $True

  #                         name     Txt        x   y    w  h
  guibutton $TabTestA $Check1Button_OnClick "Check1"   "Check 1"    10  20  75 23
  $TabControl.Controls.Add($TabTestA)

  # == Progress Bar == #
  $progressBar.DataBindings.DefaultDataSourceUpdateMode = 0
  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 589
  $System_Drawing_Point.Y = 458
  $progressBar.Location = $System_Drawing_Point
  $progressBar.Name = "progressBar"
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 15
  $System_Drawing_Size.Width = 75
  $progressBar.Size = $System_Drawing_Size
  $progressBar.TabIndex = 0

  $MainMenu.Controls.Add($progressBar)

  # == Status bar == #
  $StatusBar.DataBindings.DefaultDataSourceUpdateMode = 0
  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 0
  $System_Drawing_Point.Y = 456
  $StatusBar.Location = $System_Drawing_Point
  $StatusBar.Name = "StatusBar"
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 18
  $System_Drawing_Size.Width = 665
  $StatusBar.Size = $System_Drawing_Size
  $StatusBar.TabIndex = 1
  $StatusBar.Text = "statusBar - Text..."
  $StatusBar.add_PanelClick($statusBar_PanelClick)

  $MainMenu.Controls.Add($StatusBar)

  #To show that the tabs are switching correctly, you could add:
  $TabTestA.BackColor = "White"

  # == Save and Open Form == #
  $InitialFormWindowState = $MainMenu.WindowState
  $MainMenu.add_Load($OnLoadForm_StateCorrection)
  $MainMenu.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm

如果我启动 GUI,对话框就会出现,但是如果我点击应该 write-host ".." 的按钮,什么都不会发生:(

If I start the GUI the dialog appears, but if I click on the button that should write-host ".." nothing happens :(

推荐答案

您正在将传入的脚本块包装在另一个脚本块中.更改这一行:

You are wrapping the passed in scriptblock in another scriptblock. Change this line:

$Button.Add_Click({$todo})

$Button.Add_Click($todo)

您可能还想将 $todo 的类型指定为脚本块.

You also might want to specify the type of $todo as a scriptblock.

function guibutton($window,[scriptblock]$todo,[string]$name="Butty",[string]$text="Butty", ...

将来,您应该尝试将问题脚本降低到与问题重复的最小可能脚本.我刚好在你的脚本顶部发现了这个问题.

In the future, you should try to get the problem script down to the minimum possible script that duplicates the problem. I just happened to spot the problem at the top of your script.

这篇关于PowerShell 按钮单击事件脚本块不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆