Skip to content

Commit e23dc0c

Browse files
committed
add-HyperlinkClickedEvent-in-richtexteditor
1 parent a2324e4 commit e23dc0c

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

controls/richtexteditor/features/hyperlink.md

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ Dim info As New HyperlinkInfo() With {.NavigateUri = "http://www.telerik.com", .
5353
Me.radRichTextEditor1.InsertHyperlink(info, "RichTextBox demo")
5454

5555
````
56+
5657
{{endregion}}
5758

59+
5860
A link to a bookmark is inserted by specifying the bookmark's name as **NavigateUri** and setting the **IsAnchor** property to *true*:
5961

6062
{{source=..\SamplesCS\RichTextEditor\Features\HyperlinkCode.cs region=remove}}
@@ -81,7 +83,7 @@ You can also use the overloaded methods for inserting a hyperlink:
8183

8284
* public void __InsertHyperlink__(HyperlinkInfo hyperlinkInfo, StyleDefinition hyperlinkStyle) - create a hyperlink from the currently selected part of the document and change the style of the text to the style passed as second argument.
8385

84-
Removing a hyperlink (and keeping the part of the document that the hyperlink spanned) can be done by positioning the caret in the hyperlink and invoking.
86+
Removing a hyperlink (and keeping the part of the document that the hyperlink spanned) can be done by positioning the caret in the hyperlink and invoking:
8587

8688
{{source=..\SamplesCS\RichTextEditor\Features\HyperlinkCode.cs region=remove}}
8789
{{source=..\SamplesVB\RichTextEditor\Features\HyperlinkCode.vb region=remove}}
@@ -205,7 +207,7 @@ Next link
205207

206208
## Other Customization Options
207209

208-
__ToolTip__
210+
### ToolTip
209211

210212
By default, hyperlinks take a fixed string as a tool tip. The default format is:
211213

@@ -214,20 +216,67 @@ By default, hyperlinks take a fixed string as a tool tip. The default format is:
214216
215217
You have control over it using the __HyperlinkToolTipFormatString__ of **RadRichTextEditor**, which will set the format for all hyperlinks in the document.
216218

217-
__HyperlinkClicked__
219+
### HyperlinkClicked event
220+
221+
When a hyperlink is clicked, the __HyperlinkClicked__ event of __RadRichTextEditor__ is fired. The sender of the event is the document element, which you have clicked, e.g. a **Span**, an **Image**, **InlineUIContainer**, etc. The __HyperlinkClickedEventArgs__ provide the possibility either to cancel or replace the navigation action. This is helpful when you need to validate the clicked hyperlink and prevent it from navigating to an unsecure address or from starting a local process.
222+
223+
With the 2024 Q4 release, the default navigation behavior of the hyperlinks is to automatically open only valid and trusted addresses. The hyperlink navigation can be cancelled by either setting the __Handled__ property of the HyperlinkClickedEventArgs to _true_ or __IsTrustedUrl__ to _false_.
224+
225+
Here is an example of using the HyperlinkClicked event prompting that the clicked hyperlink might be unsafe and allows to cancel the navigation process upon receiving the end user confirmation:
226+
227+
{{source=..\SamplesCS\RichTextEditor\Features\HyperlinkCode.cs region=HyperlinkClickedEvent}}
228+
{{source=..\SamplesVB\RichTextEditor\Features\HyperlinkCode.vb region=HyperlinkClickedEvent}}
229+
230+
````C#
231+
void radRichTextEditor1_HyperlinkClicked(object sender, HyperlinkClickedEventArgs e)
232+
{
233+
var link = e.URL;
234+
if (link.EndsWith("exe"))
235+
{
236+
e.Handled = true; MessageBoxResult Result = System.Windows.MessageBox.Show("You are about to open an executable file. Do you want to proceed", "Possible unsafe link", MessageBoxButton.YesNo, MessageBoxImage.Question);
237+
if (Result == MessageBoxResult.Yes)
238+
{
239+
Process.Start(new ProcessStartInfo()
240+
{
241+
FileName = link,
242+
UseShellExecute = true
243+
});
244+
}
245+
}
246+
}
247+
248+
249+
````
250+
````VB.NET
251+
Private Sub radRichTextEditor1_HyperlinkClicked(ByVal sender As Object, ByVal e As HyperlinkClickedEventArgs)
252+
Dim link = e.URL
253+
254+
If link.EndsWith("exe") Then
255+
e.Handled = True
256+
Dim Result As MessageBoxResult = System.Windows.MessageBox.Show("You are about to open an executable file. Do you want to proceed", "Possible unsafe link", MessageBoxButton.YesNo, MessageBoxImage.Question)
257+
If Result = MessageBoxResult.Yes Then
258+
Process.Start(New ProcessStartInfo() With {
259+
.FileName = link,
260+
.UseShellExecute = True
261+
})
262+
End If
263+
End If
264+
End Sub
218265

219-
When you click on a hyperlink, the __HyperlinkClicked__ event of __RadRichTextEditor__ is fired. The sender of the event is the document element, which you have clicked, e.g. a **Span**, an **Image**, **InlineUIContainer**, etc. The event args on the other hand, provide the possibility to mark the event as handled and prevent the default action. Custom logic can also be implemented depending on the __HyperlinkTarget__ and __URL__, which are also visible as properties of the event args.
220266

221-
![WinForms RadRichTextEditor Hyperlink Click Even Handler](images/richtexteditor-features-hyperlink002.png)
267+
````
268+
269+
{{endregion}}
270+
222271

223-
## HyperlinkNavigationMode
272+
### HyperlinkNavigationMode
224273

225-
This property allows you to control what action should trigger the opening of a hyperlink. The possible options are:
274+
The __HyperlinkNavigationMode__ allows you to control what action should trigger the opening of a hyperlink. The possible options are:
226275

227276
* **CtrlClick**: Triggers the hyperlink when users hold the Ctrl key and click on the hyperlink.
228277
* **Click**: Triggers the hyperlink when users click on the hyperlink.
229278

230-
#### Change the default hyperlink navigation mode
279+
Below is demonstrated how to change the default hyperlink navigation mode:
231280

232281
{{source=..\SamplesCS\RichTextEditor\Features\HyperlinkCode.cs region=HyperlinkMode}}
233282
{{source=..\SamplesVB\RichTextEditor\Features\HyperlinkCode.vb region=HyperlinkMode}}
Binary file not shown.

0 commit comments

Comments
 (0)