共用方式為


TextPattern.GetSelection 方法

定義

擷取與當前文本選擇相關的不相交文字範圍集合。

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()

傳回

一組不相交的文字範圍。

例外狀況

文字容器不支援文字選擇。

範例

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 屬性來測試控制項是否支援文字選擇或文字插入點的位置。

適用於

另請參閱