RichTextBox for UWP | ComponentOne
Working with RichTextBox for UWP / Main Concepts and Features / Hyperlinks
In This Topic
    Hyperlinks
    In This Topic

    The C1RichTextBox supports hyperlinks. As in regular HTML documents, this feature allows you to make certain parts of the document active. When the user clicks them, the application receives a notification and takes some action.

    The code below shows how you can create a hyperlink:

    Visual Basic
    Copy Code
    Public Sub New()
        InitializeComponent()
        ' Set text
        rtb.Text = "This is some text with a hyperlink in it."
        ' Create hyperlink
        Dim pos As Integer = rtb.Text.IndexOf("hyperlink")
        rtb.[Select](pos, 9)
        Dim uri = New Uri("http://developer.mescius.com", UriKind.Absolute)
        rtb.Selection.MakeHyperlink(uri)
        ' Handle navigation requests
        rtb.NavigationMode = NavigationMode.Always
        AddHandler _rtb.RequestNavigate, AddressOf _rtb_RequestNavigate
    End Sub
    

    C#
    Copy Code
    public MainPage()
    {
      InitializeComponent();
      // Set text
      rtb.Text = "This is some text with a hyperlink in it.";
      // Create hyperlink
      int pos = rtb.Text.IndexOf("hyperlink");
      rtb.Select(pos, 9);
      var uri = new Uri("http://developer.mescius.com", UriKind.Absolute);
      rtb.Selection.MakeHyperlink(uri);
      // Handle navigation requests
      rtb.NavigationMode = C1.Xaml.RichTextBox.NavigationMode.Always;
      rtb.RequestNavigate += rtb_RequestNavigate;
    }
    

    The code starts by assigning some text to the C1RichTextBox. Next, it selects the word "hyperlink" and calls the EditExtensions.MakeHyperlink method to make it a hyperlink. The parameter is a URI that is assigned to the new hyperlink's C1Hyperlink.NavigateUri property.

    Then, the code sets the C1RichTextBox.NavigationMode property to determine how the C1RichTextBox should handle the mouse over hyperlinks. The default behavior is moving the mouse over a hyperlink, and tapping fires the C1RichTextBox.RequestNavigate event. This allows users to edit the hyperlink text as they would edit regular text.

    The C1RichTextBox.RequestNavigate event handler is responsible for handling the hyperlink navigation.

    Note that hyperlink actions are not restricted to URI navigation. You could define a set of custom URI actions to be used as commands within your application. The custom URIs would be parsed and handled by the C1RichTextBox.RequestNavigate handler. For example, the code below uses hyperlinks to show message boxes:

    Visual Basic
    Copy Code
    Public Sub New()
        InitializeComponent()
        ' Set text
        rtb.Text = "This is some text with a hyperlink in it."
        ' Create hyperlink
        Dim pos As Integer = _rtb.Text.IndexOf("hyperlink")
        rtb.[Select](pos, 9)
        Dim uri = New Uri("msgbox:Thanks for clicking!")
        rtb.Selection.MakeHyperlink(uri)
        ' Handle navigation requests
        rtb.NavigationMode = NavigationMode.Always
        AddHandler rtb.RequestNavigate, AddressOf _rtb_RequestNavigate
    End Sub
    Private Sub rtb_RequestNavigate(sender As Object, e As RequestNavigateEventArgs)
        Dim uri As Uri = e.Hyperlink.NavigateUri
        If uri.Scheme = "msgbox" Then
            MessageBox.Show(uri.LocalPath)
        End If
    End Sub
    

    C#
    Copy Code
    public MainPage()
            {
                this.InitializeComponent();
                // Set text
                    rtb.Text = "This is some text with a hyperlink in it.";
                // Create hyperlink
                 int pos = rtb.Text.IndexOf("hyperlink");
                rtb.Select(pos, 9);
                var uri = new Uri("http://developer.mescius.com", UriKind.Absolute);
                rtb.Selection.MakeHyperlink(uri);
                // Handle navigation requests
                rtb.NavigationMode = C1.Xaml.RichTextBox.NavigationMode.Always;
                rtb.RequestNavigate += rtb_RequestNavigate;
            }
            private async void rtb_RequestNavigate(object sender, RequestNavigateEventArgs e)
            {
                var md = new MessageDialog("The document is requesting to navigate to " + e.Hyperlink.NavigateUri, "Navigate");
                md.Commands.Add(new UICommand("OK", (UICommandInvokedHandler) =>
                {
                    Windows.System.Launcher.LaunchUriAsync(e.Hyperlink.NavigateUri);
                }));
                md.Commands.Add(new UICommand("Cancel", (UICommandInvokedHandler) =>
                {
                    rtb.Select(e.Hyperlink.ContentStart.TextOffset, e.Hyperlink.ContentRange.Text.Length);
                }));
                await md.ShowAsync();
            }
    

    The C1RichTextBox.RequestNavigate handler uses the URI members to parse the command and argument. You could use this technique to create documents with embedded menus for example.

    Note that the MakeHyperlink method is just a quick and easy way to turn an existing part of a document into a hyperlink. You can also create hyperlinks by adding C1Hyperlink elements to C1Document objects. This is described in later sections.