Solved

Bound properties doesn't quite work in VB.net - What am I doing wrong?

Posted on 2014-01-05
2
251 Views
Last Modified: 2014-01-07
I have this form with 4 textfields and a button:
Public Class Form1
    Dim _DB As New DB
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        txtID.DataBindings.Add("Text", _DB, "ID")
        txtField1.DataBindings.Add("Text", _DB, "Field1")
        txtField2.DataBindings.Add("Text", _DB, "Field2")
        txtField3.DataBindings.Add("Text", _DB, "Field3")
    End Sub

    Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
        _DB.firstRec()
    End Sub

    Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
        _DB.prevRec()
    End Sub

    Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
        _DB.nextRec()
    End Sub

    Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
        _DB.lastRec()
    End Sub

    Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
        txtField1.Text = ""
        txtField2.Text = ""
        txtField3.Text = ""
        txtID.Text = "0"
        txtField1.Focus()
    End Sub
End Class

Open in new window

and a class with matching properties:
Public Class DB
    Dim _ID As Long
    Dim _Field1 As String = ""
    Dim _Field2 As String = ""
    Dim _Field3 As String = ""

    '====================================================
    ' PART 1 - Properties
    '----------------------------------------------------
    Public Property ID
        Get
            Return _ID
        End Get
        Set(value)
            _ID = value
        End Set
    End Property
    Public Property Field1
        Get
            Return _Field1
        End Get
        Set(value)
            _Field1 = value
        End Set
    End Property
    Public Property Field2
        Get
            Return _Field2
        End Get
        Set(value)
            _Field2 = value
        End Set
    End Property
    Public Property Field3
        Get
            Return _Field3
        End Get
        Set(value)
            _Field3 = value
        End Set
    End Property
End Class

Open in new window

If I enter some text in the text boxes and click the btnNew, the text boxes appeares to be cleared, but if I then click on one of the fields, the old text shows up again, so apparently the properties that were supposed to be bound hasn't been updated when I cleared the text I had entered in the text boxes, but still the properties updates the text boxes, so in that case the binding works. What am I missing?

Thx!
0
Comment
Question by:IverErling
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39757993
The problem probably originate from your methods, not from your properties.

What happens in NextRec and similar methods? If you create a New object in these, the binding is still to the New object you created at the form level, so you still see the values for that first object.

DataBinding can be tricky on your own classes. Personnally, when I decide to use DataBinding with something as what you do, which is database based from the name you give your members, I would rather work with a Collection and bind with that Collection, using a CurrencyManager object to take care of moving from one record to another.

And since this can also be tricky sometimes, since you are not aware of everything that is happening in the background, I usually do not use DataBinding at all. I most often disregard use my own methods to refresh the display on the screen. It does not take that much more code, but since I control everything, I do not end up with the surprises that DataBinding can bring.

Something like the following:
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
   SaveData()
    _DB.firstRec()
   DisplayData()
End Sub

Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
   SaveData()
    _DB.prevRec()
   DisplayData()
End Sub

........ (and so on).......

Private Sub SaveData
         _DB.Field1= txtField1.Text 
         _DB.Field2= txtField2.Text 
         _DB.Field3= txtField3.Text 
         _DB.ID= txtID.Text 
End Sub

Private Sub DisplayData
        txtField1.Text = _DB.Field1
        txtField2.Text = _DB.Field2
        txtField3.Text = _DB.Field3
        txtID.Text = _DB.ID
        txtField1.Focus()
End Sub

Open in new window

0
 

Author Comment

by:IverErling
ID: 39763065
Thanks! That sounds like good advice. I was simply trying to make a very clean and systematic sort of template project I can copy and expand on later to make sure everything is taken properly care of and to minimize the chance of errors and thought the idea of databinding sounded like a nice way to make it simple to take care of all the checking and formatting in the properties and have a super-simple form code.

I will do as you suggest for now. I agree that it doesn't involve much more code and should be easy enough to keep very structured still.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question