Aracılığıyla paylaş


ListObject.SelectedIndex Property

Definition

Gets or sets the index of the row that is currently selected.

public:
 property int SelectedIndex { int get(); void set(int value); };
public int SelectedIndex { get; set; }
member this.SelectedIndex : int with get, set
Public Property SelectedIndex As Integer

Property Value

The index number of the currently selected row.

Examples

The following code example creates a ListObject on the current worksheet. It uses the SelectedIndex and AutoSelectRows properties to select the second row and to specify that the entire row is selected when the user clicks a row. The code example also creates an event handler for the SelectedIndexChanged event that displays a message box whenever a new row is selected.

This version is for a document-level customization.

private void ListObject_SelectedIndex()
{
    Microsoft.Office.Tools.Excel.ListObject list1 =
        this.Controls.AddListObject(this.Range["A1", "D4"],
        "list1");

    list1.AutoSelectRows = true;
    list1.SelectedIndex = 2;
    list1.SelectedIndexChanged +=
        new EventHandler(list1_SelectedIndexChanged);
}

void list1_SelectedIndexChanged(object sender, EventArgs e)
{
    MessageBox.Show("Selected index has changed.");
}
WithEvents SelectedIndexList As Microsoft.Office.Tools.Excel.ListObject
Private Sub ListObject_SelectedIndex()
    SelectedIndexList = _
        Me.Controls.AddListObject(Me.Range("A1", "D4"), _
        "SelectedIndexList")
SelectedIndexList.AutoSelectRows = True
    SelectedIndexList.SelectedIndex = 2
End Sub


Private Sub List1_SelectedIndexChanged(ByVal sender As Object, _
    ByVal e As EventArgs) Handles SelectedIndexList.SelectedIndexChanged
    MessageBox.Show("Selected index has changed.")
End Sub

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

private void ListObject_SelectedIndex()
{
    Worksheet vstoWorksheet = 
        Globals.Factory.GetVstoObject(this.Application.ActiveWorkbook.Worksheets[1]);
    ListObject list1 =
        vstoWorksheet.Controls.AddListObject(
        vstoWorksheet.Range["A1", "D4"],
        "list1");

    list1.AutoSelectRows = true;
    list1.SelectedIndex = 2;
    list1.SelectedIndexChanged +=
        new EventHandler(list1_SelectedIndexChanged);
}

void list1_SelectedIndexChanged(object sender, EventArgs e)
{
    System.Windows.Forms.MessageBox.Show("Selected index has changed.");
}
WithEvents SelectedIndexList As ListObject
Private Sub ListObject_SelectedIndex()

    Dim NativeWorksheet As Microsoft.Office.Interop.Excel.Worksheet =
        Me.Application.Worksheets(1)
    Dim vstoWorksheet As Microsoft.Office.Tools.Excel.Worksheet =
            Globals.Factory.GetVstoObject(NativeWorksheet)
    SelectedIndexList = _
        vstoWorksheet.Controls.AddListObject( _
        vstoWorksheet.Range("A1", "D4"), _
        "SelectedIndexList")
    SelectedIndexList.AutoSelectRows = True
    SelectedIndexList.SelectedIndex = 2
End Sub


Private Sub List1_SelectedIndexChanged(ByVal sender As Object, _
    ByVal e As EventArgs) Handles SelectedIndexList.SelectedIndexChanged
    System.Windows.Forms.MessageBox.Show("Selected index has changed.")
End Sub

Remarks

This property is 1-based, not 0-based. The first data row in a ListObject control has an index number of 1.

Applies to