[英]Copying Multiple Label and Textbox Inputs in Powershell GUI to clipboard for pasting
Please Help, 请帮忙,
I promised myself that I would get this working on my own, but no matter what I have tried I can't seem to figure it out. 我向自己保证,我将自己解决这个问题,但是无论我尝试了什么,我似乎都无法弄清楚。 I have tried google and so have some colleagues, but to no avail.
我试过谷歌,所以有一些同事,但无济于事。
Trying to have a GUI run for staff to enter information on jobs that they action so that updates placed into the jobs are following a specific TEMPLATE format. 尝试运行GUI以便员工输入有关他们所执行的作业的信息,以使放入作业中的更新遵循特定的TEMPLATE格式。
EXAMPLE: 例:
above is the template that would be followed with the text after "A: " (and so on) being filled out by the staff. 上面是模板,工作人员会填写“ A:”(依此类推)之后的文本。 The code I currently have can be found below.
我目前拥有的代码可以在下面找到。
[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()
You are trying to concatenate the whole label
object instead of its .Text
properties. 您试图连接整个
label
对象而不是其.Text
属性。 Change this: 更改此:
$A = $AssetLabel + " " + $AssetText
$E = $ErrorLabel + " " + $ErrorText
$I = $IssueLabel + " " + $IssueText
$TT = $TestTagLabel + " " + $TestTagText
$TS = $TroubleshootLabel + " " + $TroubleshootText
$RW = $ResolutionLabel + " " + $ResolutionText
to this: 对此:
$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
Also. 也。 You want to place this part into
.Add_Click
method for $Copybutton
. 您要将这部分放入
$Copybutton
.Add_Click
方法中。 And change Clip to accelerator class Windows.Clipboard
with its SetText
method. 并使用其
SetText
方法将Clip更改为加速器类Windows.Clipboard
。
$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)
})
Move the 'text to copy' block 移动“要复制的文本”块
# ----------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
to the $Copybutton.Add_Click({})
block and add the text property like so 到
$Copybutton.Add_Click({})
块并添加text属性,如下所示
$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
})
and the trim() you probably want that for each line input. 和trim(),您可能希望每个行输入都需要它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.