List Object Control Events

Table 7.2 shows the events that are available for a ListObject control in VSTO.

Table 7.2. Events of the ListObject Control

Event

Description

BeforeAddDataBoundRow

Raised when a user attempts to add a new row to a data-bound ListObject. This event is not raised when a new row is programmatically added to the ListObject, only when the user interface is used to add the row.

BeforeDoubleClick

Raised when the ListObject control is double-clicked. The BeforeDoubleClick event handler of a ListObject control enables you to take action when a user double-clicks text within a list, before the default action occurs.

BeforeRightClick

Raised when the ListObject control is right-clicked. The BeforeRightClick event enables you to take action when a user right-clicks text within a list, before the default action occurs.

BindingContextChanged

Raised when the binding context of the ListObject changes. The BindingContextChanged event handler enables you to take action when the BindingContext property of a list is changed.

Change

Raised when the value of a ListObject changes either by user interaction with the ListObject or programmatically. This event is not raised when a recalculation is performed on the ListObject.

Table 7.2. Events of the ListObject Control (Continued)

Event

Description

DataBindingFailure

Raised when the data binding of a ListObject fails. For example, it is raised if you attempt to drag a data-bound ListObject from the Data Sources window to an area on the worksheet that contains an existing ListObject.

DataMemberChanged

Raised when the value of the DataMemberChange property changes either programmatically or through user action.

DataSourceChanged

Raised when the DataSource property value of the ListObject is changed.

Deselected

Raised when the selection moves from an area inside the ListObject to an area outside the ListObject.

ErrorAddDataBoundRow

Raised when an error occurs when the user tries to add a row to a data-bound ListObject. For example, the error might occur due to a validation violation, such as adding non-unique values to the ListObject.

OriginalDataRestored

Raised when the changed value of the ListObject is restored to its original value when the change causes an error.

Disposed

Raised when the ListObject control is disposed of. This event typically does not get raised until garbage collection is called to reclaim the memory that held the control.

Selected

Raised when the selection moves inside an area of the ListObject from outside the ListObject.

SelectedIndexChanged

Raised when the selected index changes. The selected index is the ordinal position of the currently selected item in the list.

SelectionChange

Raised when the selection within the ListObject changes.

In this section, you'll learn how to use various events of the ListObject. To make it easy for you to test the code, we start with a code example in Listing 7.27 that creates a data-bound list object. This example can be used with each of the ListObject event examples. The example creates a list object named Listl that is bound to a data table called RealtorShowingTable. Users can schedule real estate appointments by adding the MLS number for the property to be viewed, the agent who is showing the property, and the name of the agent's company. The variable for the list object is created "WithEvents" so that you can easily add the event handler for each example.

Listing 7.27. Creating a data-bound ListObject Dim RealtorShowingTable As DataTable

WithEvents List1 As Microsoft.Office.Tools.Excel.ListObject

Private Sub Sheet1_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup

RealtorShowingTable = New DataTable("RealtorShowing")

Dim DateColumn As DataColumn = _

RealtorShowingTable.Columns.Add("DateScheduled", _ GetType(Date))

DateColumn.AllowDBNull = False

With RealtorShowingTable.Columns

.Add("MLS Number", GetType(String)) .Add("Realtor Name", GetType(String)) .Add("Agency", GetType(String))

End With

RealtorShowingTable.Rows.Add("03-05-2007", "123456", _ "Mike Hernandez", "Mike's Realty")

RealtorShowingTable.Rows.Add("03-06-2007", "654321", _ "Mike Hernandez", "Mike's Realty")

List1 = Me.Controls.AddListObject(Me.Range("A1"), "List1")

List1.AutoSetDataBoundColumnHeaders = True List1.SetDataBinding(RealtorShowingTable)

End Sub

0 0

Post a comment