G
Guest
Hello. I'm having trouble getting a combobox bound to a property of an
object. I am populating the combobox using data binding, but I can't
get the SelectedValue property to bind to an object; it never gets
updated. I've included my short sample below, it's from a form with just one
control, a combobox named "uxChoice". The line of code giving me grief
is: uxChoice.DataBindings.Add("SelectedValue", model, "SomeVariable")
Public Class Form1
Public Class anEntry
Public Sub New(ByVal objectId As Integer, ByVal description As
String)
_objectId = objectId
_description = description
End Sub
Private _objectId As Integer
Public ReadOnly Property ObjectId() As Integer
Get
Return _objectId
End Get
End Property
Private _description As String
Public ReadOnly Property Description() As String
Get
Return _description
End Get
End Property
End Class
Private listOfChoices As anEntry() = {New anEntry(1, "A"), New
anEntry(2, "B"), New anEntry(3, "C")}
Public Class SomeModel
Private _someVariable As Integer
Public Property SomeVariable() As Integer
Get
Return _someVariable
End Get
Set(ByVal value As Integer)
_someVariable = value
End Set
End Property
End Class
Private model As New SomeModel
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
uxChoice.DataSource = listOfChoices
uxChoice.DisplayMember = "Description"
uxChoice.ValueMember = "ObjectId"
uxChoice.DataBindings.Add("SelectedValue", model,
"SomeVariable")
End Sub
Private Sub uxChoice_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
uxChoice.SelectedIndexChanged
If uxChoice.SelectedValue IsNot Nothing Then
MsgBox(uxChoice.SelectedValue.ToString)
End If
MsgBox(model.SomeVariable.ToString)
End Sub
End Class
object. I am populating the combobox using data binding, but I can't
get the SelectedValue property to bind to an object; it never gets
updated. I've included my short sample below, it's from a form with just one
control, a combobox named "uxChoice". The line of code giving me grief
is: uxChoice.DataBindings.Add("SelectedValue", model, "SomeVariable")
Public Class Form1
Public Class anEntry
Public Sub New(ByVal objectId As Integer, ByVal description As
String)
_objectId = objectId
_description = description
End Sub
Private _objectId As Integer
Public ReadOnly Property ObjectId() As Integer
Get
Return _objectId
End Get
End Property
Private _description As String
Public ReadOnly Property Description() As String
Get
Return _description
End Get
End Property
End Class
Private listOfChoices As anEntry() = {New anEntry(1, "A"), New
anEntry(2, "B"), New anEntry(3, "C")}
Public Class SomeModel
Private _someVariable As Integer
Public Property SomeVariable() As Integer
Get
Return _someVariable
End Get
Set(ByVal value As Integer)
_someVariable = value
End Set
End Property
End Class
Private model As New SomeModel
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
uxChoice.DataSource = listOfChoices
uxChoice.DisplayMember = "Description"
uxChoice.ValueMember = "ObjectId"
uxChoice.DataBindings.Add("SelectedValue", model,
"SomeVariable")
End Sub
Private Sub uxChoice_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
uxChoice.SelectedIndexChanged
If uxChoice.SelectedValue IsNot Nothing Then
MsgBox(uxChoice.SelectedValue.ToString)
End If
MsgBox(model.SomeVariable.ToString)
End Sub
End Class