View Single Post
Old Apr 17th, 2008, 6:12 AM   #1
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 175
Rep Power: 1 BstrucT is on a distinguished road
Autosaving Streams in C#

Hi All

I have decided to implement a little autosave function into my application that is used for datacapturing, but for some reason I am having some trouble.

Example...

Let's say I have three fields that text data can be stored in: txtfield1, txtfield2, and txtfield3.
I also have a seperate btn(bntSave) that when clicked, writes the entered data to the specifics I want it to.

Also add a text box(txtSaveName) used to specify the filename if desired.

Now, the event for the button would look something like this:

private void btnSave1_Click(object sender, EventArgs e)
        {
            StreamWriter write1 = new StreamWriter 
            ( "C:\\" + txtSaveName.Text );

            write1.WriteLine(txtfield1.Text);
            write1.WriteLine(txtfield2.Text);
            write1.WriteLine(txtfield3.Text);

            write1.Close();
         }
Now let's say I want the program to autosave using a timer, can I do the following?

private void timer_Tick(object sender, EventArgs e)
     {
            DateTime autosave = DateTime.Today;
            if (txtSaveName.Visible == true)
            {
                StreamWriter write1 = new StreamWriter
                ("C:\\" + "Auto Saved" + txtSaveName.Text);

            write1.WriteLine(txtfield1.Text);
            write1.WriteLine(txtfield2.Text);
            write1.WriteLine(txtfield3.Text);

            write1.Close();
            }
      }
Or is there an easier way of calling the event for "btnSave"?
My reason for asking is that I have about 90 or so fields that I am working with, so I would like to keep my code clean.
I have tried something similar and it only saved when I minimized the application's window or when I exited, where I wanted it to save more often, but not sure how to activate the autosave function with the timer?

Should I rather let it autosave for, when example, a field is being left, by implementing it's leave event?

All help will be much appreciated.

Thanks

>BstrucT
__________________
The more the human race tries to change everything, when not needed, the less will they be able to change themselves when they need to.
BstrucT is offline   Reply With Quote