First time asking a question here. I'm working on designing a dynamic popup script to be used by other people.
One of the needs for this script is to have a popup that allows an everyday tech to create a message that has multiple links.
To do this, I've been trying to create a form with a single linklabel that has multiple links added to Linklabel.Links
The problem I've encountered is that while I can get a hyperlink to work when the user clicks on the label, I can't get powershell to differentiate between which hyper was clicked by the user.
Below is a snippet of my code. I have built custom tags to identify when the user want's to create a hyperlink
Add-Type -AssemblyName System.Windows.Forms
#....
# User's custom popup message.
# Original script sorts through this information to identify where the hyper links should be.
$testLink = 'test <hlink-start><text-start>Click Here<text-end>http://www.google.com<hlink-end> Words Words <hlink-start>https://www.vmware.com<hlink-end> done'
# Stores the linklabel text after the tags in $testLink have been sorted through
$NewLabelText = ""
# Custom object that stores the url of each hyperlink; the startPosition; and the Length of text for the url
$URLinfo = New-Object -TypeName PSObject
#... Skipping code that sorts through $testLink
#... Outcome is the following:
#...
#... $NewLabelText =
#... 'test Click Here Words Words https://www.vmware.com done'
#...
#... $URLinfo =
#... URL StartPos LinkLength
#... --- -------- ----------
#... http://www.google.com 5 10
#... https://www.vmware.com 28 22
# creates a test windows form
$form = New-Object System.Windows.Forms.Form
# Form title
$form.Text = "sample"
# Create new LinkLabel
$linklabel = New-Object System.Windows.Forms.LinkLabel
# Set LinkLabel text
$linklabel.Text = $NewLabelText
# Defines text that should be a hyperlink
foreach ($URL in $URLinfo)
{
$linklabel.Links.Add($URL.StartPos, $URL.LinkLength, $URL.URL)
# Attempted to navigate to defined Web page when Specific link is click.
# Errors out. Add_Click is not a method of Links.
#$linklabel.Links.add_Click({[system.Diagnostics.Process]::start($URL.URL)})
}
# Customizing linklable size
$linklabel.AutoSize = $true
# Add linklabel to form
$form.Controls.Add($linklabel)
# Customizing form size
$form.AutoSize = $true
# The add_click method here works.
# Makes the entire Linklabel clickable including the non-highlighted parts
#$linklabel.add_Click({[system.Diagnostics.Process]::start($URLinfo[0].URL)})
# Shows form
$form.ShowDialog()
The below Microsoft documents show a method that allows for users to have multiple hyperlinks in a single linklabel, however this appears to use event handlers and is specifically for C#. Not sure if I can translate the solution to Powershell.
I also found an interesting solution using XML, but I don't know enough about xml to modify the code found here.