ListObject.SelectionChange Event

Occurs when the selection changes inside the ListObject control.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel (in Microsoft.Office.Tools.Excel.dll)

Syntax

'Declaration
Event SelectionChange As DocEvents_SelectionChangeEventHandler
event DocEvents_SelectionChangeEventHandler SelectionChange

Remarks

This event is raised when any selected range inside the ListObject changes.

This event is not raised when focus moves from inside a ListObject control to outside of the ListObject control. It is raised when focus moves from outside a ListObject control to the ListObject control.

Examples

The following code example creates a ListObject and then creates an event handler for the SelectionChange event. To test the event handler, select a cell in the ListObject and then select a different cell in the ListObject.

This version is for a document-level customization.

WithEvents SelectionChangeList As Microsoft.Office.Tools.Excel.ListObject
    Private Sub ListObject_SelectionChange()
        SelectionChangeList = Me.Controls.AddListObject( _
            Me.Range("A1", "C4"), "SelectionChangeList")
    End Sub 


    Sub List1_SelectionChange(ByVal Target As _
        Microsoft.Office.Interop.Excel.Range) _
        Handles SelectionChangeList.SelectionChange
        MessageBox.Show("The selection in the list object has changed.")
    End Sub
private void ListObject_SelectionChange()
{
    Microsoft.Office.Tools.Excel.ListObject list1 =
        this.Controls.AddListObject(
        this.Range["A1", "C4"], "list1");
    list1.SelectionChange += new Microsoft.Office.Interop.
        Excel.DocEvents_SelectionChangeEventHandler(
        list1_SelectionChange);
}

void list1_SelectionChange(Microsoft.Office.Interop.Excel.Range Target)
{
    MessageBox.Show("The selection in the list object has changed.");
}

This version is for an application-level add-in.

WithEvents SelectionChangeList As ListObject
Private Sub ListObject_SelectionChange()
    Dim NativeWorksheet As Microsoft.Office.Interop.Excel.Worksheet =
        Me.Application.Worksheets(1)
    Dim vstoWorksheet As Microsoft.Office.Tools.Excel.Worksheet =
            Globals.Factory.GetVstoObject(NativeWorksheet)
    SelectionChangeList = vstoWorksheet.Controls.AddListObject( _
        vstoWorksheet.Range("A1", "C4"), "SelectionChangeList")
End Sub 


Sub List1_SelectionChange(ByVal Target As  _
    Microsoft.Office.Interop.Excel.Range) _
    Handles SelectionChangeList.SelectionChange
    System.Windows.Forms.MessageBox.Show( _
        "The selection in the list object has changed.")
End Sub
private void ListObject_SelectionChange()
{
    Worksheet vstoWorksheet =
            Globals.Factory.GetVstoObject(this.Application.ActiveWorkbook.Worksheets[1]); ;
    ListObject list1 =
        vstoWorksheet.Controls.AddListObject(
        vstoWorksheet.Range["A1", "C4"], "list1");
    list1.SelectionChange += new 
        Excel.DocEvents_SelectionChangeEventHandler(
        list1_SelectionChange);
}

void list1_SelectionChange(Excel.Range Target)
{
    System.Windows.Forms.MessageBox.Show("The selection in the list object has changed.");
}

.NET Framework Security

See Also

Reference

ListObject Interface

Microsoft.Office.Tools.Excel Namespace