Click here to Skip to main content
6,630,586 members and growing! (16,840 online)
Email Password   helpLost your password?
Languages » C# » Windows Forms     Intermediate

Persisting TextBox Values in a Windows Forms Application

By Shailen Sukul

An article on saving textbox values of a Windows application, and restoring them when the application restarts.
C#, Windows, .NET 1.1VS.NET2003, Dev
Posted:20 Apr 2006
Views:15,533
Bookmarked:14 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 0.95 Rating: 1.57 out of 5
3 votes, 75.0%
1

2

3

4
1 vote, 25.0%
5

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.

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.

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

About the Author

Shailen Sukul


Member

Occupation: Web Developer
Location: Australia Australia

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
Generalwhere multi-threaded filedownloader application for my next article... Pinmemberalhambra-eidos2:34 25 Feb '09  
GeneralRe: where multi-threaded filedownloader application for my next article... PinmemberShailenSukul17:14 3 Mar '09  
Generalelaborate please PinmemberRedSunBeer20:35 25 Apr '06  
AnswerRe: elaborate please PinmemberC-Sharper15:34 4 May '06  
GeneralRe: elaborate please PinmemberShailen Sukul18:59 4 May '06  
Generalgood Pinmemberi.Posei5:13 21 Apr '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Apr 2006
Editor: Smitha Vijayan
Copyright 2006 by Shailen Sukul
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project