Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6030757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:05:03+00:00 2026-05-23T05:05:03+00:00

I have a form (CustomerInfoForm) with 10 TextBoxes. The default Text property for each

  • 0

I have a form (CustomerInfoForm) with 10 TextBoxes. The default Text property for each of the TextBoxes is defined at design-time. A subclass CustomerInfoForm.CustomerInfo contains properties to hold the data entered in the form. The subclass containing the data will be serialized to XML.

In the automatically generated form code, each of the text boxes has a line of code to bind the datasource to the text box

this.customerInfoBindingSource = new System.Windows.Forms.BindingSource(this.components);

Code automatically generated by the C# ide for each text box:

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 isn’t set until after the DataBinding) in the IDE generated code.

When I run the project, the form is displayed with the default values in the TextBoxes. However when the SaveButton is pressed to serialize the properties in the MyForm.CustomerInfo subclass, they are all null. Since these values will only be changed from the form I was hoping that I didn’t have to implement the interface INotifyPropertyChanged.

Am I missing something basic or simple?

The code for the form including the serialization of the data is attached 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 happen upon this question

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();
        }
        // You must apply a DataContractAttribute or SerializableAttribute
        // to a class to have it serialized by the DataContractSerializer.
        [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; }

            // Constructor is needed to instantiate the PhoneInfo classes
            // within the CustomerInfo class
            public CustomerInfo()
            {
                PhonePrimary = new PhoneInfo();
                PhoneDays = new PhoneInfo();
                PhoneEvening = new PhoneInfo();
            }
        }

        public class PhoneInfo
        {
            public string number { get; set; }
            public string type { get; set; }
            public bool textOk { get; set; }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:05:04+00:00Added an answer on May 23, 2026 at 5:05 am

    First of all you need to decide whether to use databinding or manipulate Text property directly. Those two approaches should not be mixed together.

    If you want to use databinding than you are missing one line in the code:

    public Form1()
    {
        InitializeComponent();
        customerInfoBindingSource.DataSource = ci;  // This is the missing line
    }
    

    You need to let your customerInfoBindingSource know about the data source.

    If you add this line, then the Text that is assigned in design time will be overridden by the text from your bound data source. If you want to use binding you should manipulate with the data source instead of setting Text fields directly. Like this:

    public Form1()
    {
        InitializeComponent();
    
        ci.CustomerName = "TestCustomerName";
        customerInfoBindingSource.DataSource = ci;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have form, the form contains JavaStaticText fields, and Combo box, and Text fields.
I have form with user defined filters ( combobox with column names, combobox with
I have form and form text field which generates dynamically using JSP. And I'm
I have form like this: <input type='text' name='people' id='namepeople'> <input type='hidden' name='idpeople' id='idpeople'> <input
I have Form subclass with handlers for MouseHover and MouseLeave . When the pointer
I have form with few text boxes which goes through validation (both server and
Let's say I have form A that contains a panel (with many other controls
i have form with text inputs having some value i.e. please enter name etc
I have form elements like this: <input type='text' id='person_1_name'> <input type='text' id='person_2_name'> <input type='text'
I have form for example: <input type=text /> <input type=file /> <input type=submit />

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.