Binding a VB.NET label.text object to an object

I want to have a label in the form whose value changes depending on the value of the class instance. It looks like I can bind the text value of the label to a dataSource object. When I try to do this, it does not seem to work.

Me.Label4.DataBindings.Add(New System.Windows.Forms.Binding("Text", Me.ItemInfoBindingSource, "ItemNumber", True, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged))

My itemInfoBindingSource:

Me.ItemInfoBindingSource.DataSource = GetType(CFP.ItemInfo)

and class definition:

Public Class ItemInfo
    Public Property ItemNumber As String = "rename"
    Public Property Description As String
    Public Property FileLocation As String
    Public Property CompileHistory As List(Of CompileHistory)
End Class

I think what I did is bind to a class, not an instance of a class. Thinking about it, I really want to associate a class instance with a shortcut ... How? Is it possible?

+4
source share
1 answer

, , , , . , BindingList, , String, PropertyChanged.

:

  • INotifyPropertyChanged
  • PropertyChanged
  • .

ItemNumber :

Public Class ItemInfo
    Implements System.ComponentModel.INotifyPropertyChanged

    Private _itemNumber As String = "rename"
    Public Property ItemNumber As String
        Get
            Return _itemNumber
        End Get
        Set(value As String)
            _itemNumber = value
            RaiseEvent PropertyChanged(Me, 
                New System.ComponentModel.PropertyChangedEventArgs("ItemNumber"))
        End Set
    End Property

    Public Event PropertyChanged(sender As Object, 
        e As System.ComponentModel.PropertyChangedEventArgs) _
        Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class

, Form.Load, ItemInfoBindingSource ItemInfo ItemNumber TextBox.TextChanged.

Private ItemInfoBindingSource As New ItemInfo

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Label1.DataBindings.Add("Text", Me.ItemInfoBindingSource, "ItemNumber")
End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) _
        Handles TextBox1.TextChanged

    ItemInfoBindingSource.ItemNumber = TextBox1.Text
End Sub

, , ItemNumber.Set , , . Text, .

+6

All Articles