繁体   English   中英

Powershell文本框仅读取最后一个元素

[英]Powershell textbox only reading last element

我在工作时编写一个Powershell程序,应该在其中输入计算机名并将其解析为主机名和IP地址。 我正在使用弹出窗口和文本框从用户那里获取信息。 我遇到的问题是我的阵列仅从最底部的文本框中获取信息。

我知道如果手动创建x个数量的文本框就可以做到这一点,但是我想知道是否可以通过for循环来做到这一点? 这样,我以后可以为用户添加功能,以输入更多要解析的计算机名。

function CheckHostName {
    param(
        [Parameter(Mandatory = $true)]
        [String[]] $hostList
        )
    foreach ($hostName in $hostList) {
        try { 
            [System.Net.Dns]::Resolve($hostName) | ft -Property HostName,AddressList -AutoSize | Out-String
        } catch { 
            "$hostName does not exist" | Out-String
        }
    }
}

# Test data, for some reason this data is never overridden, even if you change the values in the textbox.
$userInput = @()
$textBoxNumber = 3 # Possible use for a more boxes button in the future
$height = $textBoxNumber * 25

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic") 

# Create the form, set its title, size, and screen position.
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,(125 + $height))
$objForm.StartPosition = "CenterScreen"

# Allows the user to click the ok button by using enter instead of click.  Also close the window
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {
    $objtextbox | % {$userInput += $_.text; Write-Host $_.text}
    [System.Windows.Forms.MessageBox]::Show((CheckHostName -hostList $userInput) ,"IP Addresses")}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
    {$objForm.Close()}})

# Create the ok button, then set its location, size, text display and function
# Clicking the ok button displays a message box and calls the CheckHostName function.
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,(50 + $height))
$OKButton.Size = New-Object System.Drawing.Size(75,25)
$OKButton.Text = "Resolve"
$OKButton.Add_Click({
    foreach($line in $objtextbox.text) {$userInput += $line; Write-Host $line}
    [System.Windows.Forms.MessageBox]::Show((CheckHostName -hostList $userInput), "IP Addresses")})
$objForm.Controls.Add($OKButton)

# Create the cancel button, then set its location, size, text display and function.
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,(50 + $height))
$CancelButton.Size = New-Object System.Drawing.Size(75,25)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Please enter the information in the space below:"
$objForm.Controls.Add($objLabel)

# Use a loop to create the textboxes, and make sure they all end up in their own unique location.
1..$textBoxNumber | % {
    $objTextBox = New-Object System.Windows.Forms.TextBox
    $objTextBox.Location = New-Object System.Drawing.Size(10,(20 + $_ * 25))
    $objTextBox.Size = New-Object System.Drawing.Size(260,20)
    $objTextBox.Name = "TextBox$_" # Textbox's Name
    $objTextBox.Text = "wsfsfc.net$_"
    $objForm.Controls.Add($objTextBox) # Create the textbox itself
}

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

您可以修改文本框控件的创建,以将它们存储在数组中,如下所示

$objTextBoxes = 1..$textBoxNumber | % {
    $objTextBox = New-Object System.Windows.Forms.TextBox
    $objTextBox.Location = New-Object System.Drawing.Size(10,(20 + $_ * 25))
    $objTextBox.Size = New-Object System.Drawing.Size(260,20)
    $objTextBox.Name = "TextBox$_" # Textbox's Name
    $objTextBox.Text = "wsfsfc.net$_"
    $objForm.Controls.Add($objTextBox) # Create the textbox itself
    $objTextBox
}

这样您就可以在click事件处理程序中使用foreach它们,例如

$OKButton.Add_Click({
    foreach($txtbox in $objTextBoxes) {
       # Do your stuff
    }
})

希望能给您带来帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM