我有一個創建文本框(資訊框)的函式,可以從其他函式或按鈕呼叫。
function InfoBox ($x,$y,$text){
$TextboxInfoBox = New-Object System.Windows.Forms.TextBox
$TextboxInfoBox.Location = New-Object System.Drawing.Size($x,$y)
$TextboxInfoBox.Size = New-Object System.Drawing.Size(200,800)
# Readonly Textbox.
$TextboxInfoBox.Enabled = $true
$TextboxInfoBox.Text = $text
$TabPanelButtons.Controls.Add($TextboxInfoBox)
}
在我呼叫文本框一次后,我無法用另一條資訊訊息覆寫它。我第一次使用所有引數($x、$y 和 $text)呼叫。
Infobox -x '100' -y '150' -text 'This is the first message'
第二次我只想給另一個文本。如何清除文本框并僅提供新文本,例如:
Infobox -text "This is the second message"
有任何想法嗎?
uj5u.com熱心網友回復:
正如評論者所寫,該函式必須輸出 TextBox 物件,以便您稍后可以參考它來更改文本:
function InfoBox ($x,$y,$text){
$TextboxInfoBox = New-Object System.Windows.Forms.TextBox
$TextboxInfoBox.Location = New-Object System.Drawing.Size($x,$y)
$TextboxInfoBox.Size = New-Object System.Drawing.Size(200,800)
# Readonly Textbox.
$TextboxInfoBox.Enabled = $true
$TextboxInfoBox.Text = $text
$TabPanelButtons.Controls.Add($TextboxInfoBox)
$TextboxInfoBox # Output
}
與大多數其他編程語言相反,在 PowerShell 中,您通常不需要使用return陳述句,除非從函式中提前回傳。簡單地在自己的行上按名稱參考變數將從函式中輸出其值。
現在您可以將輸出存盤在變數中并更改文本:
$box = Infobox -x 100 -y 150 -text 'This is the first message'
$box.Text = 'This is the second message'
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/414548.html
標籤:
