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.
Locked
User avatar
kerbob
Posts: 25
Joined: Sun Mar 03, 2013 12:39 pm

Richtextbox with multiple URLs

Post by kerbob » Mon Nov 05, 2018 8:20 pm

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
Joined: Thu Aug 18, 2011 4:56 am

Re: Richtextbox with multiple URLs

Post by davidc » Tue Nov 06, 2018 8:39 am

[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:

Code: Select all

$richtextbox1_LinkClicked=[System.Windows.Forms.LinkClickedEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.LinkClickedEventArgs]
#TODO: Place custom script here
Write-Host $_.LinkText
}
You must to handle the LinkClicked event in order to display the website.
David
SAPIEN Technologies, Inc.

User avatar
mxtrinidad
Site Admin
Posts: 303
Joined: Sun Mar 03, 2013 12:42 pm

Re: Richtextbox with multiple URLs

Post by mxtrinidad » Tue Nov 06, 2018 8:50 am

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!
:)

User avatar
jvierra
Posts: 13617
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Richtextbox with multiple URLs

Post by jvierra » Tue Nov 06, 2018 8:57 am

Señor Max,

Necesitas leer los documentos en español.

Another method is:

Start-Process $_.LinkText

User avatar
mxtrinidad
Site Admin
Posts: 303
Joined: Sun Mar 03, 2013 12:42 pm

Re: Richtextbox with multiple URLs

Post by mxtrinidad » Tue Nov 06, 2018 9:05 am

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.

:)

User avatar
jvierra
Posts: 13617
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Richtextbox with multiple URLs

Post by jvierra » Tue Nov 06, 2018 9:31 am

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
kerbob
Posts: 25
Joined: Sun Mar 03, 2013 12:39 pm

Re: Richtextbox with multiple URLs

Post by kerbob » Tue Nov 06, 2018 11:40 am

Many thanks all! Problem solved!!!

Locked