DataBinding a TextBox to an Object

D

Dustin Davis

I've been having problems with databinding. I've created a simple
solution to emulate the problem I can't figure out. Basically, I have a
TextBox bound to a property in an object. When the property is updated,
how do I get the field to show the new value. I assumed that databinding
would take care of it automatically.

Here is my source code. Can someone tell me what I would need to do to
get TextBox2 to update after the f.value is changed? (The form as one
button (Button1) and two TextBox fiels (TextBox1 & TextBox2)

Public Class Form1
Private f As New Field

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Set up binding
f.value = "test"
Me.TextBox2.DataBindings.Add("Text", f, "value", False,
DataSourceUpdateMode.OnPropertyChanged)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
f.value = Me.TextBox1.Text
End Sub
End Class

Public Class Field
Private sValue As String

Public Property value() As String
Get
Return sValue
End Get
Set(ByVal val As String)
Me.sValue = val
End Set
End Property
End Class

Thanks,
Dustin
 
D

Dustin Davis

Found a solution: http://www.15seconds.com/issue/040910.htm

Here is my updated code in case anyone cares:
Public Class Form1
Private f As New Field

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Set up binding
f.value = "test"
Me.TextBox2.DataBindings.Add("Text", f, "value")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
f.value = Me.TextBox1.Text
End Sub
End Class

Public Class Field
Private sValue As String
Dim bs As BindingSource

Public Event valueChanged As Eventhandler

Public Property value() As String
Get
Return sValue
End Get
Set(ByVal val As String)
Me.sValue = val
RaiseEvent valueChanged(Me, New EventArgs())
End Set
End Property
End Class
 
S

sloan

Private Sub SetLocalDataBindings()

If (Me.m_arg.ViewMode <> EventArgs.ViewMode.AddNew) Then

Dim titleIdBinding As Binding = _
txtTitleId.DataBindings.Add("Text", Me.m_model, "TitleId")

'Improved in .NET 2.0 //
http://msdn2.microsoft.com/library/y0h25we8(en-us,vs.80).aspx
'titleIdBinding.NullValue = "Type a TitleID Here"

AddHandler titleIdBinding.Format, AddressOf BindingNullCheck
''AddHandler titleIdBinding.Parse, AddressOf BindingNullCheck

'''Me.txtTitleId.DataBindings.Add(New Binding("Text",
Me.m_model, "TitleId"))

Me.txtPubId.DataBindings.Add(New Binding("Text", Me.m_model,
"CurrentPublisher.PublisherId"))
Me.txtTitle.DataBindings.Add(New Binding("Text", Me.m_model,
"Title"))
Me.txtPublisher.DataBindings.Add(New Binding("Text", Me.m_model,
"CurrentPublisher.PublisherName"))

Me.txtType.DataBindings.Add(New Binding("Text", Me.m_model,
"TitleType"))

Me.dtpPubDate.DataBindings.Add(New Binding("Value", Me.m_model,
"PublishedDate"))
Me.txtPubDate.DataBindings.Add(New Binding("Text", Me.m_model,
"PublishedDate"))

End If

If Not (Me.m_model.AllPublishers Is Nothing) Then
Me.cboPublisher.DataSource = Me.m_model.AllPublishers
Me.cboPublisher.DataBindings.Add(New Binding("SelectedItem",
Me.m_model, "CurrentPublisher"))
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top