I have a problem to get the information returned by my labelLink into a richtextbox.
I've got text in the RichTextBox and I create the Labellink like this.
$Textlink = New-Object System.Windows.Forms.LinkLabel
$Textlink.text = "AMIGA"
$Textlink.Name = "AMIGALINK"
$Textlink.add_LinkClicked($Textlink_LinkClicked)
$ClassicRTB1.Controls.Add($Textlink)
It's adding an AMIGA clickable link in my RichTextBox named $ClassicRTB1
I have done this powershell statement when the button is clicked
$Textlink_LinkClicked = {
$ClassicRTB1.Clear()
$message = $_.linktext
$ClassicRTB1.AppendText("$message `n") }
It's well executing the line $ClassicRTB1.Clear() but I have nothing returned into the variable $_ for .linktext :(
I tried a lot of code all the day and found nothing ... Is what I'm doing wrong ?
CodePudding user response:
tl;dr
# 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")
}
Building on Mathias' helpful comments:
Your primary problem is your mistaken attempt to refer to the event sender (the link-label object) via
$_
, which is the wrong automatic variable to use.In the simplest case, use the automatic
$this
variable.Alternatively, given that the script block receives the usual event-delegate arguments - the event's sender and the event's arguments - you can refer to them via the automatic
$args
variable:$args[0]
also refers to the event sender, and$args[1]
to the event arguments.- A more descriptive, but more verbose alternative is to formally declare parameters for these arguments:
param($evtSender, $evtArgs)
, as shown in the top section.
Note:
- Event-related automatic variables such as
$Sender
are not available - they only apply to script blocks passed to the-Action
parameter of theRegister-ObjectEvent
andRegister-EngineEvent
cmdlets.
- Event-related automatic variables such as
Your secondary problem is that a
LinkLabel
instance has no.LinkText
property.[1]Instead, such an instance has a collection of links stored in the
.Links
property, and eachLinkLabel.Link
element of that collection has a.Data
property containing the target data (typically, a URL string, but technically it can be any object)One link is present by default, and here's an example of how you would set it:
$Textlink.Links[0].LinkData = 'https://example.org'
Because a single link-label can potentially house multiple links - if configured, for multiple substrings of its label (
.Text
property) - the robust approach to respond to a click is not to examine the link-label instance itself, but to to use the event arguments to tell you which specific link was actually clicked.- The
.Link
property of the event-arguments object passed to the event delegate identifies its specific link, whose.Data
property contains the link's target.
- The
[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.