Windows Dev Center

Expand Minimize

TextBox.GetRectFromCharacterIndex method

Returns a rectangular region for the leading or trailing edge of a character at a specific character index.

Syntax


public:
Rect GetRectFromCharacterIndex(
  int charIndex, 
  bool trailingEdge
)

Parameters

charIndex

Type: System.Int32 [.NET] | int32 [C++]

A zero-based index of the character for which to retrieve the rectangle.

trailingEdge

Type: System.Boolean [.NET] | Platform::Boolean [C++]

true to get the trailing edge; false to get the leading edge of the character.

Return value

Type: Rect

A rectangle for the edge of the character at the specified index.

Remarks

To override the context menu, you can handle the ContextMenuOpening event and replace the default menu with a custom menu. Use GetRectFromCharacterIndex to determine where to position the custom menu. For an example of this, see Scenario 2 of the ContextMenu sample. For design info, see Guidelines for context menus.

Because this method returns a rectangle that represents a character edge, the width of the rectangle that's returned is always 0. To get the width of a character, you must subtract the X value of the leading Rect from the X value of the trailing Rect.

Examples

This example shows how to use GetRectFromCharacterIndex to determine the rectangle for the selected text. For the complete example, see Scenario 2 of the ContextMenu sample.


// Returns a rect for selected text.
// If no text is selected, returns caret location.
// Text box should not be empty.
private Rect GetTextboxSelectionRect(TextBox textbox)
{
    Rect rectFirst, rectLast;
    if (textbox.SelectionStart == textbox.Text.Length)
    {
        rectFirst = textbox.GetRectFromCharacterIndex(textbox.SelectionStart - 1, true);
    }
    else
    {
        rectFirst = textbox.GetRectFromCharacterIndex(textbox.SelectionStart, false);
    }

    int lastIndex = textbox.SelectionStart + textbox.SelectionLength;
    if (lastIndex == textbox.Text.Length)
    {
        rectLast = textbox.GetRectFromCharacterIndex(lastIndex - 1, true);
    }
    else
    {
        rectLast = textbox.GetRectFromCharacterIndex(lastIndex, false);
    }

    GeneralTransform buttonTransform = textbox.TransformToVisual(null);
    Point point = buttonTransform.TransformPoint(new Point());

    // Make sure that we return a valid rect if selection is on multiple lines
    // and end of the selection is to the left of the start of the selection.
    double x, y, dx, dy;
    y = point.Y + rectFirst.Top;
    dy = rectLast.Bottom - rectFirst.Top;
    if (rectLast.Right > rectFirst.Left)
    {
        x = point.X + rectFirst.Left;
        dx = rectLast.Right - rectFirst.Left;
    }
    else
    {
        x = point.X + rectLast.Right;
        dx = rectFirst.Left - rectLast.Right;
    }

    return new Rect(x, dx, y, dy);
}

Requirements (device family)

Device family

Universal

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0

Namespace

Windows.UI.Xaml.Controls
Windows::UI::Xaml::Controls [C++]

Metadata

Windows.Foundation.UniversalApiContract.winmd

Requirements (operating system)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml.Controls
Windows::UI::Xaml::Controls [C++]

Metadata

Windows.winmd

See also

TextBox

 

 

Show:
© 2015 Microsoft