共用方式為


ListObject.SelectionChange 事件

當選取範圍在 ListObject 控制項內變更時發生。

命名空間:  Microsoft.Office.Tools.Excel
組件:  Microsoft.Office.Tools.Excel (在 Microsoft.Office.Tools.Excel.dll 中)

語法

'宣告
Event SelectionChange As DocEvents_SelectionChangeEventHandler
event DocEvents_SelectionChangeEventHandler SelectionChange

備註

ListObject 內已選取的範圍變更時會引發這個事件。

當焦點從 ListObject 控制項內移動至 ListObject 控制項之外時不會引發這個事件。 當焦點從 ListObject 控制項之外移動至 ListObject 控制項時會引發這個事件。

範例

下列程式碼範例會建立 ListObject,然後建立 SelectionChange 事件的事件處理常式。 若要測試這個事件處理常式,請選取 ListObject 中的儲存格,然後選取 ListObject 中不同的儲存格。

這是示範文件層級自訂的版本。

    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.");
}

這是示範應用程式層級增益集的版本。

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 安全性

請參閱

參考

ListObject 介面

Microsoft.Office.Tools.Excel 命名空間