Skip to content

Commit 25db98d

Browse files
authored
Merge pull request #812 from telerik/new-kb-chat-intercept-html-links-7e401019300049c98e7d100d84e56d3c
Added new kb article chat-intercept-html-links
2 parents 7e3e4c7 + 85d47bd commit 25db98d

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
title: Intercepting HTML Link Click in RadChat Control
3+
description: Learn how to intercept when HTML links are clicked in the RadChat control in UI for WinForms
4+
type: how-to
5+
page_title: Intercepting HTML Link Click in RadChat Control
6+
meta_title: Intercepting HTML Link Click in RadChat Control
7+
slug: chat-intercept-html-links
8+
tags: chat, radchat, link interception, html links, click events
9+
res_type: kb
10+
ticketid: 1701502
11+
---
12+
13+
|Product Version|Product|Author|
14+
|----|----|----|
15+
|2025.3.812|RadChat for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)|
16+
17+
## Description
18+
19+
At the moment of writing this Knowledge Base article, the RadChat control does not expose such an event that will be triggered when a hyperlink is clicked. We can use a custom code to catch the moment when the user clicks a link before it is actually opened in the browser.
20+
21+
## Solution
22+
23+
To intercept HTML links in RadChat, subclass the control and override its `OnMouseDown` method. Check if the clicked element is a hyperlink, then handle it accordingly.
24+
25+
### Steps to Implement
26+
1. Create a custom class inheriting from `RadChat`.
27+
2. Override the `OnMouseDown` method to detect clicked links.
28+
3. Use `ElementTree.GetElementAtPoint` to identify the element under the mouse.
29+
4. Implement a helper method to verify if the mouse is over a link.
30+
5. Replace the default RadChat control with the customized one in your `Form.Designer.cs` file.
31+
32+
### Example Code
33+
Below is the complete implementation:
34+
35+
````C#
36+
37+
public class MyRadChat : RadChat
38+
{
39+
protected override void OnMouseDown(MouseEventArgs e)
40+
{
41+
var element = this.ElementTree.GetElementAtPoint(e.Location);
42+
if (element != null && element is ChatMessageBubbleElement chatMessageBubbleElement)
43+
{
44+
string[] numLines = chatMessageBubbleElement.Text.Split('\n');
45+
46+
FormattedText formattedTextLink = IsMouseOverBlock(element, e, chatMessageBubbleElement.TextBlock.Lines);
47+
if (formattedTextLink != null)
48+
{
49+
var link = formattedTextLink.Link;
50+
return;
51+
}
52+
else
53+
{
54+
base.OnMouseDown(e);
55+
}
56+
}
57+
base.OnMouseDown(e);
58+
}
59+
60+
FormattedText IsMouseOverBlock(object sender, MouseEventArgs e, List<TextLine> lines)
61+
{
62+
RadElement element = sender as RadElement;
63+
Debug.Assert(element != null, "Element is not RadElement");
64+
Point elementAtPoint = element.PointFromControl(e.Location);
65+
int linesCount = lines.Count();
66+
for (int i = 0; i < linesCount; ++i)
67+
{
68+
TextLine textLine = lines[i];
69+
int textLineCount = textLine.List.Count;
70+
for (int j = 0; j < textLineCount; ++j)
71+
{
72+
FormattedText formattedText = textLine.List[j];
73+
if (!string.IsNullOrEmpty(formattedText.Link) && formattedText.DrawingRectangle.Contains(elementAtPoint))
74+
{
75+
return formattedText;//found link under mouse
76+
}
77+
}
78+
}
79+
80+
return null;//notfound
81+
}
82+
public override string ThemeClassName
83+
{
84+
get
85+
{
86+
return typeof(RadChat).FullName;
87+
}
88+
}
89+
}
90+
91+
````
92+
93+
With this subclass, you can intercept links and customize their behavior, such as downloading content or displaying it in a custom viewer.
94+
95+
## See Also
96+
97+
* [RadChat Overview](https://docs.telerik.com/devtools/winforms/controls/chat/overview)
98+
* [HTML-like Text Formatting](https://docs.telerik.com/devtools/winforms/telerik-presentation-framework/html-like-text-formatting)
99+

0 commit comments

Comments
 (0)