I have a form (CustomerInfoForm) with 10 text fields. The default Text property for each of the TextBoxes is determined at design time. The CustomerInfoForm.CustomerInfo subclass contains properties for storing data entered in the form. A subclass containing the data will be serialized in XML.
In code with an automatically generated form, each text field has a line of code to bind the data source to the text field
this.customerInfoBindingSource = new System.Windows.Forms.BindingSource(this.components);
Code automatically generated by C # ide for each text field:
this.txtCustomer.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.customerInfoForm_CustomerInfoBindingSource, "CustomerName", true)); this.txtCustomer.Location = new System.Drawing.Point(60, 23); this.txtCustomer.Name = "txtCustomer"; this.txtCustomer.Size = new System.Drawing.Size(257, 20); this.txtCustomer.TabIndex = 0; this.txtCustomer.Text = "CustomerName";
(I noticed that the Text property is not set until after the DataBinding) in the generated IDE code.
When I run the project, the form is displayed with the default values ββin TextBoxes . However, when SaveButton clicked to serialize properties in the MyForm.CustomerInfo subclass, they are all zero. Since these values ββwill only be changed from the form, I was hoping that I did not need to implement the INotifyPropertyChanged interface.
Am I missing something basic or simple?
The form code, including data serialization, is given below.
using System; using System.Windows.Forms; using System.Xml.Serialization; using System.IO; using System.Runtime.Serialization; namespace SimpleCustomerInfo { // You must apply a DataContractAttribute or SerializableAttribute // to a class to have it serialized by the DataContractSerializer. public partial class CustomerInfoForm : Form { CustomerInfo ci = new CustomerInfo(); public CustomerInfoForm() { InitializeComponent(); } private void btnSave_Click(object sender, EventArgs e) { DataContractSerializer serializer = new DataContractSerializer(typeof(CustomerInfo)); FileStream writer = new FileStream(@"C:\Users\Me\temp\testme.xml", FileMode.Create); serializer.WriteObject(writer,ci); writer.Close(); } [DataContract(Name = "Customer", Namespace = "net.ElectronicCanvas")] public class CustomerInfo { [DataMember] public string CustomerName { get; set; } [DataMember] public PhoneInfo PhonePrimary { get; set; } [DataMember] public PhoneInfo PhoneDays { get; set; } [DataMember] public PhoneInfo PhoneEvening { get; set; } } public class PhoneInfo { public string number { get; set; } public string type { get; set; } public bool textOk { get; set; } } } }
EDIT - for others that may arise on this subject
using System; using System.Windows.Forms; using System.Xml.Serialization; using System.IO; using System.Runtime.Serialization; namespace SimpleCustomerInfo { public partial class CustomerInfoForm : Form { CustomerInfo ci; public CustomerInfoForm() { InitializeComponent(); ci = new CustomerInfo(); ci.CustomerName = "My Customer Name"; ci.PhoneDays.number = "888-888-8888"; customerInfoForm_CustomerInfoBindingSource.DataSource = ci; } private void btnSave_Click(object sender, EventArgs e) { DataContractSerializer serializer = new DataContractSerializer(typeof(CustomerInfo)); FileStream writer = new FileStream(@"C:\Users\me\temp\testme.xml", FileMode.Create); serializer.WriteObject(writer,ci); writer.Close(); }