我在將 labelLink 回傳的資訊放入富文本框中時遇到問題。
我在 RichTextBox 中有文本,我像這樣創建了 Labellink。
$Textlink = New-Object System.Windows.Forms.LinkLabel
$Textlink.text = "AMIGA"
$Textlink.Name = "AMIGALINK"
$Textlink.add_LinkClicked($Textlink_LinkClicked)
$ClassicRTB1.Controls.Add($Textlink)
它在我的 RichTextBox 中添加了一個 AMIGA 可點擊鏈接,名為 $ClassicRTB1
單擊按鈕時,我已經完成了這個 powershell 陳述句
$Textlink_LinkClicked = {
$ClassicRTB1.Clear()
$message = $_.linktext
$ClassicRTB1.AppendText("$message `n") }
它很好地執行了 $ClassicRTB1.Clear() 行,但我沒有回傳到變數 $_ for .linktext :(
我整天嘗試了很多代碼,但一無所獲……我做錯了嗎?
uj5u.com熱心網友回復:
tl;博士
# This assumes that you've actually assigned a URL to the
# $TextLink instance - see the bottom section for how to do that.
$Textlink_LinkClicked = {
# Declare the event delegate's parameters.
param($evtSender, $evtArgs)
# Retrieve the target info (typically, a URL) associated
# with the link that was clicked.
$message = $evtArgs.Link.LinkData
$ClassicRTB1.AppendText("$message `n")
}
基于 Mathias 的有用評論:
您的主要問題是您錯誤地嘗試通過 參考事件
$_發送者(鏈接標簽物件),這是使用錯誤的自動變數。在最簡單的情況下,使用自動
$this變數。$args或者,假設腳本塊接收通常的事件委托引數 - 事件的發送者和事件的引數 - 您可以通過自動變數參考它們:$args[0]也指事件發送者和$args[1]事件引數。- 一個更具描述性但更詳細的替代方法是正式宣告這些引數的引數:
param($evtSender, $evtArgs),如頂部所示。
筆記:
- 與事件相關的自動變數如
$Sender不可用- 它們僅適用于傳遞給和cmdlet-Action引數的腳本塊。Register-ObjectEventRegister-EngineEvent
- 與事件相關的自動變數如
您的
LinkLabel.LinkText次要問題是實體沒有屬性。[1]相反,這樣的實體具有存盤在屬性中的鏈接集合
.Links,并且該集合的每個LinkLabel.Link元素都有一個.Data包含目標資料的屬性(通常是URL 字串,但從技術上講,它可以是任何物件)默認情況下存在一個鏈接,以下是如何設定它的示例:
$Textlink.Links[0].LinkData = 'https://example.org'
因為單個鏈接標簽可能包含多個鏈接 - 如果已配置,則針對其標簽(屬性)的多個子字串
.Text- 回應點擊的穩健方法不是檢查鏈接標簽實體本身,而是使用事件引數告訴您實際點擊了哪個特定鏈接。.Link傳遞給事件委托的事件引數物件的屬性標識其特定鏈接,其屬性.Data包含鏈接的目標。
[1] You may be thinking of the .LinkText property of the event-arguments object that is available if you respond to the LinkClicked event of a RichTextBox instance itself, in response to clicking a link that that instance itself provides, due to recognizing raw URLs in its text (and, in .NET (Core), also RTF hyperlinks assigned via .Rtf) and turning them into clickable links.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/450350.html
下一篇:DataGridView'OnRowValidating(DataGridViewCellCancelEventArgse)'方法未呼叫第一行
