Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 17th, 2008, 6:12 AM   #1
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 132
Rep Power: 1 BstrucT is on a distinguished road
Send a message via MSN to BstrucT
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
__________________
>BstrucT
BstrucT is offline   Reply With Quote
Old Apr 17th, 2008, 8:16 AM   #2
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 925
Rep Power: 4 lectricpharaoh will become famous soon enough
Re: Autosaving Streams in C#

First off, you can add all those textboxes to an array or other collection, such that you can iterate through them. This is very handy if you're performing identical operations on multiple textboxes, such as validation, serialization, etc. Stick it in the form's handler for the Load event, or something:
C# Syntax (Toggle Plain Text)
  1. // textBoxArray is a class-scope object, so all methods can see it
  2. textBoxArray = new TextBox[NUMBER_OF_TEXTBOXES];
  3. textBoxArray[0] = txtfield1;
  4. textBoxArray[1] = txtfield2;
  5. textBoxArray[2] = txtfield3; // and so on
Then you can write a method that loops through all of these, and saves them:
C# Syntax (Toggle Plain Text)
  1. private bool saveData(string filename)
  2. {
  3. try
  4. {
  5. StreamWriter sw = new StreamWriter(filename);
  6. for(int x=0; x<textBoxArray.Length; ++x)
  7. sw.WriteLine(textBoxArray[x]);
  8. }
  9. catch
  10. {
  11. return false;
  12. }
  13. finally
  14. {
  15. if(sw != null)
  16. sw.Close();
  17. }
  18. return true;
  19. }
Now, you can call this method from your timer function, your handler for the save button click, etc. It will let you know if it succeeds or not with the return value (true for success, false for failure).
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Old Apr 17th, 2008, 5:32 PM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: Autosaving Streams in C#

You can set the timer interval to say 120 seconds, and it will save every two minutes by calling timer_tick.

Try this

c# Syntax (Toggle Plain Text)
  1. Class asdjkfj
  2. {
  3. string autoSavePrefix = "";
  4. ...
  5. private void btnSave1_Click(object sender, EventArgs e)
  6. {
  7. StreamWriter write1 = new StreamWriter
  8. ( "C:\\" + autoSavePrefix + txtSaveName.Text );
  9.  
  10. write1.WriteLine(txtfield1.Text);
  11. write1.WriteLine(txtfield2.Text);
  12. write1.WriteLine(txtfield3.Text);
  13.  
  14. write1.Close();
  15. }
  16. ....
  17. private void timer_Tick(object sender, EventArgs e)
  18. {
  19. DateTime autosave = DateTime.Today;
  20. if (txtSaveName.Visible == true)
  21. {
  22. autoSavePrefix = "Auto Saved";
  23. btnSave1_Click(null, null);
  24. autoSavePrefix = "";
  25. }
  26. }
OpenLoop is offline   Reply With Quote
Old Apr 18th, 2008, 12:39 AM   #4
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 132
Rep Power: 1 BstrucT is on a distinguished road
Send a message via MSN to BstrucT
Re: Autosaving Streams in C#

Wow, thanks for the great advice guys.

@lectricpharaoh:
Thanks for showing me how to implement it by putting all those frisky fields into an array then just work with the array as a whole. This should make my life a lot easier regarding code repitition!

@Openloop:
Will definitely take a closer look at your timer function and see how I can implement it.

Thanks for the great advice guys, gonna get cracking on the code right away!

>BstrucT
__________________
>BstrucT
BstrucT is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# Streams and Installer Question BstrucT C# 2 Mar 27th, 2008 8:40 AM
Binary Serialization vs. Byte Streams kurifu C# 1 Apr 7th, 2007 5:17 PM
Audio Streams Writlaus Other Web Development Languages 2 Dec 11th, 2006 12:08 AM
Java's I/O Files and Streams ReggaetonKing Java 6 May 6th, 2006 10:14 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC