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

    The C1RichTextBox supports hyperlinks, which provide a standard mechanism for implementing user interactivity. In some cases, you may want to go beyond that and provide additional, custom mouse interactions. For example, you may want to apply some custom formatting or show a context menu when the user clicks an element.

    To enable these scenarios, the C1RichTextBox exposes ElementTapped events and a C1RichTextBox.GetPositionFromPoint method.

    If all you need to know is the element that triggered the mouse event, you can get it from the source parameter in the event handler. If you need more detailed information (the specific word that was clicked within the element for example), then you need the C1RichTextBox.GetPositionFromPoint method. C1RichTextBox.GetPositionFromPoint takes a point in client coordinates and returns a C1TextPosition object that expresses the position in document coordinates.

    The C1TextPosition object has two main properties: Element and Offset. The Element property represents an element within the document; Offset is a character index (if the element is a C1Run) or the index of the child element at the given point.

    For example, the code below creates a C1RichTextBox and attaches a handler to the C1RichTextBox.RightTapped event:

    C#
    Copy Code
    public sealed partial class MainPage : Page
        {
            C1RichTextBox rtb;
            public MainPage()
            {
                this.InitializeComponent();
                // Create a C1RichTextBox and add it to the page
                rtb = new C1RichTextBox();
                LayoutRoot.Children.Add(rtb);
                // Attach event handler
                rtb.RightTapped += rtb_RightTapped;
            }
    

    If you wanted to toggle the C1TextElement.FontWeight value of a single word, then you would need to determine which character was clicked and expand the selection to the whole word. This is where the C1RichTextBox.GetPositionFromPoint method becomes necessary. Here is a version of the event handler that accomplishes that:

    C#
    Copy Code
    void rtb_RightTapped(object sender, RightTappedRoutedEventArgs e)
    {
       // Get position in control coordinates
           var pt = e.GetPosition(rtb);
       // Get text pointer at position
           var pointer = rtb.GetPositionFromPoint(pt);
       // Check that the pointer is pointing to a C1Run
           var run = pointer.Element as C1Run;
             if (run != null)
              {
       // Get the word within the C1Run
           var text = run.Text;
           var start = pointer.Offset;
           var end = pointer.Offset;
            while (start > 0 && char.IsLetterOrDigit(text, start - 1))
            start--;
           while (end < text.Length - 1 && char.IsLetterOrDigit(text, end + 1))
            end++;
       // Toggle the bold property for the run that was clicked
           var word = new C1TextRange(pointer.Element, start, end - start + 1);
            word.FontWeight = word.FontWeight.HasValue && word.FontWeight.Value.Weight == FontWeights.Bold.Weight
             ? FontWeights.Normal
             : FontWeights.Bold;
            }
         }
      }
    }
    

    Notice that the C1TextElement.FontWeight property returns a nullable value. If the range contains a mix of values for this attribute, the property returns null. The code used to toggle the C1TextElement.FontWeight property is the same we used earlier when implementing the formatting toolbar.

    The GetPositionFromPoint method allows you to get a C1TextPosition object from a point on the screen. The GetRectFromPosition method performs the reverse operation, returning a Rect that represents the screen position of a C1TextPosition object. This is useful in situations where you want to present a UI element near a specific portion of a document.