Microsoft KB Archive/311543

= INFO: Firing the Windows Forms CurrencyManager Events in Visual Basic .NET =

Article ID: 311543

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.1
 * Microsoft ADO.NET 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q311543



For a Microsoft Visual C# .NET version of this article, see 312045.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data
 * System.Data.SqlClient
 * System.Windows.Forms



SUMMARY
The CurrencyManager object allows you to synchronize bound controls as a user browses through rows in a table. For example, CurrencyManager allows you to display the correct FirstName and LastName properties in separate, bound TextBox controls as a user browses through the Employees table.

CurrencyManager has three events: CurrentChanged, ItemChanged, and PositionChanged. You can use these events to track when a user moves through rows and when a user edits values in bound controls. This article describes which actions cause the various CurrencyManager events to fire.



CurrentChanged Event
The CurrentChanged event of the CurrencyManager fires when the bound value of the current row in the CurrencyManager list of rows changes.

Specifically, the CurrentChanged event of the CurrencyManager fires in the following situations:
 * When the user changes position in the CurrencyManager list of rows so that a different value is in the bound control.
 * When the user changes the bound value in the current row of the CurrencyManager.
 * When the user adds a row in a position that is lower than the current position in the CurrencyManager list.
 * When the user deletes the current row in the CurrencyManager list.
 * When the user deletes a row that is located before the current position in the list.
 * When the user refreshes the list, such as by sorting.
 * When the user moves the current row in the CurrencyManager to another position in the list.
 * When the user moves a row to the current position in the list.
 * When the DataSource property of the CurrencyManager is changed.

ItemChanged Event
The ItemChanged event of the CurrencyManager fires when any item in any CurrencyManager row is edited.

Specifically, the ItemChanged event of the CurrencyManager fires in the following situations:
 * When the user calls the CancelCurrentEdit method of the CurrencyManager.
 * When the user adds a row to the CurrencyManager list.
 * When the user deletes any row from the CurrencyManager list.
 * When the user updates any row in the CurrencyManager list.
 * When the user refreshes the CurrencyManager list, such as by sorting.
 * When the DataSource property of the CurrencyManager is changed.

PositionChanged Event
The PositionChanged event fires when the Position property of the CurrencyManager list changes. The Position property is the current position in CurrencyManager list of rows.

Specifically, the PositionChanged event of the CurrencyManager fires in the following situations:
 * When the user moves to a different row, which implicitly changes the Position property of the CurrencyManager.
 * When the Position property of the CurrencyManager is changed programmatically.

Sample Code
This sample code displays message boxes when the CurrencyManager events are fired. The code uses the Employees table of the Microsoft SQL Server Northwind database.  Create a new Visual Basic Windows Application project. Form1 is added to the project by default. Add three TextBox controls to Form1. Textbox1, Textbox2, and Textbox3 are added by default. Add five Button controls to Form1. Button1 through Button5 are added by default. Change the Button properties as follows:

  Open the Code window for Form1. Paste the following code at the very top of the Code window, above Public Class Form1: Imports System.Data Imports System.Data.SqlClient   The CurrencyManager must be defined in form-level scope so that it is available in each button. Locate the following line of code: Inherits System.Windows.Forms.Form and paste the following code immediately after this code: Private WithEvents myCurrencyManager As CurrencyManager </li>  Add the following code to the Form1_Load event procedure. The code opens the Employees table and then binds the text boxes to the EmployeeID, FirstName, and LastName properties: ' Open a connection to the SQL Server Northwind database. ' Modify the following line to connect with your SQL Server and log on. Dim con As New SqlConnection(&quot;server=server;uid=login;pwd=password;database=northwind&quot;) ' Open the Employees table. Dim daCust As New SqlDataAdapter(&quot;Select * from Employees&quot;, con) ' Store the Employees into a local table. Dim ds As New DataSet daCust.Fill(ds, &quot;Employees&quot;)

'Bind the TextBox controls to the fields of the Employees table. TextBox1.DataBindings.Add(&quot;Text&quot;, ds.Tables!Employees, &quot;EmployeeID&quot;) TextBox2.DataBindings.Add(&quot;Text&quot;, ds.Tables!Employees, &quot;FirstName&quot;) TextBox3.DataBindings.Add(&quot;Text&quot;, ds.Tables!Employees, &quot;LastName&quot;)

'Specify the CurrencyManager for the Employees table. myCurrencyManager = CType(Me.BindingContext(ds.Tables!Employees), CurrencyManager) </li>  Paste the following code immediately after the Form1_Load event procedure. This code declares the CurrencyManager events and displays message boxes as each event fires.

You can also list these events by first selecting myCurrencyManager in the Code window's Object list and then selecting each event in the Code window's Member list. Private Sub myCurrencyManager_CurrentChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles myCurrencyManager.CurrentChanged MessageBox.Show(&quot;CurrentChanged fired&quot;) End Sub

Private Sub myCurrencyManager_ItemChanged(ByVal sender As Object, _ ByVal e As System.Windows.Forms.ItemChangedEventArgs) Handles myCurrencyManager.ItemChanged MessageBox.Show(&quot;ItemChanged fired&quot;) End Sub

Private Sub myCurrencyManager_PositionChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles myCurrencyManager.PositionChanged MessageBox.Show(&quot;PositionChanged fired&quot;) End Sub </li>  Paste the following code immediately after the PositionChanged event. This code allows the user to browse through the rows by clicking the command buttons. You set the Position property of the CurrencyManager to reposition to the new row: Private Sub cmdFirst_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdFirst.Click 'Move to the first row in the DataTable. myCurrencyManager.Position = 0 End Sub

Private Sub cmdPrevious_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdPrevious.Click 'If you are positioned past the first row, If myCurrencyManager.Position > 0 Then 'move back one row. myCurrencyManager.Position -= 1 End If End Sub

Private Sub cmdNext_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdNext.Click 'If you are positioned before the last row, If myCurrencyManager.Position < myCurrencyManager.Count Then 'move back one row. myCurrencyManager.Position += 1 End If End Sub

Private Sub cmdLast_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdLast.Click 'Position at the last row. myCurrencyManager.Position = myCurrencyManager.Count End Sub

Private Sub cmdCancelEdit_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdCancelEdit.Click 'Cancel the current editing in the bound controls. myCurrencyManager.CancelCurrentEdit End Sub </li> Modify the connection string to use your SQL Server and log on to connect.</li> Press the F5 key to build and run the project.</li> Click the buttons to browse between the rows. This raises the CurrentChanged and PositionChanged events.</li> Modify the values in either the FirstName or LastName text boxes, and then move to a different row. This raises the ItemChanged, CurrentChanged, and PositionChanged events.</li> Modify a value, and then click Undo to cancel the change. This raises the ItemChanged event.</li></ol>

<div class="references_section">