Databinding to custom class

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

I have a custom class with its properties bound to controls on a Form. If I
change the values of the controls, the changes are carried back to the
object. Is there any way to reverse this - so that programmatic changes
made to the object are reflected back to the form? I'm not making a
list-type object, so I don't want to have to use IBindingList.

Thanks,

Dave Hagedorn
 
You have to create ValueChanged events for each of your databound properties
For example

Public Class MyClass
private m_strMyText as string
private m_intMyInt as integer
public event MyTextChanged as EventHandler
public event MyIntChanged as EventHandler

Public Property MyText() as string
Get
return m_strMyText
End Get
Set(Value as String)
m_strMyText=Value
RaiseEvent MyTextChanged(me,New EventArgs)
End Set
End Property
Public Property MyInt() as Integer
Get
return m_intMyInt
End Get
Set(Value as Integer)
m_intMyInt =Value
RaiseEvent MyIntChanged(me,New EventArgs)
End Set
End Property
End Class

hope this helps
cheers,
Paul
 
Thanks for the help.

I'm still a little confused though - so, do I have to catch these events in
my form and act on them, or should the value of the bound control be updated
automatically?

I have something like this:

public class myclass
public event TextChanged as EventHandler

private pText as string

public property text as string
get
return pText
end get
set(byval value as string)
pText = value
end set
end property

public sub doStuff()
pText = "doing stuff"
RaiseEvent TextChanged(me,new EventArgs())
end sub
end class

Now if I call doStuff() from my form, with a TextBox bound to the text
property, nothing happens to the value in the TextBox.

Thanks again,

Dave Hagedorn
 
Hi Dave,
You don't have to handle these events yourself (unless you want to)
My understanding (and experience with) is that when you databind a property
to a control it automatically 'listens' for those events to update the
control when that property whenever that event is raised.
You will also want the RaiseEvent to be in the Property Set method (see
below)
I have had great success with this myself. Another option (although not as
nice) is to create a Typed Dataset from your class and then bind to the
dataset. I have done this as well, but it is more work and not as slick (in
my opinion)
Hope this helps.

cheers,
Paul

Joe said:
Thanks for the help.

I'm still a little confused though - so, do I have to catch these events in
my form and act on them, or should the value of the bound control be updated
automatically?

I have something like this:

public class myclass
public event TextChanged as EventHandler

private pText as string

public property text as string
get
return pText
end get
set(byval value as string)
pText = value

'**************************************************************
'this is where you raise the event
'**************************************************************
RaiseEvent TextChanged(me, new EventArgs())
 
Back
Top