Saving text in labels

892 Views Asked by At

I have created a C# Windows Form Application which contains labels (among other things, but they're unrelated to the question). what i would like to be able to do is save the .Text property of certain labels. I understand how to set up the form close and form load events, but i have no idea where to even start with the saving??

in case anybody wanted to show me a code snippet, the labels are named: lblJoeMoney, lblBobMoney, lblAlMoney

any tips or links to where i can get more info would be great.

Side Note: i know i didn't explicitly ask a question, so here it is: how can i save certain data in a C# Windows Form Application

3

There are 3 best solutions below

0
On

It sounds like you want to save the text to be used the next time the program is run. See: https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v=vs.110).aspx

for an example to save the contents in the configuration files App Settings.

0
On

In my knowledge, the simple solution is serializing all you need to save information into a xml file.
when your program is closing, in the Form_Closing event section, you save all information in to xml file.
And then, in the next time of running, in the Form_Load section, you will load all in formation from that xml file.

0
On

There are several options for saving data. Each option is either good or bad depending on your needs and depending on the kind of data you need to save. I will run through a few options.

Option 1 -- Database

Databases are, by far, the most popular choice for saving data. In a database, you create tables. Tables organize, group, and define the types of data you want to save. For a simple address book, for example, you might have a table that holds Name, Address, City, State and Zip.

Databases can be complex, but simplify the tasks of retrieving your data back. You might want to retrieve only names from the state of New York, for example. Storing data in a database makes good sense if you are needing to store lots of data and need to query the data several different ways.

Some of the most popular choices for database servers include Microsoft Sql Server, Oracle, and MySql. These are known as client/server databases. The database server usually sits on a remote computer somewhere on the Internet and your program (the client) connects to it. There are also file system databases that don't require a server. Examples of this would be SQLite and SQL Server Express. These just create a file on your computer and all the data gets stored on that single file.

For Internet and Web applications, usually a client/server database is the way to go. For Desktop applications, such as WPF and WinForms applications, usually you'll want a file system database so that it can be easily distributed with your application without the need for a constant Internet connection.

Option 2 -- Xml File

An XML file is kind of like a database (however, internally, it works nothing like a database) in that it is used to store data in a column and row kind of format. An XML file stores data in human readable format that can be opened in notepad. An XML file stores data in tags. Here is an example of what that address book might look like in an XML file:

<?xml version="1.0" encoding="UTF-8"?>
<Names>
    <Name>
        <FirstName>Joe</FirstName>
        <LastName>Smith</LastName>
        <Address>123 Main Street</Address>
        <City>Poughkeepsie</City>
        <State>NY</State>
    </Name>
    <Name>
        <FirstName>Mike</FirstName>
        <LastName>Jones</LastName>
        <Address>982 Baker Street</Address>
        <City>Fargo</City>
        <State>ND</State>
    </Name>
</Names>

Xml files are easy to create in C#. In most cases, you can just create an instance of the XmlSerializer class and call the Serialize method or convert your class instance into XML and call the Deserialize method to convert from XML back into an instance of your class. The disadvantage is that XML is very verbose and if you are storing tons of data, your XML file will become big and it will become a slow process to serialize and deserialize your data. In addition, searching for data can become a slow process.

For your application, this may be the way to go. You can find step-by-step instructions on how to do this here.

Option 3 -- Registry

The registry is usually used to store program settings and is not recommended for storing data. Since it is a viable storage option, I just wanted to briefly mention it here. Use this for storing settings and not data!