Solved

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

Posted on 2014-01-05
2
243 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
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now