Share via


ListObject.SelectionChange Event (2007 System)

Occurs when the selection changes inside the ListObject control.

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

Syntax

'Declaration
Public Event SelectionChange As DocEvents_SelectionChangeEventHandler
'Usage
Dim instance As ListObject 
Dim handler As DocEvents_SelectionChangeEventHandler 

AddHandler instance.SelectionChange, handler
public event DocEvents_SelectionChangeEventHandler SelectionChange
public:
 event DocEvents_SelectionChangeEventHandler^ SelectionChange {
    void add (DocEvents_SelectionChangeEventHandler^ value);
    void remove (DocEvents_SelectionChangeEventHandler^ value);
}
JScript does not support events.

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 vstoWorksheet As Worksheet = CType( _
        Me.Application.ActiveWorkbook.Worksheets(1),  _
        Excel.Worksheet).GetVstoObject()
    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 = ((Excel.Worksheet)
        this.Application.ActiveWorkbook.Worksheets[1]).GetVstoObject();
    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 Class

ListObject Members

Microsoft.Office.Tools.Excel Namespace

Change History

Date

History

Reason

July 2008

Added a version of the code example for an application-level add-in.

SP1 feature change.