Resolved DataBinding only works under certain circumstances

pgm575

Member
Joined
Jul 16, 2010
Messages
12
Programming Experience
Beginner
So this may take a few posts to get out all the information needed to solve this issue, but I'll try and be as complete as possible.

I have a Windows Forms which contains a whole bunch of different text items on TextBoxes, Labels, and ComboBoxes. I then catalog what is written in these Textboxes and selected in these ComboBoxes in XML files so that they can be reopened later. Because what goes into the Textboxes and ComboBoxes are physical quantities, they have units (for examples "Inches," or "Lbs," etc.) which are the text of the Labels themselves. Multiple quantities share similar units, so I don't allow the user to change each one individually, but instead change a unit globally (for example, all weights in "Kilogram" instead of "Lb").

The units themselves are Public Properties of a separate class which I named "MyDBClass". The properties raise events when they are Set, so that objects data-bound to them change as well.

When I initialize the main Windows form, I instantiate a class of type "MyDBClass," set all the unit defaults ("LengthShort = mm," etc.), and create a databinding for the Labels to the "MyDBClass" properties (e.g. ctrl.DataBindings.Add("Text", MyDBClass, "LengthS") ). From then on, the only way the user can change the units is to use my MenuStrip and select "New" (which erases all text and sets the MyDBClass properties back to default), use the MenuStrip and select "Open" (which opens an XML file, reads the "Unit" elements in, and uses them to set the MYDBClass properties), or use the MenuStrip and select "Change Units" (which brings up a dialog with a handful of ComboBoxes containing possible units, and then sets the properties of the MyDBClass based on what the user selected in the dialog).

Having said ALL of that, I can finally get to the problem- one of my units ("SpringRate") does not change when the user selects "New" or "Change Units" from the MenuStrip. It will only change when the user opens an XML file. This makes no sense, as the act of opening an XML file calls the exact same code as selecting "New" from the MenuStrip (which is that is sets the properties of the MyDBClass). The label is also identical to its surrounding labels and the ComboBox which selects the unit is identical to its surrounding ComboBoxes (I tend to use Copy/Paste when working in Design mode). I even put Try->Catch blocks around the Property's Set code and the code inside the other Subs which calls it.

Anyone see what detail I'm missing here?
 
Last edited:

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
this may be silly answer but check your handles on the code, i was making an applciation for my uncles business, had to start over about 2 times, but one time everything stoped working, and couldn't figure out why. i had most of my stuff in a panel as i had a few labels and textboxs hiden underneath it, but but because i cut all the item then deleted the panel when i pasted them back in the form the handles on all of my sub that were cut and pasted were removed
 

pgm575

Member
Joined
Jul 16, 2010
Messages
12
Programming Experience
Beginner
The thing is, there are no direct Handles assigned.
Inside the MyDBClass, I instantiate EventHandlers for each property (which is
VB.NET:
Public Event SprRateChanged As EventHandler
in this case) and then I call the EventHandler inside the Property's Set routine.

VB.NET:
Public Property SpringRate() As String
     Get
          Return xSprRate
     End Get
     Set(ByVal value As String)
          xSprRate = value
          RaiseEvent SprRateChanged(Me, New EventArgs)
     End Set
End Property

Therefore, the Event Handle is assigned automatically during runtime when the DataBinding is created.

This works with the databindings for every other label except for this one.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,609
Location
Norway
Programming Experience
10+
For binding notification to work your event must be named same as property with the "Changed" appendix, eg "SpringRateChanged" event for "SpringRate" property. "SprRateChanged" will not work for binding notification for a "SpringRate" property.
Or else you can implement INotifyPropertyChanged interface.
 

pgm575

Member
Joined
Jul 16, 2010
Messages
12
Programming Experience
Beginner
I had no idea that was the case, I was just naming them similarly so that they were easy to remember.

Your suggestion has, in fact, fixed my problem. Thanks very much!
 
Top Bottom