I have a custom class in which I have defined a couple of properties:
| 1 |
PublicClassMyClass |
| 2 |
|
| 3 |
DimaasInteger=0 |
| 4 |
DimbasInteger=0 |
| 5 |
|
| 6 |
PublicPropertyA1()asInteger |
| 7 |
Get |
| 8 |
Returna |
| 9 |
EndGet |
| 10 |
Set(ByValvalueAsInteger) |
| 11 |
a=value |
| 12 |
EndSet |
| 13 |
EndProperty |
| 14 |
|
| 15 |
PublicPropertyB1()asInteger |
| 16 |
Get |
| 17 |
Returnb |
| 18 |
EndGet |
| 19 |
Set(ByValvalueAsInteger) |
| 20 |
b=value |
| 21 |
EndSet |
| 22 |
EndProperty |
| 23 |
|
| 24 |
EndClass | I am using the properties defined in this class (A1() and B1()) as the datasource for two labels (on the text property)on a form I have. In the designer, I haveeven set the DataSource Update Mode to OnPropertyChanged. But the thing is, the text of the labels is not changing when the values of my two properties (A1() and B1()) are changing. In a databound context, shouldn't the change in the underlying property reflect in the bound control as well? I'd like to know if there is anything wrong with my databinding method, or if there is something else wrong with the application itself, why it isn't binding as it should? | | reachrishikh Tuesday, March 10, 2009 11:11 AM |
Hi reachrishikh,
I am sorry I don't familiar with VB language so I use convert tool to convert my code written before from C# to VB. It is not correctly, Now I complete make it in VB project, it works fine.
| ImportsSystem.ComponentModel |
|
| PublicClassForm1 |
| InheritsForm |
| PrivatemClassAsNewMClass() |
| PublicSubNew() |
| InitializeComponent() |
| Me.TextBox1.DataBindings.Add("Text",mClass,"A1",True,DataSourceUpdateMode.OnPropertyChanged) |
| Me.TextBox2.DataBindings.Add("Text",mClass,"B1",True,DataSourceUpdateMode.OnPropertyChanged) |
| EndSub |
|
| PrivateSubbutton1_Click(ByValsenderAsObject,ByValeAsEventArgs)HandlesButton1.Click |
| mClass.A1=11 |
| mClass.B1=12 |
| EndSub |
| EndClass |
| PublicClassMClass |
| ImplementsINotifyPropertyChanged |
|
| PrivateaAsInteger=0 |
| PrivatebAsInteger=0 |
| PublicEventPropertyChanged(ByValsenderAsObject,ByValeAsSystem.ComponentModel.PropertyChangedEventArgs)ImplementsSystem.ComponentModel.INotifyPropertyChanged.PropertyChanged |
|
| PublicSubNew() |
| EndSub |
|
| PublicPropertyA1()AsInteger |
| Get |
| Returna |
| EndGet |
| Set(ByValvalueAsInteger) |
| a=value |
| RaiseEventPropertyChanged(Me,NewPropertyChangedEventArgs("A1")) |
| EndSet |
| EndProperty |
|
| PublicPropertyB1()AsInteger |
| Get |
| Returnb |
| EndGet |
| Set(ByValvalueAsInteger) |
| b=value |
| RaiseEventPropertyChanged(Me,NewPropertyChangedEventArgs("B1")) |
| EndSet |
| EndProperty |
| EndClass |
MyClass is a keyword in VB, you cannot use it as your class name. So I change it into MClass. Sorry for any inconvenience.
Sincerely, Kira Qian
Please mark the replies as answers if they help and unmark if they don't. - Marked As Answer byreachrishikh Friday, March 13, 2009 8:27 AM
-
| | Kira Qian Friday, March 13, 2009 1:46 AM | I tried to remove the databinding done from the designer in Visual Studio, and tried hardcoding the databinding instead using the code: Me.Label14.DataBindings.Add("Text", myclass, "mypropertyinmyclassl", True, DataSourceUpdateMode.OnPropertyChanged) but still no go.
What gives? | | reachrishikh Wednesday, March 11, 2009 6:09 PM |
Hi reachrishikh,
You should implement INotifyPropertyChanged interface. Here is the example.
| PublicClassForm1 |
| InheritsForm |
| PrivatemyClassAsNewMyClass() |
| PublicSubNew() |
| InitializeComponent() |
| Me.textBox1.DataBindings.Add("Text",myClass,"A1",True,DataSourceUpdateMode.OnPropertyChanged) |
| Me.textBox2.DataBindings.Add("Text",myClass,"B1",True,DataSourceUpdateMode.OnPropertyChanged) |
| EndSub |
| |
| PrivateSubbutton1_Click(ByValsenderAsObject,ByValeAsEventArgs) |
| myClass.A1=11 |
| myClass.B1=12 |
| EndSub |
| EndClass |
|
| PublicClassMyClass |
| ImplementsINotifyPropertyChanged |
| PrivateaAsInteger=0 |
| PrivatebAsInteger=0 |
| PublicEventPropertyChangedAsPropertyChangedEventHandler |
| |
| PublicSubNew() |
| EndSub |
| |
| PublicPropertyA1()AsInteger |
| Get |
| Returna |
| EndGet |
| Set(ByValvalueAsInteger) |
| a=value |
| PropertyChanged(Me,NewPropertyChangedEventArgs("A1")) |
| EndSet |
| EndProperty |
| |
| PublicPropertyB1()AsInteger |
| Get |
| Returnb |
| EndGet |
| Set(ByValvalueAsInteger) |
| b=value |
| PropertyChanged(Me,NewPropertyChangedEventArgs("B1")) |
| EndSet |
| EndProperty |
| EndClass |
So when the value of your property is changed, it can notify the UI to update the value.
Sincerely, Kira Qian
Please mark the replies as answers if they help and unmark if they don't. | | Kira Qian Thursday, March 12, 2009 4:22 AM | I thought as much. I knew everything else was working fine, including the databinding upon form load, but changing the properties in the source class wasn't raising the propertychanged event, which is why the labels weren't updating. And I didn't know how to raise that event.
Anyway, I had to import System.ComponentModel first, and even then, in the implements statement it gave me a blue squiggly line saying that "Class MyClassmust implement 'EventPropertyChanged(sender As Object, e as Event As PropertyChangedEventArgs)' for interface 'System.ComponentModel.INotifyPropertyChanged'." | | reachrishikh Thursday, March 12, 2009 8:41 AM |
Hi reachrishikh,
I have highlight the key point in my code. PropertyChanged(Me, New PropertyChangedEventArgs("A1")) will raise the event and notify which property is changed. Do you have any question?
Sincerely, Kira Qian
Please mark the replies as answers if they help and unmark if they don't. | | Kira Qian Thursday, March 12, 2009 9:17 AM | Actually I do:
I had to import System.ComponentModel first, and even then, in the implements statement it gave me a blue squiggly line saying that "Class MyClassmust implement 'EventPropertyChanged(sender As Object, e as Event As PropertyChangedEventArgs)' for interface 'System.ComponentModel.INotifyPropertyChanged'." The Implements statement in the code you gave me isn't working on my machine. It gives an error as stated above.
Also, the line you highlighted in your last post also gives a blue squiggly andsaysit is an event and can't be used directly and that I have touse a RaiseEvent statement toraise an event.
I think solving the problem with the squiggly in the Implements will solve all other squigglys as well. | | reachrishikh Thursday, March 12, 2009 10:10 AM |
Hi reachrishikh,
I am sorry I don't familiar with VB language so I use convert tool to convert my code written before from C# to VB. It is not correctly, Now I complete make it in VB project, it works fine.
| ImportsSystem.ComponentModel |
|
| PublicClassForm1 |
| InheritsForm |
| PrivatemClassAsNewMClass() |
| PublicSubNew() |
| InitializeComponent() |
| Me.TextBox1.DataBindings.Add("Text",mClass,"A1",True,DataSourceUpdateMode.OnPropertyChanged) |
| Me.TextBox2.DataBindings.Add("Text",mClass,"B1",True,DataSourceUpdateMode.OnPropertyChanged) |
| EndSub |
|
| PrivateSubbutton1_Click(ByValsenderAsObject,ByValeAsEventArgs)HandlesButton1.Click |
| mClass.A1=11 |
| mClass.B1=12 |
| EndSub |
| EndClass |
| PublicClassMClass |
| ImplementsINotifyPropertyChanged |
|
| PrivateaAsInteger=0 |
| PrivatebAsInteger=0 |
| PublicEventPropertyChanged(ByValsenderAsObject,ByValeAsSystem.ComponentModel.PropertyChangedEventArgs)ImplementsSystem.ComponentModel.INotifyPropertyChanged.PropertyChanged |
|
| PublicSubNew() |
| EndSub |
|
| PublicPropertyA1()AsInteger |
| Get |
| Returna |
| EndGet |
| Set(ByValvalueAsInteger) |
| a=value |
| RaiseEventPropertyChanged(Me,NewPropertyChangedEventArgs("A1")) |
| EndSet |
| EndProperty |
|
| PublicPropertyB1()AsInteger |
| Get |
| Returnb |
| EndGet |
| Set(ByValvalueAsInteger) |
| b=value |
| RaiseEventPropertyChanged(Me,NewPropertyChangedEventArgs("B1")) |
| EndSet |
| EndProperty |
| EndClass |
MyClass is a keyword in VB, you cannot use it as your class name. So I change it into MClass. Sorry for any inconvenience.
Sincerely, Kira Qian
Please mark the replies as answers if they help and unmark if they don't. - Marked As Answer byreachrishikh Friday, March 13, 2009 8:27 AM
-
| | Kira Qian Friday, March 13, 2009 1:46 AM | Thank you very much, it works fine now.
Oh, and 'MyClass' isn't really the name of my class. I just put whatever name first came to my mind in the example here, I'm not posting the names of my actual classes, properties, methods etc. publicly.
Thanks for the advice anyway, who knows, if I ran out of names I might have really used 'MyClass' as the name for one of my classes - I am really bad at naming things. | | reachrishikh Friday, March 13, 2009 8:27 AM |
|