|
|
Title | Bind a CurrencyManager to objects in a collection and use it for navigation in VB .NET |
Description | This example shows how to bind a CurrencyManager to objects in a collection and use it for navigation in VB .NET. |
Keywords | CurrencyManager, binding, data binding, VB.NET, DataBinding |
Categories | Database, VB.NET |
|
|
When the form loads, the program creates a collection of Person objects. (The Person class is nothing special. It simply provides three public properties: FirstName, LastName, and Title.) It then gets a CurrencyManager to manage the collection.
Next the program adds DataBindings to the form's First Name, Last Name, and Title TextBoxes, binding the collection to the controls' Text properties.
The code then creates a Binding object for the Title TextBox and adds event handlers for that object's Format and Parse events.
|
|
Private WithEvents m_CurrencyManager As CurrencyManager
Private m_People As Collection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles MyBase.Load
m_People = New Collection
m_People.Add(New Person("Rod", "Stephens", "Grunt"))
m_People.Add(New Person("Sergio", "Aragones", "Artist"))
m_People.Add(New Person("Terry", "Pratchett", "Author"))
m_CurrencyManager = CType(Me.BindingContext(m_People), _
CurrencyManager)
txtFirstName.DataBindings.Add("Text", m_People, _
"FirstName")
txtLastName.DataBindings.Add("Text", m_People, _
"LastName")
txtTitle.DataBindings.Add("Text", m_People, "Title")
Dim binding_object As Binding = _
txtTitle.DataBindings("Text")
AddHandler binding_object.Format, AddressOf DataFormat
AddHandler binding_object.Parse, AddressOf DataParse
m_CurrencyManager.Refresh()
UpdateControlState()
End Sub
|
|
The DataFormat event handler executes when the Title TextBox's Binding object receives a new value to display. That happens when the CurrencyManager navigates to a new record.
The event handler examines the new value and, if it is Grunt, changes the value to King and makes the TextBox's background color gold. If the value is not Grunt, then the program leaves its value alone and gives the TextBox a white background color.
|
|
Private Sub DataFormat(ByVal sender As Object, ByVal e As _
ConvertEventArgs)
If e.Value.Equals("Grunt") Then
e.Value = "King"
Dim binding_object As Binding = DirectCast(sender, _
Binding)
binding_object.Control.BackColor = Color.Gold
Else
Dim binding_object As Binding = DirectCast(sender, _
Binding)
binding_object.Control.BackColor = Color.White
End If
End Sub
|
|
The DataParse event handler executes when the Binding object needs to save a new value in the collection. This version changes the value Prince to Grunt. (These versions of DataFormat and DataParse aren't very useful, they just let you see how they work.)
|
|
Private Sub DataParse(ByVal sender As Object, ByVal e As _
ConvertEventArgs)
If e.Value.Equals("Prince") Then
e.Value = "Grunt"
End If
End Sub
|
|
The UpdateControlState subroutine uses the CurrencyManager to enable and disable the previous and next buttons, and to display the current record's number.
The previous and next buttons use the CurrencyManager to navigate through the collection.
The PositionChanged event handler calls UpdateControlState when the CurrencyManager moves through the collection.
Finally, the add and delete buttons use the CurrencyManager to add and remove Person objects from the collection. (Pretty slick that the CurrencyManager can create the objects automatically, huh?)
|
|
Private Sub UpdateControlState()
btnPrev.Enabled = m_CurrencyManager.Position > 0
btnNext.Enabled = m_CurrencyManager.Position < _
m_CurrencyManager.Count - 1
lblPosition.Text = m_CurrencyManager.Position + 1 & _
" of " & m_CurrencyManager.Count
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnPrev.Click
m_CurrencyManager.Position -= 1
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnNext.Click
m_CurrencyManager.Position += 1
End Sub
Private Sub m_CurrencyManager_PositionChanged(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles _
m_CurrencyManager.PositionChanged
UpdateControlState()
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnAdd.Click
m_CurrencyManager.List.Add(New Person("", "", ""))
m_CurrencyManager.Refresh()
m_CurrencyManager.Position = m_CurrencyManager.Count - 1
UpdateControlState()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDelete.Click
m_CurrencyManager.RemoveAt(m_CurrencyManager.Position)
m_CurrencyManager.Refresh()
UpdateControlState()
End Sub
|
|
|
|
|
|