question

Hobbyistprogrammer-7674 avatar image
0 Votes"
Hobbyistprogrammer-7674 asked karenpayneoregon answered

Push Shared Property value change to object instances Vb.Net

Hallo All,

I have following code and i am trying to push the shared property change to another class object instances. I have a class setting with shared property language.

I have another class Test where it takes this shared property. I want to change the language property in setting so that the change of language immediately updated in my Test object instances. How do i do it?.

Thanks

 Imports System.ComponentModel
 Imports System.Runtime.CompilerServices
    
 Public Class Form1
    
     Public Test As New Tests
     Public BS_Setting As New BindingSource
    
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         BS_Setting.DataSource = Test
         DataGridView2.DataSource = BS_Setting
     End Sub
     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
         BS_Setting.Add(New Test())
     End Sub
     Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
         Setting.language = "French"
     End Sub
    
 End Class
    
    
 Public Class Setting
     Implements INotifyPropertyChanged
    
     Public Sub New()
     End Sub
    
     Public Shared languageValue As String = "ENG"
     Public Shared Property language() As String
         Get
             Return languageValue
         End Get
         Set(ByVal value As String)
             languageValue = value
         End Set
     End Property
    
     Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
     Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional memberName As String = Nothing)
         RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(memberName))
     End Sub
 End Class
    
 Public Class Test
     Public Sub New()
     End Sub
    
     Private LangValue As String
     Public Property Lang As String
         Get
             Return Setting.language
         End Get
         Set(ByVal value As String)
             LangValue = value
         End Set
     End Property
    
 End Class
    
 Public Class Tests
     Inherits BindingList(Of Test)
 End Class

42822-test.gif


dotnet-visual-basic
test.gif (90.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered

Hi @Hobbyistprogrammer-7674 ,

I want to change the language property in setting so that the change of language immediately updated in my Test object instances.

Try to refresh the DataGridView when you change the 'language' property.

         Setting.language = "French"
         DataGridView2.Refresh()

Result of my test.
42910-gif.gif
Hope it could be helpful.

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.



gif.gif (42.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hobbyistprogrammer-7674 avatar image
0 Votes"
Hobbyistprogrammer-7674 answered

wow.. thanks for the answer XingyuZhao.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hobbyistprogrammer-7674 avatar image
0 Votes"
Hobbyistprogrammer-7674 answered

Why shared properties are not showing up in the datagridview?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

Hello @Hobbyistprogrammer-7674

Even though you have an accepted solution I'd like to provide another solution.

  • Rather than a shared field use a non-shared field unless you want all existing rows to change.

  • There is really no need to have a wrapper class simply for the BindingList, instead use the Setting class assigned to a BindingSource in the form. The class should really only have properties and INotifyPropertyChanged event and OnPropertyChanged

Setting class
Read the comments

 Imports System.ComponentModel
 Imports System.Runtime.CompilerServices
    
 Namespace Classes
     Public Class Setting
         Implements INotifyPropertyChanged
    
         Public Sub New(Optional language As String = "English")
             languageValue = language
         End Sub
         ''' <summary>
         ''' Shared modifier means all instances are affected,
         ''' remove Shared if that is not what you want.
         ''' </summary>
         Public languageValue As String = ""
         Public Property Language() As String
             Get
                 Return languageValue
             End Get
             Set
                 languageValue = Value
                 '
                 ' Make sure to implement the line below
                 '
                 OnPropertyChanged()
             End Set
         End Property
    
         Public Event PropertyChanged As PropertyChangedEventHandler _
             Implements INotifyPropertyChanged.PropertyChanged
    
         Protected Overridable Sub OnPropertyChanged(
             <CallerMemberName> Optional memberName As String = Nothing)
             RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(memberName))
    
         End Sub
     End Class
 End Namespace


Form code

 Imports System.ComponentModel
 Imports BindingWithoutRefreshing.Classes
    
 Public Class Form1
    
     Public SettingsBindingSource As New BindingSource
     Public SettingsBindingList As New BindingList(Of Setting)
    
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         DataGridView2.AutoGenerateColumns = False
    
         SettingsBindingList = New BindingList(Of Setting)(New List(Of Setting))
         SettingsBindingSource.DataSource = New BindingSource(SettingsBindingList, Nothing)
    
         DataGridView2.DataSource = SettingsBindingSource
    
         LanguagesComboBox.DataSource = New List(Of String) From {"English", "French", "Spanish"}
    
     End Sub
    
     Private Sub AddNewSettingButton_Click(sender As Object, e As EventArgs) _
         Handles AddNewSettingButton.Click
    
         SettingsBindingSource.Add(New Setting(LanguagesComboBox.Text))
         SettingsBindingSource.MoveLast()
    
     End Sub
    
     Private Sub ChangeLanguageButton_Click(sender As Object, e As EventArgs) _
         Handles ChangeLanguageButton.Click
    
         If SettingsBindingSource.Current IsNot Nothing Then
             CType(SettingsBindingSource.Current, Setting).Language = LanguagesComboBox.Text
         End If
    
     End Sub
    
     Private Sub DataGridView2_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) _
         Handles DataGridView2.DefaultValuesNeeded
    
         e.Row.Cells(0).Value = LanguagesComboBox.Text
    
     End Sub
 End Class

Full source GitHub repository

43405-form.png



form.png (6.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.