Share via


TextPattern.GetSelection メソッド

定義

現在の 1 つ以上のテキスト選択に関連付けられている、非結合テキスト範囲のコレクションを取得します。

public:
 cli::array <System::Windows::Automation::Text::TextPatternRange ^> ^ GetSelection();
public System.Windows.Automation.Text.TextPatternRange[] GetSelection ();
member this.GetSelection : unit -> System.Windows.Automation.Text.TextPatternRange[]
Public Function GetSelection () As TextPatternRange()

戻り値

TextPatternRange[]

非結合テキスト範囲のコレクション。

例外

テキスト コンテナーはテキスト選択をサポートしていません。

private TextPatternRange CurrentSelection(AutomationElement target)
{
    // Specify the control type we're looking for, in this case 'Document'
    PropertyCondition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document);

    // target --> The root AutomationElement.
    AutomationElement textProvider = target.FindFirst(TreeScope.Descendants, cond);

    TextPattern textpatternPattern = textProvider.GetCurrentPattern(TextPattern.Pattern) as TextPattern;

    if (textpatternPattern == null)
    {
        Console.WriteLine("Root element does not contain a descendant that supports TextPattern.");
        return null;
    }
    TextPatternRange[] currentSelection = textpatternPattern.GetSelection();
    return currentSelection[0];
}
Private Function CurrentSelection(ByVal target As AutomationElement) As TextPatternRange
    ' Specify the control type we're looking for, in this case 'Document'
    Dim cond As PropertyCondition = New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document)

    ' target --> The root AutomationElement.
    Dim textProvider As AutomationElement = target.FindFirst(TreeScope.Descendants, cond)

    Dim textpatternPattern As TextPattern = CType(textProvider.GetCurrentPattern(TextPattern.Pattern), TextPattern)

    If (textpatternPattern Is Nothing) Then
        Console.WriteLine("Root element does not contain a descendant that supports TextPattern.")
        Return Nothing
    End If
    Dim currentTextSelection As TextPatternRange() = textpatternPattern.GetSelection()
    Return currentTextSelection(0)
End Function

注釈

テキストが選択されていない場合は、 GetSelection システム カーソル (テキスト挿入ポイント) の位置にある縮退テキスト範囲 (空の範囲) を返します。

システム カーソル (テキスト挿入ポイント) が存在しない場合は、 GetSelection 次を返す null可能性があります。

注意

このプロパティを SupportedTextSelection 使用して、テキスト挿入ポイントのテキストの選択または位置がコントロールでサポートされているかどうかをテストします。

適用対象

こちらもご覧ください