Home > Mobile >  Databinding textbox to a structure
Databinding textbox to a structure

Time:01-27

I am using a structure and serializing it to SQL server. I am trying to databind a few masked & normal textboxes to this structure so when I read the data back into my structure it will auto populate my bound textboxes but it doesn't work...I think my binding is maybe incorrect syntax? Can somebody lend a hand? Thank you!

<Serializable()>
Private Enum PhoneTypes
    <Description("New phoneline")> [NewLine] = 1
    <Description("Existing phoneline")> [ExistingLine] = 2
    <Description("Voicemail only")> [VoicemailOnly] = 3
End Enum

'this is for the phones tab to serialize it and store it in the database 
<Serializable()>
Private Structure Phones
    Public Property PhoneNeeds As PhoneTypes
    Public Property PhoneNumber As String
    Public Property Extension As String
    Public Property Notes As String
End Structure

Private PhoneTab As new Phones

Private Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    InitPhones()
    DoStuff()
End Sub

Private Sub InitPhones()
    txtPhoneNotes.DataBindings.Add("Text", PhoneTab, "Notes")
    txtPhoneNumber.DataBindings.Add("Text", PhoneTab, "PhoneNumber")
    txtExtension.DataBindings.Add("Text", PhoneTab, "Extension")
End Sub

Private Sub DoStuff()
    PhoneTab = mod_Global.Deserialize(Of Phones)(.Item("User_Needed_Phones"))
    MsgBox(PhoneTab.Notes) '<--- this works and I can see the values contained in my structure but does not auto populate the textbox from the binding 
End If 

CodePudding user response:

TL; DR; The case is an interesting one however the fix is pretty simple and if you are looking for the fix, jump to end of the post to the Fix section.


If you are interested to pay more attention to the details, continue reading here. Consider the following facts:

  • Databinding to structures is half-way supported. It means, right after the databinding, you can see the property value of the structure in the control property. But later, if you modify control property value, the changes will not be reflected into your structure. Also if you modify the structure property value the changes will not be reflected to the control property.
    That's because when you setup databinding to a structure, the value of your structure will be copied to the Binding class, and later when you update your control or the values of the COPY will be updates, or if you assign a new value to property of your original structure, the COPY will not see the changes.
    Also even if you use a class, when you modify property values of your object, the changes will be reflected in the control immediately, only if your class implement the INotifyPropertyChanged correctly.

  • Even if you use a class, assuming you have an object in a variable v, and you setup databinding to that object, later if you assign a new instance to variable v, it has nothing to do with the databinding. Variable v now is pointing to a new instance of your object and the datavbinding keeps using the previous instance of the object.

I believe reading a bit more about Value Types and Reference Types will help you to get a better understanding of the current situation.

Here is a simplified version of your code:

1: dim x = New MyStructure()
2: textBox1.DataBindings.Add("Text", x , "MyProperty")
3: Dim x =  LoadAnotherMyStructure()

In line two, you are setting databinding to an instance of MyStructure; It basically copies the value of x into the constructor of Binding class and then it doesn't have any idea about what happens for the x later; it cannot monitor it.

It's just able to load the value of MyProperty of x which you have in line 1 and show it in textBox1.Text, but later, if you assign a new Structure to x or modify value of x.MyProperty then textBox1.Text will not be updated.

Also if you change the value of the textBox1.Text, the change will not be reflected to the structure.


Fix

The fix is pretty simple here:

1: Dim x =  LoadAnotherMyStructure()
2: textBox1.DataBindings.Add("Text", x , "MyProperty")

But keep in mind, it will be still half-way. You can see the values in your control, but if you modify the values in control, they will not be written back to the structure.

  •  Tags:  
  • Related