共用方式為


ListObject.SelectionChange 事件 (2007 系統)

更新: 2008 年 7 月

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

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

語法

Public Event SelectionChange As DocEvents_SelectionChangeEventHandler

Dim instance As ListObject
Dim handler As DocEvents_SelectionChangeEventHandler

AddHandler instance.SelectionChange, handler
public 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 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.");
}

使用權限

請參閱

參考

ListObject 類別

ListObject 成員

Microsoft.Office.Tools.Excel 命名空間

變更記錄

日期

記錄

原因

2008 年 7 月

加入應用程式層級增益集的程式碼範例版本。

SP1 功能變更。