Page 1 of 1

Richtextbox with multiple URLs

Posted: Mon Nov 05, 2018 8:20 pm
by kerbob
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

Re: Richtextbox with multiple URLs

Posted: Tue Nov 06, 2018 8:39 am
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:

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.

Re: Richtextbox with multiple URLs

Posted: Tue Nov 06, 2018 8:50 am
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!
:)

Re: Richtextbox with multiple URLs

Posted: Tue Nov 06, 2018 8:57 am
by jvierra
Señor Max,

Necesitas leer los documentos en español.

Another method is:

Start-Process $_.LinkText

Re: Richtextbox with multiple URLs

Posted: Tue Nov 06, 2018 9:05 am
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.

:)

Re: Richtextbox with multiple URLs

Posted: Tue Nov 06, 2018 9:31 am
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.

Re: Richtextbox with multiple URLs

Posted: Tue Nov 06, 2018 11:40 am
by kerbob
Many thanks all! Problem solved!!!