Richtextbox with multiple URLs

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jpbobrek
Posts: 25
Last visit: Sat Dec 23, 2023 1:52 pm

Richtextbox with multiple URLs

Post by jpbobrek »

Product, version and build:
64 bit version of product: 5.5.155
Operating system: Win10x64

*** Please add details and screenshots as needed below. ***

I published a GUI that had a RichTextBox control with DetectUrls = True with a link inside of it like in the example below:

"If you would like more information, click this link: <https://example.com/info.html>"

It worked great. But now, I needed to add a couple of additional links as in the example below:

"If you would like more information, click this link: <https://example.com/info.html>
If read the policy, click here: <https://example.com/sure.html>
For further questions, click here: <https://example.com/notsure.html>"

It compiles fine and looks good.. but none of the links work. Am I doing something wrong or is each RichTextBox limited to finding one link?

Thanks,
Kerbob
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Richtextbox with multiple URLs

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]

I ran a quick test and the RichTextBox is returning the correct links in the LinkClicked event:
  1. $richtextbox1_LinkClicked=[System.Windows.Forms.LinkClickedEventHandler]{
  2. #Event Argument: $_ = [System.Windows.Forms.LinkClickedEventArgs]
  3.     #TODO: Place custom script here
  4.     Write-Host $_.LinkText
  5. }
You must to handle the LinkClicked event in order to display the website.
David
SAPIEN Technologies, Inc.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Richtextbox with multiple URLs

Post by mxtrinidad »

This is a good one!

Just keep in mind, when working with Windows Form controls, you're dealing with events. So, you need to add the event for the when you click the Url link.
This is the "LinkClicked" event.

This is well documented in the Microsoft Doc: https://docs.microsoft.com/es-es/dotnet/api/system.windows.forms.richtextbox.detecturls?redirectedfrom=MSDN&view=netframework-4.7.2

Now, to add the event to the form, just Right-Clicked on the RichTextBox control, then select "Add Events".
Select (Checked) the "LinkClicked" event, and this will be added to the script section of the control.

For example:

Code: Select all

$richtextbox1_LinkClicked=[System.Windows.Forms.LinkClickedEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.LinkClickedEventArgs]
	#TODO: Place custom script here

}
After the event code block has been added, the include the following code:

Code: Select all

	#.NET CSharp code => System.Diagnostics.Process.Start(e.LinkText);
	[System.Diagnostics.Process]::Start($_.LinkText)
Now, you can run the form with the RichTextBox showing any Urls that can the executed running the browser.

Hope this helps!
:)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Richtextbox with multiple URLs

Post by jvierra »

Señor Max,

Necesitas leer los documentos en español.

Another method is:

Start-Process $_.LinkText
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Richtextbox with multiple URLs

Post by mxtrinidad »

Mr Jvierra!

Oops! I thought when I did the translate to english that the Url will keep it in "English".
I wasn't expecting to stay in spanish language.

Haha!

Also, I was showing the code translation from C#.
Thanks for showing the "Start-Process" cmdlet.

:)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Richtextbox with multiple URLs

Post by jvierra »

Max,
Use either. I just posted the alternate as another choice. It may be easier to remember.

Also just change the url "es-es" to "en-US" and it will be in English again.
User avatar
jpbobrek
Posts: 25
Last visit: Sat Dec 23, 2023 1:52 pm

Re: Richtextbox with multiple URLs

Post by jpbobrek »

Many thanks all! Problem solved!!!
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked