将Powershell GUI中的多个标签和文本框输入复制到剪贴板以进行粘贴

问题描述 投票:0回答:2

请帮忙,

我向自己承诺,我会自己开始工作,但无论我尝试过什么,我都无法弄明白。我试过谷歌,所以有一些同事,但无济于事。

尝试为员工输入GUI,以输入他们操作的作业的信息,以便放置到作业中的更新遵循特定的TEMPLATE格式。

例:

  • 答:资产编号 - 任何受影响设备的资产编号(断线)
  • E:错误文本 - 如果适用,则出现错误消息(断行)
  • I:问题文本 - 技术人员的观察是什么(Break 线)
  • TT:测试和标签 - 电力电缆是否符合T&T标准(断路 线)
  • TS:故障排除 - 在执行期间执行了哪些步骤 分辨率(Break Line)
  • RW:解决方案/解决方法 - 如何解决问题(Break Line)

上面是工作人员填写“A:”(等等)之后的文本。我目前拥有的代码可以在下面找到。

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Windows.Forms.Application]::EnableVisualStyles()

$Form = New-Object system.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(900,320)
$Form.MaximizeBox = $false
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = 'Fixed3D'
$Form.Text = "Ticket Updates"

# ----------CREATE LABELS---------- #
$AssetLabel = New-Object System.Windows.Forms.Label
$AssetLabel.Text = "A:"
$AssetLabel.AutoSize = $true
$AssetLabel.Location = New-Object System.Drawing.Size(21,15)
$Font = New-Object System.Drawing.Font("Consolas",14,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$Form.Controls.Add($AssetLabel)

$ErrorLabel = New-Object System.Windows.Forms.Label
$ErrorLabel.Text = "E:"
$ErrorLabel.AutoSize = $true
$ErrorLabel.Location = New-Object System.Drawing.Size(21,50)
$Font = New-Object System.Drawing.Font("Consolas",14,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$Form.Controls.Add($ErrorLabel)

$IssueLabel = New-Object System.Windows.Forms.Label
$IssueLabel.Text = "I:"
$IssueLabel.AutoSize = $true
$IssueLabel.Location = New-Object System.Drawing.Size(21,85)
$Font = New-Object System.Drawing.Font("Consolas",14,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$Form.Controls.Add($IssueLabel)

$TestTagLabel = New-Object System.Windows.Forms.Label
$TestTagLabel.Text = "TT:"
$TestTagLabel.AutoSize = $true
$TestTagLabel.Location = New-Object System.Drawing.Size(10,120)
$Font = New-Object System.Drawing.Font("Consolas",14,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$Form.Controls.Add($TestTagLabel)

$TroubleshootLabel = New-Object System.Windows.Forms.Label
$TroubleshootLabel.Text = "TS:"
$TroubleshootLabel.AutoSize = $true
$TroubleshootLabel.Location = New-Object System.Drawing.Size(10,155)
$Font = New-Object System.Drawing.Font("Consolas",14,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$Form.Controls.Add($TroubleshootLabel)

$ResolutionLabel = New-Object System.Windows.Forms.Label
$ResolutionLabel.Text = "RW:"
$ResolutionLabel.AutoSize = $true
$ResolutionLabel.Location = New-Object System.Drawing.Size(10,190)
$Font = New-Object System.Drawing.Font("Consolas",14,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$Form.Controls.Add($ResolutionLabel)
# ----------END LABELS---------- #

# ----------CREATE TEXT BOXES---------- #
$AssetText = New-Object System.Windows.Forms.TextBox
$AssetText.Size = New-Object System.Drawing.Size(750,20)
$AssetText.Location = New-Object System.Drawing.Size(50,12)
$Font = New-Object System.Drawing.Font("Consolas",14,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$AssetText.Text = "<Asset Number(s)> - Asset number(s) of any affected equipment"
$Form.Controls.Add($AssetText)

$ErrorText = New-Object System.Windows.Forms.TextBox
$ErrorText.Size = New-Object System.Drawing.Size(750,20)
$ErrorText.Location = New-Object System.Drawing.Size(50,47)
$Font = New-Object System.Drawing.Font("Consolas",14,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$ErrorText.Text = "<Error Text> - Error message if applicable"
$Form.Controls.Add($ErrorText)

$IssueText = New-Object System.Windows.Forms.TextBox
$IssueText.Size = New-Object System.Drawing.Size(750,20)
$IssueText.Location = New-Object System.Drawing.Size(50,82)
$Font = New-Object System.Drawing.Font("Consolas",14,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$IssueText.Text = "<Issue Text> - What the technician's observation is"
$Form.Controls.Add($IssueText)

$TestTagText = New-Object System.Windows.Forms.TextBox
$TestTagText.Size = New-Object System.Drawing.Size(750,20)
$TestTagText.Location = New-Object System.Drawing.Size(50,117)
$Font = New-Object System.Drawing.Font("Consolas",14,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$TestTagText.Text = "<Test & Tag> - Were the power cables T&T compliant"
$Form.Controls.Add($TestTagText)

$TroubleshootText = New-Object System.Windows.Forms.TextBox
$TroubleshootText.Size = New-Object System.Drawing.Size(750,20)
$TroubleshootText.Location = New-Object System.Drawing.Size(50,152)
$Font = New-Object System.Drawing.Font("Consolas",14,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$TroubleshootText.Text = "<Troubleshooting> - What steps were performed during the resolution"
$Form.Controls.Add($TroubleshootText)

$ResolutionText = New-Object System.Windows.Forms.TextBox
$ResolutionText.Size = New-Object System.Drawing.Size(750,20)
$ResolutionText.Location = New-Object System.Drawing.Size(50,187)
$Font = New-Object System.Drawing.Font("Consolas",14,[System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$ResolutionText.Text = "<Resolution/Workaround> - How you fixed the problem"
$Form.Controls.Add($ResolutionText)

# ----------END TEXT BOXES---------- #

# ----------CREATE BUTTONS---------- #
$Resetbutton = New-Object System.Windows.Forms.Button 
$Resetbutton.Location = New-Object System.Drawing.Size(810,11) 
$Resetbutton.Size = New-Object System.Drawing.Size(70,30) 
$Resetbutton.Text = "Reset" 
$Resetbutton.Add_Click({$AssetText.Text = "<Asset Number(s)> - Asset number(s) of any affected equipment"
                        $ErrorText.Text = "<Error Text> - Error message if applicable"
                        $IssueText.Text = "<Issue Text> - What the technician's observation is"
                        $TestTagText.Text = "<Test & Tag> - Were the power cables T&T compliant"
                        $TroubleShootText.Text = "<Troubleshooting> - What steps were performed during the resolution"
                        $ResolutionText.Text = "<Resolution/Workaround> - How you fixed the problem"}) 
$Form.Controls.Add($Resetbutton)

$Copybutton = New-Object System.Windows.Forms.Button 
$Copybutton.Location = New-Object System.Drawing.Size(810,187) 
$Copybutton.Size = New-Object System.Drawing.Size(70,30) 
$Copybutton.Text = "COPY"
$Copybutton.Add_Click({$TEMPLATE.Text.Trim() | Clip}) 
$Form.Controls.Add($Copybutton)

$Exitbutton = New-Object System.Windows.Forms.Button 
$Exitbutton.Location = New-Object System.Drawing.Size(810,222) 
$Exitbutton.Size = New-Object System.Drawing.Size(70,30) 
$Exitbutton.Text = "Exit" 
$Exitbutton.Add_Click({$Form.Close()})
$Form.Controls.Add($Exitbutton)
# ----------END BUTTONS---------- #

# ----------Text to Copy---------- #

$A = $AssetLabel + " " + $AssetText
$E = $ErrorLabel + " " + $ErrorText
$I = $IssueLabel + " " + $IssueText
$TT = $TestTagLabel + " " + $TestTagText
$TS = $TroubleshootLabel + " " + $TroubleshootText
$RW = $ResolutionLabel + " " + $ResolutionText

$TEMPLATE = $A, $E, $I, $TT, $TS, $RW

# ----------End Text to Copy---------- #



$Form.ShowDialog()
javascript powershell
2个回答
0
投票

移动“要复制的文本”块

# ----------Text to Copy---------- #

$A = $AssetLabel + " " + $AssetText
$E = $ErrorLabel + " " + $ErrorText
$I = $IssueLabel + " " + $IssueText
$TT = $TestTagLabel + " " + $TestTagText
$TS = $TroubleshootLabel + " " + $TroubleshootText
$RW = $ResolutionLabel + " " + $ResolutionText

$TEMPLATE = $A, $E, $I, $TT, $TS, $RW

$Copybutton.Add_Click({})块并添加文本属性,如此

$Copybutton.Add_Click({

    $A = $AssetLabel.Text + " " + $AssetText.Text
    $E = $ErrorLabel.Text + " " + $ErrorText.Text
    $I = $IssueLabel.Text + " " + $IssueText.Text
    $TT = $TestTagLabel.Text + " " + $TestTagText.Text
    $TS = $TroubleshootLabel.Text + " " + $TroubleshootText.Text
    $RW = $ResolutionLabel.Text + " " + $ResolutionText.Text

    $TEMPLATE = $A, $E, $I, $TT, $TS, $RW | foreach {$_.Trim()}
    $TEMPLATE | Clip
})

你可能想要每个线路输入的trim()。


0
投票

您正在尝试连接整个label对象而不是其.Text属性。改变这个:

$A = $AssetLabel + " " + $AssetText
$E = $ErrorLabel + " " + $ErrorText
$I = $IssueLabel + " " + $IssueText
$TT = $TestTagLabel + " " + $TestTagText
$TS = $TroubleshootLabel + " " + $TroubleshootText
$RW = $ResolutionLabel + " " + $ResolutionText

对此:

$A = $AssetLabel.Text + " " + $AssetText.Text
$E = $ErrorLabel.Text + " " + $ErrorText.Text
$I = $IssueLabel.Text + " " + $IssueText.Text
$TT = $TestTagLabel.Text + " " + $TestTagText.Text
$TS = $TroubleshootLabel.Text + " " + $TroubleshootText.Text
$RW = $ResolutionLabel.Text + " " + $ResolutionText.Text

也。你想把这部分放到.Add_Click$Copybutton方法中。并使用Windows.Clipboard方法将Clip更改为加速器类SetText

$Copybutton.Add_Click({
$A = $AssetLabel.Text + " " + $AssetText.Text
$E = $ErrorLabel.Text + " " + $ErrorText.Text
$I = $IssueLabel.Text + " " + $IssueText.Text
$TT = $TestTagLabel.Text + " " + $TestTagText.Text
$TS = $TroubleshootLabel.Text + " " + $TroubleshootText.Text
$RW = $ResolutionLabel.Text + " " + $ResolutionText.Text

$TEMPLATE = $A.Trim(), $E.Trim(), $I.Trim(), $TT.Trim(), $TS.Trim(), $RW.Trim()
[Windows.Clipboard]::SetText($TEMPLATE)
}) 
© www.soinside.com 2019 - 2024. All rights reserved.