Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

609 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