'Get infos returned by a labellink in a richtextbox (.NET FORMS)
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 ?
Solution 1:[1]
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
$thisvariable.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
$argsvariable:$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
$Senderare not available - they only apply to script blocks passed to the-Actionparameter of theRegister-ObjectEventandRegister-EngineEventcmdlets.
- Event-related automatic variables such as
Your secondary problem is that a
LinkLabelinstance has no.LinkTextproperty.[1]Instead, such an instance has a collection of links stored in the
.Linksproperty, and eachLinkLabel.Linkelement of that collection has a.Dataproperty 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 (
.Textproperty) - 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
.Linkproperty of the event-arguments object passed to the event delegate identifies its specific link, whose.Dataproperty 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mklement0 |
