By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,905 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,905 IT Pros & Developers. It's quick & easy.

Binding problem restated - inconsistent behavior in databinding

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hi Marina,
There is something strange this message was not in the lanuage.vb group,
however I have placed an answer there.

Cor
Jul 21 '05 #2

P: n/a
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
///
Jul 21 '05 #3

P: n/a
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?

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:uZ****************@tk2msftngp13.phx.gbl...
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
///

Jul 21 '05 #4

P: n/a
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!

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:uZ****************@tk2msftngp13.phx.gbl...
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
///

Jul 21 '05 #5

P: n/a
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
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?

Jul 21 '05 #6

P: n/a
That was the change what I did as well so read my text again.
Jul 21 '05 #7

P: n/a
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
Jul 21 '05 #8

P: n/a
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

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:uP*************@tk2msftngp13.phx.gbl...
That was the change what I did as well so read my text again.

Jul 21 '05 #9

P: n/a
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.
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:OS**************@TK2MSFTNGP09.phx.gbl...
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

Jul 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.