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 85211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:03:13+00:00 2026-05-10T22:03:13+00:00

I’m trying to implement a Load / Save function for a Windows Forms application.

  • 0

I’m trying to implement a Load / Save function for a Windows Forms application.

I’ve got following components:

  • A tree view
  • A couple of list views
  • A couple of text boxes
  • A couple of objects (which holds a big dictionarylist)

I want to implement a way to save all of this into a file, and resume/load it later on.

What’s the best way to do this?

I think XML serialization is the way to go, but I’m not quite sure how, or where to start. Or will it require a really complex solution to be able to do this?

  • 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. 2026-05-10T22:03:13+00:00Added an answer on May 10, 2026 at 10:03 pm

    Here’s an example that binds an object and some ancestors to the UI; the use of C# 3.0 here is purely for brevity – everything would work with C# 2.0 too.

    Most of the code here is setting up the form, and/or dealing with property-change notifications – importantly, there isn’t any code devoted to updating the UI from the object model, or the object model from the UI.

    Note also the IDE can do a lot of the data-binding code for you, simply by dropping a BindingSource onto the form and setting the DataSource to a type via the dialog in the property grid.

    Note that it isn’t essential to provide property change notifications (the PropertyChanged stuff) – however, most 2-way UI binding will work considerably better if you do implement this. Not that PostSharp has some interesting ways of doing this with minimal code.

    using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Windows.Forms; using System.Xml.Serialization; static class Program { // formatted for vertical space     [STAThread]     static void Main() {         Application.EnableVisualStyles();          Button load, save, newCust;         BindingSource source = new BindingSource { DataSource = typeof(Customer) };         XmlSerializer serializer = new XmlSerializer(typeof(Customer));         using (Form form = new Form {             DataBindings = {{'Text', source, 'Name'}}, // show customer name as form title             Controls = {                 new DataGridView { Dock = DockStyle.Fill, // grid of orders                     DataSource = source, DataMember = 'Orders'},                 new TextBox { Dock = DockStyle.Top, ReadOnly = true, // readonly order ref                     DataBindings = {{'Text', source, 'Orders.OrderRef'}}},                 new TextBox { Dock = DockStyle.Top, // editable customer name                     DataBindings = {{'Text', source, 'Name'}}},                 (save = new Button { Dock = DockStyle.Bottom, Text = 'save' }),                 (load = new Button{ Dock = DockStyle.Bottom, Text = 'load'}),                 (newCust = new Button{ Dock = DockStyle.Bottom, Text = 'new'}),                }         })         {             const string PATH = 'customer.xml';             form.Load += delegate {                 newCust.PerformClick(); // create new cust when loading form                 load.Enabled = File.Exists(PATH);             };             save.Click += delegate {                 using (var stream = File.Create(PATH)) {                     serializer.Serialize(stream, source.DataSource);                 }                 load.Enabled = true;             };             load.Click += delegate {                 using (var stream = File.OpenRead(PATH)) {                     source.DataSource = serializer.Deserialize(stream);                 }             };             newCust.Click += delegate {                 source.DataSource = new Customer();             };             Application.Run(form);         }      } }  [Serializable] public sealed class Customer : NotifyBase {     private int customerId;     [DisplayName('Customer Number')]     public int CustomerId {         get { return customerId; }         set { SetField(ref customerId, value, 'CustomerId'); }     }      private string name;     public string Name {         get { return name; }         set { SetField(ref name, value, 'Name'); }     }      public List<Order> Orders { get; set; } // XmlSerializer demands setter      public Customer() {         Orders = new List<Order>();     } }  [Serializable] public sealed class Order : NotifyBase {     private int orderId;     [DisplayName('Order Number')]     public int OrderId  {         get { return orderId; }         set { SetField(ref orderId, value, 'OrderId'); }     }      private string orderRef;     [DisplayName('Reference')]     public string OrderRef {         get { return orderRef; }         set { SetField(ref orderRef, value, 'OrderRef'); }     }      private decimal orderValue, carriageValue;      [DisplayName('Order Value')]     public decimal OrderValue {         get { return orderValue; }         set {             if (SetField(ref orderValue, value, 'OrderValue')) {                 OnPropertyChanged('TotalValue');             }         }     }      [DisplayName('Carriage Value')]     public decimal CarriageValue {         get { return carriageValue; }         set {             if (SetField(ref carriageValue, value, 'CarriageValue')) {                 OnPropertyChanged('TotalValue');             }         }     }      [DisplayName('Total Value')]     public decimal TotalValue { get { return OrderValue + CarriageValue; } } }  [Serializable] public class NotifyBase { // purely for convenience     [field: NonSerialized]     public event PropertyChangedEventHandler PropertyChanged;      protected bool SetField<T>(ref T field, T value, string propertyName) {         if (!EqualityComparer<T>.Default.Equals(field, value)) {             field = value;             OnPropertyChanged(propertyName);             return true;         }         return false;     }     protected virtual void OnPropertyChanged(string propertyName) {         PropertyChangedEventHandler handler = PropertyChanged;         if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 60k
  • Answers 60k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Duplicate the test project template, which forces that stupid text… May 11, 2026 at 9:31 am
  • added an answer First add the Get-tfs function to your script: function get-tfs… May 11, 2026 at 9:30 am
  • added an answer The question 'So how can I test whether this method… May 11, 2026 at 9:30 am

Related Questions

I keep getting tasks that are above my skill level. How can I address this without coming accross as grossly incompetent?
I have a web-service that I will be deploying to dev, staging and production.
I'm thinking of starting a wiki, probably on a low cost LAMP hosting account.
I have the following tables in my database that have a many-to-many relationship, which
I'm using the RESTful authentication Rails plugin for an app I'm developing. I'm having
I recently printed out Jeff Atwood's Understanding The Hardware blog post and plan on
I find that getting Unicode support in my cross-platform apps a real pain in
I would like to test a string containing a path to a file for
I'm getting this problem: PHP Warning: mail() [function.mail]: SMTP server response: 550 5.7.1 Unable
I'm an Information Architect and JavaScript developer by trade nowadays, but recently I've been

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.