Partager via


Window2.Selection Property

Definition

Gets an object representing the current selection on the Window object.

public:
 property System::Object ^ Selection { System::Object ^ get(); };
public:
 property Platform::Object ^ Selection { Platform::Object ^ get(); };
[System.Runtime.InteropServices.DispId(129)]
public object Selection { [System.Runtime.InteropServices.DispId(129)] get; }
[<System.Runtime.InteropServices.DispId(129)>]
[<get: System.Runtime.InteropServices.DispId(129)>]
member this.Selection : obj
Public ReadOnly Property Selection As Object

Property Value

An object.

Implements

Attributes

Examples

This example makes a text file the active window. It then uses it to iterate through the windows collection and to display the name of the containing project and project item for the window.

Before running this example, open a project in Visual Studio and add a text file named "TextFile1.txt" to it. Add some text to the file and select it. The example displays the selected text in a message box.

Imports EnvDTE  
Imports EnvDTE80  
Sub SelectionExample (ByVal dte As DTE2)  
    Try  
        'Before running, create a text file named   
        ' "TextFile1.txt", include it in your solution,  
        ' and make it the active window. Write some text in the file  
        ' and select it.  
        Dim win As Window2  
        Dim doc As Document  
        If _applicationObject.Documents.Count > 0 Then  
            doc = _applicationObject.Documents.Item("TextFile1.txt")  
            win = CType(doc.ActiveWindow, Window2)  
            ' Show the name of the project that contains this  
            ' window and document.  
            MsgBox("The name of the project containing the window: " _  
            & win.Project.Name)  
            ' Acticate the winodw.  
            win.Activate()  
            ' Show the name of the current ProjectITem.  
            MsgBox("The name of the current project item is: "  _  
            & win.ProjectItem.Name)  
            ' How many other windows are available?  
            Dim w As String = "Available windows are :" & vbCr  
            For Each wi As Window2 In win.Collection  
                w = w & wi.Caption & vbCr  
             Next  
             MsgBox(w)  
             ' Show the selected text.  
             MsgBox(CType(win.Selection, TextSelection).Text _  
             & " is selected.")  
            ' Determine that the document returned by the   
            ' document property  
            ' is the same as the document object 'doc'.  
            If win.Document Is doc Then  
                MsgBox("The documents match!")  
            Else  
                MsgBox("The documents do not match.")  
            End If  
            ' Close the window.  
            win.Close(vsSaveChanges.vsSaveChangesNo)  
        End If  
    Catch ex As Exception  
        MsgBox(ex.Message)  
    End Try  
End Sub  
using EnvDTE;  
using EnvDTE80;  
using System.Windows.Forms;  
public void SelectionExample(DTE2 dte)  
{  
    try  
    {  
        // Before running, create a text file named   
        // "TextFile1.txt", include it in your solution,  
        // and make it the active window. Write some text in the file  
        // and select it.  
        Window win;  
        Document doc;  
        if (dte.Documents.Count > 0)  
        {  
            doc = dte.Documents.Item("TextFile1.txt");  
            win = doc.ActiveWindow;  
            // Show the name of the project that contains this window   
            // and document.  
            MessageBox.Show("The name of the containing project is : "  
 + win.Project.Name);  
            win.Activate(); // Activate the window  
            // Show the name of the current ProjectItem in the window.  
            MessageBox.Show("The name of the project item is: "   
+ win.ProjectItem.Name);  
            // How many other windows are available?  
            string w = "Available windows:\n";  
            foreach (Window wi in win.Collection)  
            {  
                w = w + wi.Caption + "\n";  
            }  
            MessageBox.Show(w);  
            // Show the selected text.  
            MessageBox.Show(((TextSelection)win.Selection).Text   
+ " is selected.");  
            // Determine that the document returned by the document   
            // property is the same as the document object 'doc'.  
            if (win.Document.Equals(doc))  
                MessageBox.Show("The documents match!");  
            Else  
                MessageBox.Show("The documents do not match!");  
            // Close the window.  
            win.Close(vsSaveChanges.vsSaveChangesNo);  
        }  
    }  
    catch (Exception ex)  
    {  
        MessageBox.Show(ex.Message);  
    }  
}  

Applies to