簡體   English   中英

Powershell將文本框作為函數參數傳遞

[英]Powershell pass textbox as function argument

我正在使用PowerShell和System.Windows.Forms制作一個簡單的GUI表單。

圖片

對於每個文本框-按鈕對,我希望按鈕打開文件瀏覽器,並且一旦選擇了文件,文件路徑應寫入相應的文本框中。

在下面的代碼中, New-TextBox創建一個標簽和一個文本框, New-Button創建一個按鈕。

使用這些功能可以創建帶有3個按鈕的3個文本框。

然后,我創建了功能Set-FileDialogButton 它以按鈕和文本框為參數,並應創建一個按鈕單擊事件,以打開文件瀏覽器,獲取所選文件路徑,並將其從參數寫入文本框。

單擊事件和文件瀏覽器起作用。 但是,選擇文件后,我得到

The property 'Text' cannot be found on this object. Verify that the property exists and can b
e set.
At D:\...\test.ps1:62 char:5
+                 $inputTextBox.Text = $FileBrowser.FileName
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

所以我假設在函數中傳遞文本框時我做錯了什么。 如果我不將click事件包裝在函數中,則我的代碼可以正常工作。

編碼:

Add-Type -assembly System.Windows.Forms
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='AP209 Converter GUI'

$main_form.Width = 300
$main_form.Height = 200
$main_form.AutoSize = $true

#Creates a label and text box:
function New-TextBox($labelText, $x, $y, $w){
    $label = New-Object System.Windows.Forms.Label
    $label.Text = $labelText
    $label.Location  = New-Object System.Drawing.Point($x, $y)
    $label.AutoSize = $true
    $main_form.Controls.Add($label)

    $textbox            = New-Object system.Windows.Forms.TextBox
    $textbox.multiline  = $false
    $textbox.width      = $w
    $textbox.height     = 20
    $textbox.location   = New-Object System.Drawing.Point(($x + 50), $y)
    $main_form.Controls.Add($textbox)

    return $textbox
}
#Creates a button with $text as label:
function New-Button($text, $x, $y, $w, $h){
    $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.Text = $text
    $main_form.Controls.Add($button)
    return $button
}

$textBoxW = 100
$labelX = 0
$buttonX = $x + 155

$y = 10
$dy = 30

$buttonW = 25
$buttonH = 20

#Create 3 text boxes and buttons:
$textbox1 = New-TextBox "File 1" $labelX  $y $textBoxW
$button1  = New-Button  "..."    $buttonX $y $buttonW $buttonH
$y = $y + $dy
$textbox2 = New-TextBox "File 2" $labelX  $y $textBoxW
$button2  = New-Button  "..."    $buttonX $y $buttonW $buttonH
$y = $y + $dy
$textbox3 = New-TextBox "File 3" $labelX  $y $textBoxW
$button3  = New-Button  "..."    $buttonX $y $buttonW $buttonH


#Set click event to button:
function Set-FileDialogButton($button, $inputTextBox){
    $button.Add_Click(
        {
            $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ InitialDirectory = $loadpath}
            $fileBrowserReturned = $FileBrowser.ShowDialog()
            if($fileBrowserReturned -eq "OK"){
                $inputTextBox.Text = $FileBrowser.FileName
                $inputTextBox.SelectionStart = $inputTextBox.Text.Length;
            }                   
        }
    )
}
Set-FileDialogButton $button1 $textbox1
Set-FileDialogButton $button2 $textbox2
Set-FileDialogButton $button3 $textbox3



$main_form.ShowDialog()

您需要在定義時捕獲變量引用,並將它們綁定到事件處理程序的本地范圍。 您可以使用GetNewClosure()做到這一點:

#Set click event to button:
function Set-FileDialogButton($button, $inputTextBox){
    $button.Add_Click(
        {
            $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ InitialDirectory = $loadpath}
            $fileBrowserReturned = $FileBrowser.ShowDialog()
            if($fileBrowserReturned -eq "OK"){
                $inputTextBox.Text = $FileBrowser.FileName
                $inputTextBox.SelectionStart = $inputTextBox.Text.Length;
            }                   
        }.GetNewClosure()
    )
}

使用當前代碼,直到執行事件處理程序后,變量才會被綁定。 因此,inputTextBox為null。

嘗試這個:

Add-Type -assembly System.Windows.Forms
$main_form = New-Object System.Windows.Forms.Form
$main_form.Text ='AP209 Converter GUI'

$main_form.Width = 300
$main_form.Height = 200
$main_form.AutoSize = $true

#Creates a label and text box:
function New-TextBox($labelText, $x, $y, $w){
    $label = New-Object System.Windows.Forms.Label
    $label.Text = $labelText
    $label.Location  = New-Object System.Drawing.Point($x, $y)
    $label.AutoSize = $true
    $main_form.Controls.Add($label)

    $textbox            = New-Object system.Windows.Forms.TextBox
    $textbox.multiline  = $false
    $textbox.width      = $w
    $textbox.height     = 20
    $textbox.location   = New-Object System.Drawing.Point(($x + 50), $y)
    $main_form.Controls.Add($textbox)

    return $textbox
}
#Creates a button with $text as label:
function New-Button($text, $x, $y, $w, $h){
    $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.Text = $text
    $main_form.Controls.Add($button)
    return $button
}

$textBoxW = 100
$labelX = 0
$buttonX = $x + 155

$y = 10
$dy = 30

$buttonW = 25
$buttonH = 20

#Create 3 text boxes and buttons:
$textbox1 = New-TextBox "File 1" $labelX  $y $textBoxW
$textbox1.Name = 'textbox1'
$button1  = New-Button  "..."    $buttonX $y $buttonW $buttonH
$button1.Name = 'button1'
$y = $y + $dy
$textbox2 = New-TextBox "File 2" $labelX  $y $textBoxW
$textbox2.Name = 'textbox2'
$button2  = New-Button  "..."    $buttonX $y $buttonW $buttonH
$button2.Name = 'button2'
$y = $y + $dy
$textbox3 = New-TextBox "File 3" $labelX  $y $textBoxW
$textbox3.Name = 'textbox3'
$button3  = New-Button  "..."    $buttonX $y $buttonW $buttonH
$button3.Name = 'button3'


foreach( $control in $main_form.Controls ) {

    if( $control.Name -like 'button*' ) {
        $control.Add_Click(
            {
                $FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ InitialDirectory = $loadpath}
                $fileBrowserReturned = $FileBrowser.ShowDialog()
                if($fileBrowserReturned -eq [System.Windows.Forms.DialogResult]::OK ) {
                    $controlNumber = $this.Name -replace '^\D+(\d+)$', '$1'
                    $textboxName   = 'textbox' + $controlNumber
                    foreach( $control in $this.parent.Controls ) {
                        if( $control.Name -eq $textboxName ) {
                            $control.Text = $FileBrowser.FileName 
                            $control.SelectionStart = $control.Text.Length
                        }
                    }
                }
            } )

    }

}


$main_form.ShowDialog()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM