![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
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();
}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();
}
}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 |
|
|
|
|
|
#2 |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 922
Rep Power: 4
![]() |
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)
C# Syntax (Toggle Plain Text)
__________________
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 |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
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)
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |