This documentation is archived and is not being maintained.

Window.Selection Property

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

Namespace: EnvDTE
Assembly: EnvDTE (in envdte.dll)

Object Selection { get; }
/** @property */
Object get_Selection ()

function get Selection () : Object

Property Value

An object representing the current selection on the Window object.

public void CodeExample(DTE2 dte, AddIn addin)
    {   // Before running, create a text file named 
        // "TextFile1.txt", include it in your solution,
        // and make it the active window.
        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.
            win.Activate(); // Activate the window
            // Show the name of the current ProjectItem in the window.
            // How many other windows are available?
            string w = "Windows:\n";
            foreach (Window wi in win.Collection)
                w = w + wi.Caption + "\n";
            // 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.
    catch(Exception ex)