?
Solved

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

Posted on 2014-01-05
2
Medium Priority
?
252 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 2000 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

764 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