This is a migrated thread and some comments may be shown as answers.

How to force RadRichTextBox to perform hyperlink recognition?

8 Answers 477 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
MURAT ERAYDIN
Top achievements
Rank 1
MURAT ERAYDIN asked on 11 Jun 2017, 01:10 PM

Background:

I have a RadRickTextBox with the following initializiton and I have set AutoInsertHyperlinks="True" in XAML.

private void loaded(object sender, RoutedEventArgs e)
{
    string data = "This is first line\n\n" + "\tUrl: http://www.google.com\n";
    this.radRichTextBox.Insert(data);
}

When I run the code google.com is not "hyperlinked". However, if I move cursor to end of hyperlink and press ENTER, hyperlink is recognized.

Question:

How can I force radRichTextBox to recognize hyperlinks when I insert a string? I have tried UpdateEditorLayout etc but no luck.

 

 

 

8 Answers, 1 is accepted

Sort by
0
Mihail
Telerik team
answered on 14 Jun 2017, 11:45 AM
Hello Murat,

The auto insert of a hyperlink is a functionality of the UI. Its intention is when the user is typing and inserts a hyperlink, the hyperlink to be recognized. However, when you insert a text in code behind this functionality won't work. The same behavior is when you paste a text that contains a hyperlink. The hyperlink won't be recognized. The same behavior can be observed in MS Word.

To overcome this obstacle you could parse the text and if there is a hyperlink to split the text and insert the chunks with the corresponding hyperlink between them. To insert a hyperlink you should use the InsertHyperlink method of RadRichTextBox
If you would like those operations to be inserted as one undo step you could use the BeginUndoGroup and EndUndoGroup methods of the RadRichTextBox.

I hope this information is helpful.

Regards,
Mihail
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
Danny
Top achievements
Rank 1
answered on 15 May 2018, 04:02 PM

Hi,

From your last explanation, I understood that a hyperlink can be added programmatically to radRichText using only InsertHyperlink. I did like this and everything is OK (means the hyperlink is located at the correct position, the text before and after the hyperlink is formatted how I wanted, but my question for you is how to format the hyperlink aspect than the default one (blue color, font Verdana, size whatever - too big for me ? (means, another font size, color, font family, etc).

What I have tried was to define a new style for the hyperlink and then to call inserthyperlink with a 3rd parameter - the style. I have obtained the error "the parameter Hyperlink Style is not used. The document hyperlink style will be used instead"...(see capt1). The code for defining the custom style is:

Dim hyperlinkstyle As New StyleDefinition
hyperlinkstyle.SpanProperties.FontFamily = New Telerik.WinControls.RichTextEditor.UI.FontFamily("Segoe UI")
hyperlinkstyle.SpanProperties.FontSize = Unit.PointToDip(10)
hyperlinkstyle.SpanProperties.ForeColor = Colors.Orange
hyperlinkstyle.DisplayName = "hyperlinkStyle"
hyperlinkstyle.Name = "hyperlinkStyle"

Thanks in advance for your support,

Dan

0
Danny
Top achievements
Rank 1
answered on 15 May 2018, 04:04 PM
sorry. Here the capture with the error
0
Tanya
Telerik team
answered on 18 May 2018, 10:26 AM
Hi Danny,

The InsertHyperlink() method with the third parameter for style is obsolete and indeed, this third parameter is not used. This is why you see the warning. You can directly modify the default Hyperlink style and it will be automatically applied for all hyperlinks. Example 8 from the Styles topic shows how this can be achieved for the Normal style - you can use the same approach for Hyperlink style.

Hope this is helpful.

Regards,
Tanya
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Danny
Top achievements
Rank 1
answered on 18 May 2018, 03:15 PM

Tanya - many thanks, it looks ok now.

If you don't mind, may I ask again your support? - thanks in advance.

I want that those inserted hyperlink(s) act as buttons (means I  want to add some code behind onClick event. Is that possible? How?

Again, many thanks.

Danny

 

0
Tanya
Telerik team
answered on 22 May 2018, 04:10 PM
Hello Danny,

When the user clicks a hyperlink inside the document, the HyperlinkClicked event is fired. You can attach to this event through the RadRichTextBox class:
this.radRichTextBox.HyperlinkClicked += RadRichTextBox_HyperlinkClicked;

Hope this helps.

Regards,
Tanya
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
Danny
Top achievements
Rank 1
answered on 24 May 2018, 06:19 PM

Hi Tanya,

Your suggestion is welcomed - one more question: because I want to use HyprlinkClicked event and to insert some code inside the procedure (open an excel file with some rows already selected), I need to disable the "normal" behavior of the hyperlink (means the HypelinkInfo with the parameters NavigateUri and Target to be somehow bypassed). How to do this? (I have attached to this post my short project in which I insert some text in RadRichTextEditor and at the end of each line a small InlineImage as Hyperlink. Clicking on the InlineImage I don't want to navigate to an Url, but to do something else - messagebox.show or whatever code)

Thanks in advance,

Danny

Imports System.IO
Imports Telerik.WinControls
Imports Telerik.WinForms.Documents
Imports Telerik.WinForms.Documents.Model
Public Class RadForm1
Private Sub InsertHyprlinkedImage()
Dim image As ImageInline = New ImageInline(New Uri("file:/d:\untitled-2.png"))
Me.RadRichTextEditor1.InsertInline(image)
Me.RadRichTextEditor1.Document.CaretPosition.MoveToPreviousInline()
Dim ImageInDoc = TryCast(Me.RadRichTextEditor1.Document.CaretPosition.GetCurrentInline(), ImageInline)
If ImageInDoc IsNot Nothing Then
Dim start As DocumentPosition = New DocumentPosition(Me.RadRichTextEditor1.Document)
Dim [end] As DocumentPosition = New DocumentPosition(Me.RadRichTextEditor1.Document)
start.MoveToStartOfDocumentElement(ImageInDoc)
[end].MoveToEndOfDocumentElement(ImageInDoc)
[end].MoveToNext()
Me.RadRichTextEditor1.Document.Selection.AddSelectionStart(start)
Me.RadRichTextEditor1.Document.Selection.AddSelectionEnd([end])
Dim hyperlinkInfo As HyperlinkInfo = New HyperlinkInfo() With {.NavigateUri = "http://www.telerik.com", .Target = HyperlinkTargets.Blank}
Me.RadRichTextEditor1.InsertHyperlink(hyperlinkInfo)
End If
End Sub
Private Sub RadForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click
RadRichTextEditor1.Insert("jgggjhjh kkhkhkhh")
InsertHyprlinkedImage()
RadRichTextEditor1.Insert(Environment.NewLine)
End Sub
Private Sub RadRichTextEditor1_HyperlinkClicked(sender As Object, e As HyperlinkClickedEventArgs) Handles RadRichTextEditor1.HyperlinkClicked
RadMessageBox.Show("Cdcsd")
End Sub
End Class

0
Tanya
Telerik team
answered on 29 May 2018, 11:34 AM
Hi Danny,

To suppress the default action of the hyperlink click, you can set the Handled property of the event's arguments to true.

Regards,
Tanya
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
Tags
RichTextBox
Asked by
MURAT ERAYDIN
Top achievements
Rank 1
Answers by
Mihail
Telerik team
Danny
Top achievements
Rank 1
Tanya
Telerik team
Share this question
or