Maryam
Maryam

Reputation: 63

VB.Net - Bind a TextBox to a collection's property

I have an Account class and Accountlist collection.

Public ReadOnly Property TotalAmount() As Decimal
    Get
        If ACCOUNTList.Count > 0 Then
            Return ACCOUNTList.Sum(Function(s) s.InvoiceAmount)
        Else
            Return 0
        End If            
    End Get
End Property

As it can be seen in code above I like to bind TotalAmount property which is in Accountlist collection to a textBox so that when BindingSource changes the textBox's value changes automatically. I can access collection Count in BindingSource but I have no idea how I can bind TotalAmount property to the BindingSource! How can I achieve this?

 Private Sub ACCOUNTBindingSource_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles ACCOUNTBindingSource.ListChanged
        txtBox1.Text = ACCOUNTBindingSource.Count
        txtBox2.Text = ACCOUNTBindingSource.???

    End Sub

Upvotes: 1

Views: 468

Answers (2)

Eldar Zeynalov
Eldar Zeynalov

Reputation: 409

You can change it to a Function () as :

Public Function TotalAmount() As Decimal
    If ACCOUNTList.Count > 0 Then
        Return ACCOUNTList.Sum(Function(s) s.InvoiceAmount)
    Else
        Return 0
    End If            
End Function 

So every time you use Textbox1.Text = [Some Class].TotalAmount().ToString() it processes and returns new summaried value

Upvotes: 1

the_lotus
the_lotus

Reputation: 12748

Your class will need to implement INotifyPropertyChanged. Then you just need to call the PropertyChanged event. There also a good little example here.

Upvotes: 1

Related Questions