I did look at other posts involving creating HTML links in the RadLabel control but they all are navigating to some web page.
I don't know if this is possible but i wanted to "detect" which link is clicked in the RadLabel_Click event and then load some Winforms based on the link.
To make this simple lets say for example, the RadLabel has some HTML text with say 3 link tags.
RadLabel1.Text = "<h
tml
><
span
><
a
href
='
??'
>Link 1</
a
>,
<
a
href
='
??'
>Link 2</
a
>, <
a
href
='
??'
>Link 3</
a
>
</
span
></
html
>"
When user click on "link 1", i would show message box "Link 1" or load Form1, click on Link 2, show message box "Link 2" or load Form2 and so on. I put "??" in the href as am not sure what will go there yet.
Is this possible? I know it can be done with the LinkControl but i really like the HTML formatting available with a RadLabel.
Thanks
Don
Edit:
I do see a "Other" option in the hyperlink type option.(image attached)
Please could you tell me how to use this option? Can i use this option to load winforms?
Thanks..
8 Answers, 1 is accepted
Thank you for the question.
Our Html-like feature does not support this functionality out of the box and you can detect the link that the user have pressed after the press action.
Please refer to the code snippet which demonstrates how to detect the pressed link:
this
.radLabel1.MouseDown +=
new
System.Windows.Forms.MouseEventHandler(
this
.radLabel1_MouseDown);
private
FormattedText IsMouseOverBlock(FormattedTextBlock textBlock, MouseEventArgs e)
{
Point elementAtPoint =
this
.radLabel1.LabelElement.PointFromControl(e.Location);
int
linesCount = textBlock.Lines.Count;
for
(
int
i = 0; i < linesCount; ++i)
{
TextLine textLine = textBlock.Lines[i];
int
textLineCount = textLine.List.Count;
for
(
int
j = 0; j < textLineCount; ++j)
{
FormattedText formattedText = textLine.List[j];
if
(!
string
.IsNullOrEmpty(formattedText.Link) && formattedText.DrawingRectangle.Contains(elementAtPoint))
{
return
formattedText;
//found link under mouse
}
}
}
return
null
;
//notfound
}
private
void
radLabel1_MouseDown(
object
sender, MouseEventArgs e)
{
FieldInfo pi =
this
.radLabel1.LabelElement.Children[2].Children[1].GetType().GetField(
"textPrimitiveImpl"
, BindingFlags.Instance | BindingFlags.SetProperty |BindingFlags.NonPublic);
TextPrimitiveHtmlImpl text = (TextPrimitiveHtmlImpl)pi.GetValue(
this
.radLabel13.LabelElement.Children[2].Children[1]);
FormattedTextBlock textBlock = text.TextBlock;
FormattedText clickedLink = IsMouseOverBlock(textBlock, e);
if
(clickedLink !=
null
)
{
MessageBox.Show(clickedLink.Text +
" pressed"
);
}
}
I hope this helps. Regards,
Peter
the Telerik team

I can now use the HTML tags to create a "linkable" RadLabel.
Thanks again,
don

I know this is an old post, but I was just wondering if the approach described by Peter, still is the way to get this functionality?
Or is there by now, a simpler/more readalbe way to do this ?
/Thomas
Thank you for writing.
The approach, introduced by Peter for detecting which link is clicked in a HTML-like text formatted RadLabel is still the appropriate way to achieve this requirement. HTML-like Text Formatting is just an advanced text styling mechanism, which can be applied to all Telerik controls for WinForms and their elements and it does not support the mentioned functionality out of the box. If we have more requests for this we will consider it in the future improvement of the HTML-like Text Formatting functionality.
I hope this information helps. Should you have further questions, I would be glad to help.
Regards,
Desislava
Telerik

I would also like there to be some built-in way to do this. I can somewhat use the code above to detect clicks but it doesn't seem ideal. For example, I can't find a clean way to stop the built-in click behavior. I want to cancel the default action (of doing a Process.Start on the link text) and instead do something else. I've found that I can clear out the LinkText after the control has drawn and then the normal Process.Start won't be called. But then when I clear out the LinkText, then the cursor doesn't turn to a hand when I hover over the link.
Is there some way to be able to cancel the default action but still have everything behave normally? Or better yet, it would be great if this became standard functionality.
Thank you for writing.
The Html-like Text Formatting is basically an advanced styling mechanism which would allow you to easily format your labels using Html tags. As seen from the answers of my colleagues having multiple links in one label and differentiating them in a click event is not supported out of the box.
This functionality, however, can be accomplished. Considering your question, I am not completely sure that I understand it properly. Removing the link text in <a> tag results in an empty link hence the cursor does not turn into a hand. If you would like to override the default implementation of some events you can create a custom RadLabel control and override its input methods, e.g.:
public
partial
class
MyRadLabel : RadLabel
{
public
MyRadLabel()
{
InitializeComponent();
}
protected
override
void
OnMouseDown(MouseEventArgs e)
{
//base.OnMouseDown(e);
}
protected
override
void
OnClick(EventArgs e)
{
//base.OnClick(e);
}
}
Not calling the base implementation will prevent the built-in functionality from executing.
I hope this helps. Should you have further questions please do not hesitate to write back.
Regards,
Hristo Merdjanov
Telerik

Hi,
I tried to use the same way when Label is placed in Grid. But it doesn/t work
Could you please, look at the code
using
System;
using
System.Collections.Generic;
using
System.Drawing;
using
System.Windows.Forms;
using
Telerik.WinControls.Primitives;
using
Telerik.WinControls.TextPrimitiveUtils;
using
Telerik.WinControls.UI;
namespace
WindowsFormsApp7
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
var grid =
new
RadGridView()
{
Dock = DockStyle.Fill,
AutoGenerateColumns =
false
,
};
grid.AutoGenerateColumns =
false
;
grid.TableElement.RowHeight = 60;
grid.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
// grid.MasterTemplate.AllowAddNewRow = false;
Controls.Add(grid);
var ds =
new
List<Document>
{
new
Document()
{
Name =
"<html>Link: <a href='http://google.com'>Google</a></html>"
},
new
Document()
{
Name =
"<html>Link: <a href='http://microsoft.com'>Microsoft</a><br/>"
+
"Second Link: <a href='http://yahoo.com'>Yahoo</a></html>"
},
};
grid.DataSource = ds;
grid.Columns.Add(
new
GridViewTextBoxColumn(
"Name"
)
{
HeaderText =
"Name"
,
DataType =
typeof
(
string
),
ReadOnly =
true
,
Width = 300
});
grid.CellFormatting += (s, e) =>
{
if
(e.CellElement.RowInfo
is
GridViewFilteringRowInfo)
{
return
;
}
if
(e.CellElement.ColumnInfo.Name ==
"Name"
)
{
RadLabelElement label;
if
(e.CellElement.Children.Count == 0)
{
label =
new
RadLabelElement();
e.CellElement.Children.Add(label);
}
else
{
label = e.CellElement.Children[0]
as
RadLabelElement;
}
try
{
label.Text = ((GridDataCellElement) e.CellElement).Value.ToString();
}
catch
(Exception ex)
{
return
;
}
label.MouseDown += LabelOnMouseDown;
//e.CellElement.MouseDown += LabelOnMouseDown;
e.CellElement.DrawText =
false
;
}
};
}
private
void
LabelOnMouseDown(
object
sender, MouseEventArgs e)
{
var label = ((RadLabelElement) sender);
//var label = ((GridCellElement)sender).Children[0] as RadLabelElement;
if
(label ==
null
)
return
;
TextPrimitiveHtmlImpl text = label.LabelText.Impl
as
TextPrimitiveHtmlImpl;
FormattedTextBlock textBlock = text.TextBlock;
FormattedText clickedLink = IsMouseOverBlock(label, textBlock, e);
if
(clickedLink !=
null
)
{
MessageBox.Show(clickedLink.Text +
" pressed"
);
}
}
private
FormattedText IsMouseOverBlock(RadLabelElement label, FormattedTextBlock textBlock, MouseEventArgs e)
{
Point elementAtPoint = label.PointFromControl(e.Location);
int
linesCount = textBlock.Lines.Count;
for
(
int
i = 0; i <= linesCount - 1; i++)
{
TextLine textLine = textBlock.Lines[i];
int
textLineCount = textLine.List.Count;
for
(
int
j = 0; j <= textLineCount - 1; j++)
{
FormattedText formattedText = textLine.List[j];
if
(formattedText.DrawingRectangle.Contains(elementAtPoint))
{
//found link under mouse
return
formattedText;
}
}
}
return
null
;
}
}
public
class
Document
{
public
string
Name {
get
;
set
; }
}
}
Thank you for writing.
When it is placed inside a grid cell, the text is being centered. It is necessary to consider the vertical offset. Here is the modified IsMouseOverBlock method:
private
FormattedText IsMouseOverBlock(RadLabelElement label, FormattedTextBlock textBlock, MouseEventArgs e)
{
Point elementAtPoint = label.PointFromControl(e.Location);
int
linesCount = textBlock.Lines.Count;
TextPrimitiveHtmlImpl text = label.LabelText.Impl
as
TextPrimitiveHtmlImpl;
TextParams textParams =
new
TextParams();
textParams.alignment = ContentAlignment.TopLeft;
textParams.flipText = label.FlipText;
textParams.font = label.Font;
textParams.foreColor = label.ForeColor;
textParams.paintingRectangle =
new
RectangleF(PointF.Empty, label.Size);
textParams.rightToLeft = label.RightToLeft;
textParams.stretchHorizontally = label.StretchHorizontally;
textParams.text = label.Text;
textParams.textOrientation = label.TextOrientation;
textParams.textWrap = label.TextWrap;
textParams.useCompatibleTextRendering = label.UseCompatibleTextRendering;
textParams.useMnemonic = label.UseMnemonic;
textParams.enabled = label.Enabled;
SizeF size = text.MeasureOverride(
new
SizeF(
float
.PositiveInfinity,
float
.PositiveInfinity), textParams);
elementAtPoint.Offset(0, (
int
)(grid.TableElement.RowHeight- size.Height) / -2);
for
(
int
i = 0; i <= linesCount - 1; i++)
{
TextLine textLine = textBlock.Lines[i];
int
textLineCount = textLine.List.Count;
for
(
int
j = 0; j <= textLineCount - 1; j++)
{
FormattedText formattedText = textLine.List[j];
if
(formattedText.DrawingRectangle.Contains(elementAtPoint))
{
//found link under mouse
return
formattedText;
}
}
}
return
null
;
}
I hope this information helps. Should you have further questions I would be glad to help.
Regards,
Dess
Progress Telerik