INFO: Firing the Windows Forms CurrencyManager Events in Visual Basic .NET (311543)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework 1.1)
  • Microsoft ADO.NET (included with the .NET Framework) 1.0
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

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.

MORE INFORMATION

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.
  1. Create a new Visual Basic Windows Application project. Form1 is added to the project by default.
  2. Add three TextBox controls to Form1. Textbox1, Textbox2, and Textbox3 are added by default.
  3. Add five Button controls to Form1. Button1 through Button5 are added by default.
  4. Change the Button properties as follows:
    Button(Name)Text
    Button1cmdFirst<<
    Button2cmdPrevious<
    Button3cmdNext>
    Button4cmdLast>>
    Button5cmdCancelEditUndo

  5. 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
    					
  6. 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
    					
  7. 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("server=server;uid=login;pwd=password;database=northwind")
    ' Open the Employees table.
    Dim daCust As New SqlDataAdapter("Select * from Employees", con)
    ' Store the Employees into a local table.
    Dim ds As New DataSet()
    daCust.Fill(ds, "Employees")
    
    'Bind the TextBox controls to the fields of the Employees table.
    TextBox1.DataBindings.Add("Text", ds.Tables!Employees, "EmployeeID")
    TextBox2.DataBindings.Add("Text", ds.Tables!Employees, "FirstName")
    TextBox3.DataBindings.Add("Text", ds.Tables!Employees, "LastName")
    
    'Specify the CurrencyManager for the Employees table.
    myCurrencyManager = CType(Me.BindingContext(ds.Tables!Employees), CurrencyManager)
    					
  8. 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("CurrentChanged fired")
    End Sub
    
    Private Sub myCurrencyManager_ItemChanged(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.ItemChangedEventArgs) Handles myCurrencyManager.ItemChanged
            MessageBox.Show("ItemChanged fired")
    End Sub
    
    Private Sub myCurrencyManager_PositionChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles myCurrencyManager.PositionChanged
            MessageBox.Show("PositionChanged fired")
    End Sub
    					
  9. 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
    					
  10. Modify the connection string to use your SQL Server and log on to connect.
  11. Press the F5 key to build and run the project.
  12. Click the buttons to browse between the rows. This raises the CurrentChanged and PositionChanged events.
  13. 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.
  14. Modify a value, and then click Undo to cancel the change. This raises the ItemChanged event.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

308052 HOW TO: Display Parent and Child Records in a DataGrid Using Windows Forms by Using Visual Basic .NET

For more information, see the following topics in Visual Studio .NET Help documentation: back to the top

Modification Type:MinorLast Reviewed:11/18/2005
Keywords:kbDatabase kbDataBinding kbinfo KB311543