Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone else who has had more caffeine than I please look at this example and tell me what I am doing wrong?

I have a custom business object called Customer and a Form1 with a BindingSource1, NumericUpDown1, TextBox1 and ComboBox1 and the attached code.

Setting break points in the Property Sets shows that the Set for the ComboBox never fires at all even though the Set for the NumericUpDown and TextBox fire like crazy.

I've obviously missed something and I know not what.

Thanks
VB
Option Explicit On
Option Strict On
Public Class Customer
Public Enum ChildGroup
Infant
Toddler
Preschool
GradeSchool
HighSchool
College
End Enum

Private m_Id As Int32
Private m_Name As String
Private m_Child As ChildGroup

Public Sub New(ByVal id As Int32, ByVal name As String, ByVal child As ChildGroup)
m_Id = id
m_Name = name
m_Child = child
End Sub

Public Property Id() As Int32
Get
Return m_Id
End Get
Set(ByVal value As Int32)
m_Id = value
End Set
End Property

Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property

Public Property Child() As ChildGroup
Get
Return m_Child
End Get
Set(ByVal value As ChildGroup)
m_Child = value
End Set
End Property
End Class

Option Explicit On
Option Strict On
Public Class Form1

Private m_Customer As New Customer(123, "John Doe", Customer.ChildGroup.HighSchool)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BindingSource1.DataSource = Customer
NumericUpDown1.Minimum = 0
NumericUpDown1.Maximum = 10000
NumericUpDown1.DataBindings.Add( _
New Windows.Forms.Binding("Value", BindingSource1, "Id", False, _ DataSourceUpdateMode.OnPropertyChanged))
TextBox1.DataBindings.Add( _
New Windows.Forms.Binding("Text", BindingSource1, "Name", False, _ DataSourceUpdateMode.OnPropertyChanged))
ComboBox1.DataBindings.Add( _
New Windows.Forms.Binding("Text", BindingSource1, "Child", False, _ DataSourceUpdateMode.OnPropertyChanged))
ComboBox1.Items.Add(Customer.ChildGroup.Infant)
ComboBox1.Items.Add(Customer.ChildGroup.Toddler)
ComboBox1.Items.Add(Customer.ChildGroup.Preschool)
ComboBox1.Items.Add(Customer.ChildGroup.GradeSchool)
ComboBox1.Items.Add(Customer.ChildGroup.HighSchool)
ComboBox1.Items.Add(Customer.ChildGroup.College)
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox1.DisplayMember = "Child"
ComboBox1.ValueMember = "Child"
End Sub

Public ReadOnly Property Customer() As Customer
Get
Return m_Customer
End Get
End Property
End Class
Posted
Updated 22-Apr-10 20:17pm
v2

1 solution

You can use the SelectedItem property instead of the Text property of the ComboBox when you are adding the data bindings. Then the setter will fire on the lost focus of the ComboBox.

VB
ComboBox1.DataBindings.Add( _
        New Windows.Forms.Binding("SelectedItem", BindingSource1, "Child", False, _
                                  DataSourceUpdateMode.OnPropertyChanged))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900