Databinding 2 properties from 2 bindingsources to one textbox?

Hobbyist_programmer 621 Reputation points
2021-08-18T19:56:36.277+00:00

Hallo,

Is there a way to combine two string properties from 2 different binding sources to a text box and keep updating?. lets say i have First name in one binding source and last name in other binding source and i want to bind these two properties together to one text box without using additional properties to combine two?.

Thanks

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,596 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Hobbyist_programmer 621 Reputation points
    2021-08-19T19:08:56.84+00:00

    Hallo

    I have 2 class

    Public FirstNames as New List ( of Name1)
    Public LastNames as New List ( of Name2)

    Public BS_Name1 as New Bindingsource
    Public BS_Name2 as New Bindingsource

    On form1.load

    BS_Name1.Datasource = FirstNames
    BS_Name2.Datasource = LastNames

    Public Class Name1

    Public Property FirstName as String

    End Class

    Public Class Name2

    Public Property LastName as String

    End Class

    Now lets say i want to combine FirstName & LastName in to one textbox ?

    0 comments No comments

  2. Xingyu Zhao-MSFT 5,356 Reputation points
    2021-08-20T02:08:08.033+00:00

    Hi @Hobbyist_programmer
    You can refer to the following code.

            Dim name1 As Name1 = CType(BS_Name1.Current, Name1)  
            Dim name2 As Name2 = CType(BS_Name2.Current, Name2)  
            TextBox1.Text = name1.FirstName + name2.LastName  
    

    Besides, if I have any misunderstanding, please explain in more detail how to use these BindingSources. For example, did you have other controls bound to these BindingSources?

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Karen Payne MVP 35,196 Reputation points
    2021-08-20T13:58:20.423+00:00

    For this (Full source)

    125077-figure1.png

    Mockup first and last names from string list

    Namespace Classes  
        Public Module Mocked  
            Public ReadOnly Property FirstNames() As List(Of String)  
                Get  
                    Return {  
                               "Alejandra", "Alexander", "Ana", "Anabela", "André", "Ann", "Annette", "Antonio",  
                               "Aria", "Art", "Bernardo", "Carine", "Carlos", "Catherine", "Christina", "Daniel",  
                               "Diego", "Dominique", "Eduardo", "Elizabeth", "Felipe", "Fran", "Francisco",  
                               "Frédérique", "Georg", "Giovanni", "Guillermo", "Hanna", "Hari", "Helen", "Helvetius",  
                               "Henriette", "Horst", "Howard", "Isabel", "Jaime", "Janete", "Janine", "Jean",  
                               "John", "Jonas", "Jose", "José", "Jytte", "Karin", "Karl", "Laurence", "Lino", "Liu",  
                               "Liz", "Lúcia", "Manuel", "Maria", "Marie", "Mario", "Martín", "Martine", "Mary", "Matti",  
                               "Maurizio", "Michael", "Miguel", "Palle", "Paolo", "Pascale", "Patricia", "Patricio", "Paul",  
                               "Paula", "Pedro", "Peter", "Philip", "Pirkko", "Renate", "Rene", "Rita",  
                               "Roland", "Sergio", "Simon", "Sven", "Thomas", "Victoria", "Yang", "Yoshi", "Yvonne",  
                               "Zbyszek"}.ToList  
                End Get  
            End Property  
            Public ReadOnly Property LastNames() As List(Of String)  
                Get  
                    Return {  
                               "Accorti", "Afonso", "Anders", "Angel Paolino", "Ashworth", "Batista", "Bennett", "Berglund", "Bergulfsen",  
                               "Bertrand", "Braunschweiger", "Brown", "Camino", "Cartrain", "Carvalho", "Chang", "Citeaux", "Cramer",  
                               "Crowther", "Cruz", "de Castro", "Devon", "Dewey", "Domingues", "Fernández", "Feuer", "Fonseca",  
                               "Franken", "Fresnière", "González", "Gutiérrez", "Hardy", "Henriot", "Hernández", "Holz", "Ibsen",  
                               "Izquierdo", "Jablonski", "Josephs", "Karttunen", "Kloss", "Koskitalo", "Kumar", "Labrune", "Larsson",  
                               "Latimer", "Lebihan", "Limeira", "Lincoln", "McKenna", "Mendel", "Messner", "Moncada", "Moos", "Moreno",  
                               "Moroni", "Müller", "Nagy", "Nixon", "Ottlieb", "Parente", "Pavarotti", "Pedro Freyre", "Pereira", "Perrier",  
                               "Petersen", "Pfalzheim", "Phillips", "Piestrzeniewicz", "Pipps", "Pontes", "Rancé", "Rodriguez", "Roel",  
                               "Roulet", "Rovelli", "Saavedra", "Saveley", "Schmitt", "Simpson", "Snyder", "Sommer", "Steel", "Tannamuri",  
                               "Tonini", "Trujillo", "Wang", "Wilson", "Wong", "Yorres"}.ToList  
                End Get  
            End Property  
        End Module  
    End Namespace  
    

    Form code

    Imports ControlBindingProject.Classes  
      
    Public Class Form1  
        WithEvents lastNameBindingSource As New BindingSource  
        WithEvents firstNameBindingSource As New BindingSource  
      
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown  
      
            firstNameBindingSource.DataSource = Mocked.FirstNames()  
            FirstNameComboBox.DataSource = firstNameBindingSource  
      
            lastNameBindingSource.DataSource = Mocked.LastNames()  
            LastNameComboBox.DataSource = lastNameBindingSource  
      
            ChangePosition()  
      
        End Sub  
    
        Private Sub BindingSources_PositionChanged(sender As Object, e As EventArgs) _  
            Handles firstNameBindingSource.PositionChanged,  
                    lastNameBindingSource.PositionChanged  
      
            ChangePosition()  
      
        End Sub  
      
        Private Sub ChangePosition()  
      
            If firstNameBindingSource.Current IsNot Nothing AndAlso lastNameBindingSource.Current IsNot Nothing Then  
                NameTextBox.Text = $"{FirstNameComboBox.Text} {LastNameComboBox.Text}"  
            End If  
      
        End Sub  
    End Class  
    

    Now we can also use the following two classes rather than list as per above

    FirstNames

    Public Class FirstName  
        Implements INotifyPropertyChanged  
      
        Private _name As String  
      
        Public Property Name() As String  
            Get  
                Return _name  
            End Get  
            Set  
                _name = Value  
                OnPropertyChanged()  
            End Set  
        End Property  
      
        Public Overrides Function ToString() As String  
            Return Name  
        End Function  
      
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged  
        Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional ByVal propertyName As String = Nothing)  
            PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(propertyName))  
        End Sub  
    End Class  
    

    And Last names

    Public Class LastName  
        Implements INotifyPropertyChanged  
      
        Private _name As String  
      
        Public Property Name() As String  
            Get  
                Return _name  
            End Get  
            Set  
                _name = Value  
                OnPropertyChanged()  
            End Set  
        End Property  
      
        Public Overrides Function ToString() As String  
            Return Name  
        End Function  
      
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged  
        Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional ByVal propertyName As String = Nothing)  
            PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(propertyName))  
        End Sub  
    End Class  
    

    Where the above as crystal clear what they are but also more than needed. Why not

    Public Class GenericName  
        Implements INotifyPropertyChanged  
      
        Private _name As String  
      
        Public Property Name() As String  
            Get  
                Return _name  
            End Get  
            Set  
                _name = Value  
                OnPropertyChanged()  
            End Set  
        End Property  
      
        Public Overrides Function ToString() As String  
            Return Name  
        End Function  
      
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged  
        Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional ByVal propertyName As String = Nothing)  
            PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(propertyName))  
        End Sub  
    End Class  
    
    0 comments No comments