Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms Powershell传递文本框作为函数参数_Winforms_Powershell - Fatal编程技术网

Winforms Powershell传递文本框作为函数参数

Winforms Powershell传递文本框作为函数参数,winforms,powershell,Winforms,Powershell,我正在使用PowerShell和System.Windows.Forms制作一个简单的GUI表单 对于每个文本框-按钮对,我希望按钮打开文件浏览器,一旦选择文件,文件路径应写入相应的文本框中 在下面的代码中,新建文本框创建标签和文本框,新建按钮创建按钮 使用这些功能创建3个文本框和3个按钮 然后我创建了函数Set FileDialogButton。它将按钮和文本框作为参数,并应创建一个按钮单击事件以打开文件浏览器,获取选定的文件路径,并将其从参数写入文本框 单击事件和文件浏览器工作。但是,在选

我正在使用PowerShell和
System.Windows.Forms
制作一个简单的GUI表单

对于每个文本框-按钮对,我希望按钮打开文件浏览器,一旦选择文件,文件路径应写入相应的文本框中

在下面的代码中,
新建文本框
创建标签和文本框,
新建按钮
创建按钮

使用这些功能创建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()
试试这个:

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()
试试这个:

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()

您需要在定义时捕获变量引用,并将它们绑定到事件处理程序的本地范围。您可以使用
GetNewClosure()
执行此操作:

#将单击事件设置为按钮:
函数集FileDialogButton($button,$inputTextBox){
$button.Add\u单击(
{
$FileBrowser=New Object System.Windows.Forms.OpenFileDialog-Property@{InitialDirectory=$loadpath}
$fileBrowserReturned=$FileBrowser.ShowDialog()
如果($fileBrowserReturned-eq“OK”){
$inputTextBox.Text=$FileBrowser.FileName
$InputExtBox.SelectionStart=$InputExtBox.Text.Length;
}                   
}.GetNewClosure()
)
}

在执行事件处理程序之前,当前代码不会绑定变量。因此inputTextBox为空。

您需要在定义时捕获变量引用,并将它们绑定到事件处理程序的本地范围。您可以使用
GetNewClosure()
执行此操作:

#将单击事件设置为按钮:
函数集FileDialogButton($button,$inputTextBox){
$button.Add\u单击(
{
$FileBrowser=New Object System.Windows.Forms.OpenFileDialog-Property@{InitialDirectory=$loadpath}
$fileBrowserReturned=$FileBrowser.ShowDialog()
如果($fileBrowserReturned-eq“OK”){
$inputTextBox.Text=$FileBrowser.FileName
$InputExtBox.SelectionStart=$InputExtBox.Text.Length;
}                   
}.GetNewClosure()
)
}

在执行事件处理程序之前,当前代码不会绑定变量。因此InputExtBox为空。

如果您在AddClick事件中单独处理每个filebrowse按钮,您的代码将更简单,可读性也更高。@Distibilist的想法是通过在函数中包装AddClick来删除重复的代码,并使其更具可读性…如果您只需在AddClick事件中单独处理每个filebrowse按钮,您的代码就会更容易,可读性也会更高。@Dis疑论者的想法是通过在函数中包装AddClick来删除重复代码,并使其更具可读性…答案@Palle Due非常有效。我也尝试过这个,但是看起来像
$control.Name-像“button*”
永远不会返回true?您必须给控件命名。复制并粘贴我的源代码完成,它将工作。答案@Palle Due工程伟大。我也尝试过这个,但是看起来像
$control.Name-像“button*”
永远不会返回true?您必须给控件命名。复制粘贴我的源代码完成,它将工作。