Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C#
Article

Persisting TextBox Values in a Windows Forms Application

Rate me:
Please Sign up or sign in to vote.
1.57/5 (4 votes)
20 Apr 2006 33.4K   568   18   6
An article on saving textbox values of a Windows application, and restoring them when the application restarts.

Introduction

I was writing a Windows application and needed to save some values displayed in a textbox. I thought it would be neat if I could encapsulate the logic for persisting textbox values in a Windows Form and have it automatically restored when the application restarts, for example in an input form.

Using the code

The main bit of logic is encapsulated in the PersistenceHelper assembly.

C#
public Form1() 
{
    ...
    persist = new StatePersistence(this);
}

The target form calls the persistence class from its constructor and passes a reference to itself in the argument. That is all that is needed. The rest is taken care of in the PersistenceHelper library.

C#
public StatePersistence(Form form)
{
    this.form = form;
    this.form.Closing += 
      new System.ComponentModel.CancelEventHandler(form_Closing);
    formStateFileName = 
      Assembly.GetExecutingAssembly().Location.Substring(0,
      Assembly.GetExecutingAssembly().Location.LastIndexOf("\\")) + 
      "\\" + form.Name + ".xml" ;
    LoadControlValues();
}

The persistence library has only one class; StatePersistence.cs. Its constructor takes in a Form as parameter and loads saved values from an XML file into the Form's controls. The name of the persisted XML file is the form name + ".xml". It also subscribes to the Form's closing event, and when invoked, it saves the Form's controls into the XML file.

Fig 1: A sample Windows Form demonstrating persistence.

History

  • 14-April-2006 - Initial version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalwhere multi-threaded filedownloader application for my next article... Pin
kiquenet.com25-Feb-09 1:34
professionalkiquenet.com25-Feb-09 1:34 
GeneralRe: where multi-threaded filedownloader application for my next article... Pin
ShailenSukul3-Mar-09 16:14
ShailenSukul3-Mar-09 16:14 
Generalelaborate please Pin
RedSunBeer25-Apr-06 19:35
RedSunBeer25-Apr-06 19:35 
AnswerRe: elaborate please Pin
C-Sharper4-May-06 14:34
C-Sharper4-May-06 14:34 
GeneralRe: elaborate please Pin
Shailen Sukul4-May-06 17:59
Shailen Sukul4-May-06 17:59 
Generalgood Pin
i.Posei21-Apr-06 4:13
i.Posei21-Apr-06 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.