Binding problem restated - inconsistent behavior in databinding

M

Marina

Here is the problem. If 2 different properties on the same (or different)
control are bound to the same data column, changing the Text property and
calling EndCurrentEdit discards the new value. Changing a custom property
and calling EndCurrentEdit accepts the new value, stores it in the datasoure
and behaves normally. Here is a reproduceable example:

First, extend Textbox:

Public Class MyTextBox
Inherits TextBox

Private myProp As String

Public Property MyProperty() As String
Get
Return myProp
End Get
Set(ByVal Value As String)
myProp = Value
End Set
End Property
End Class

Then, use this new Textbox on a form, and run this example.

You will notice, that calling just TestCustomProperty, updates the value in
the datasource and in what is displayed in the textbox.
Calling just TestTextProperty discards the value and does not update
anything.

What I want to understand is - why the behavior difference?

Note, I have experimented, and binding to the Text properties of 2 different
textboxes works fine.
This is only a problem in binding to the Text property and another property.
There is something about the Text property that seems to make it behave very
strangely when the column it is bound to, is also bound to another non-Text
property.

Here is the Form code:

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents MyTextBox1 As VisionControlTester.MyTextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.MyTextBox1 = New VisionControlTester.MyTextBox
Me.SuspendLayout()
'
'MyTextBox1
'
Me.MyTextBox1.Location = New System.Drawing.Point(80, 72)
Me.MyTextBox1.MyProperty = Nothing
Me.MyTextBox1.Name = "MyTextBox1"
Me.MyTextBox1.TabIndex = 0
Me.MyTextBox1.Text = "MyTextBox1"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.MyTextBox1)
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)

End Sub

#End Region
Dim dt As New DataTable

Private Sub Form2_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
dt.Columns.Add("MyColumn")
Dim row As DataRow = dt.NewRow
row(0) = "TestValue"
dt.Rows.Add(row)

MyTextBox1.DataBindings.Add("Text", dt, "MyColumn")
MyTextBox1.DataBindings.Add("MyProperty", dt, "MyColumn")

TestTextProperty()

TestCustomProperty()
End Sub

Private Sub TestTextProperty()
MyTextBox1.Text = "NewValue"
Me.BindingContext(dt).EndCurrentEdit()
End Sub

Private Sub TestCustomProperty()
MyTextBox1.MyProperty = "NewValue"
Me.BindingContext(dt).EndCurrentEdit()
End Sub
End Class
 
C

Cor Ligthert

Hi Marina,

Better is to stay in the same thread as you know, however see my message in
the original thread.

Cor
 
M

Marina

I posted a new thread, so that if people lost interest in the old one, they
would see the restated version.

You are right, your example does work.

The question is: why?

If I did the same thing, except had 2 textboxes, added a binding for each of
them for their Text property, and used just the datatable - all would work
great.

It only starts breaking when Text and non-Text properties are mixed - and I
want to know why. If it's 2 Text properties, everything is well. And if it
is a Text property and some other property - then changing the non-Text
property works as expected - it is only when doing the same with a Text
property that it breaks.

So why work one way and not the other (like in my original example)?

The issue being is that I'm working on something very generic that would
allow people to have controls on a form bound to any column - possibly some
columns being bound to by more then one control.

If I need to start creating a separate dataview for each time I add a
databinding, I can only imagine the performance hit, not to mention the
management issue.
 
C

Cor Ligthert

Hi Marina,

This you do not believe, I was thinking maybe is there something in the
logic that it keeps the value from the binding and uses that.

To test that I changed the code to this.

And you may tell me what happens here, however I think you can use it.

For me it is something the same as that text from the combobox where I want
to use the text part real as a textbox.

Cor

\\\
Dim dt As New DataTable
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
dt.Columns.Add("MyColumn")
Dim row As DataRow = dt.NewRow
row(0) = "TestValue"
dt.Rows.Add(row)
MyTextBox1.DataBindings.Add("MyProperty", dt, "MyColumn")
MyTextBox1.DataBindings.Add("Text", dt, "MyColumn")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MyTextBox1.Text = "TextValue"
Me.BindingContext(dt).EndCurrentEdit()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
MyTextBox1.MyProperty = "PropValue"
Me.BindingContext(dt).EndCurrentEdit()
End Sub
End Class
///
 
M

Marina

I'm not sure what you mean. Nothing happens when I run code like this -
clicking Button1 doesn't accept the value, just as it didn't in my previous
code. Clicking Button2 does accept the value. So the behavior is the same as
before.

Again, I am still confused as to why the matching of Text and non-Text
properties is causing the behavior. Also, why it is inconsistently behaving
this way - i.e. changing the non-Text property accepts the value - changing
the Text property does not. Why?
 
M

Marina

Here is another weird thing. If you reverse the order of the addition of
the bindings - then everything works perfectly! So add binding for
MyProperty first, then for Text

Both changing MyProperty or Text now start working as they should!

What is up with that?

Why does the order matter?
Is this a bug? It's hard to imagine someone designing this kind of behavior
on purpose!
 
C

Cor Ligthert

Hi Marina,

With me gives button 1 with this code textValue and button2
Propvalue and I can go on and on, are you sure you changed those two
bindings from place?.

Cor
 
M

Marina

Oh, I see, I didn't notice, sorry.

Does that not seem odd that bindings have to be done in some order? I mean,
one would think that the order wouldn't matter - or at least it shouldn't. I
would expect the same behavior no matter the order.

I mean, Text is just another property. And it's fine if the other properties
bound to that column are other Text properties belonging to different
controls.

Do you or anyone else know why the order matters? Is there any
documentation explaining this? Is this expected behavior? If it is expected,
there certainly should be documentation defining it - otherwise, how is one
supposed to know the order to add these bindings in?


Is there an explanation of
 
C

Cor Ligthert

Hi Marina,

What is posible is that the binding from the properties is in a sequential
way.

Something the same as when you do a classic byte move than you get this kind
of behaviour.

I do not know if you no that. You copy a byte over the next byte which copys
again with the new value. I don't know if it is still used but a classic way
to set all bytes to zero.

That can be the same behaviour as this. However just a gues.

Cor
 
M

Marina

I would accept that order matters, if reversing the order of the bindings
broke TestCustomProperty.

The problem is: it doesn't. If TestCustomProperty would stop working - at
least behavior would be consistent. But it works fine - just as
TestTextProperty begins working as it should.
 

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