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
Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

792 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