0

I am new to C#. Basically I want to implement an auto save function using a timer. May I know how do I have to implement it, so that the text will get saved automatically every 5 seconds?

SaveFileDialog saveFile1 = new SaveFileDialog();

saveFile1.DefaultExt = "*.rtf";
saveFile1.Filter = "RTF Files|*.rtf";

if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
           saveFile1.FileName.Length > 0)
{

   txtb.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText);

I can save the file but how do I auto save it?

croxy
  • 4,082
  • 9
  • 28
  • 46
Ema_wat
  • 33
  • 2
  • 11
  • Write a method that saves the file without user interaction, i.e. without a `SaveFileDialog`, then call that method from your manual save routine, i.e. the `Click` event handler of the appropriate `Button`, and from your auto save routine, i.e. the `Tick` event handler of your `Timer`. There's no magic to it. – jmcilhinney Mar 26 '18 at 03:32
  • 1
    You could've simply searched [How to use Timers in C#](https://stackoverflow.com/a/12535731/6138713) and [How to write a text file](https://stackoverflow.com/questions/1919552/how-to-write-a-text-file-in-c-sharp). – jegtugado Mar 26 '18 at 03:32

1 Answers1

0

Just use a timer with an interval of 5000ms

Timer tmr = New Timer;
tmr.Interval = 5000;

Now call your timer in the Form_Load event or whereever you want to call it the first time. After that, just use the Tick event of the timer (Note: The first time you call the SAVEFILEDIALOG, make sure that you store the location in some variable so that you can keep on reusing it to save the text file which will eliminate the need of using the SAVEFILEDIALOG again and again):

String pathOfFile;

private sub FirstTimeSaveIt_Click // the button that saves it first
if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
saveFile1.FileName.Length > 0)
{
  pathOfFile = saveFile1.FileName
  txtb.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText);
  }
private void tmr_Tick()
{
tmr.Stop();
txtb.SaveFile(pathOfFile, RichTextBoxStreamType.PlainText);

tmr.start();
}
Software Dev
  • 5,368
  • 5
  • 22
  • 45
  • tmr.Tick += new EventHandler(File_Save_MenuItem_Click); i used this tick event method but its throwing an error.can u pls explain me how to use Tick event and where do i have to call this function @Sebastian Hofmann – Ema_wat Mar 26 '18 at 07:00
  • Just declare the event in ur form load – Software Dev Mar 26 '18 at 07:05
  • private void File_Save_MenuItem_Click(object sender, EventArgs e) { Timer tmr = new Timer(); tmr.Interval = 5000; tmr.Start(); //logic to save tmr.Stop(); tmr.Tick += File_Save_MenuItem_Click; } is the way i have done is correct – Ema_wat Mar 26 '18 at 07:10
  • Just add a timer timer to ur form from toolbox – Software Dev Mar 26 '18 at 07:22
  • i added the method within the timer but .am getting an error, The type or namespace name 'Timers' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?) .i tried to add the assembly system.windowsform dll but its not available .may i know how to fix it – Ema_wat Mar 26 '18 at 07:37
  • its TIMER not TIMERS there's no S – Software Dev Mar 26 '18 at 07:40
  • private void timer1_Tick(object sender, EventArgs e) { } i added the code which i mentioned above.theres no compile timer errors.but the file is not getting auto saved.should i invoke the method somewhere.can u please tell me what i have to do. – Ema_wat Mar 26 '18 at 07:54
  • System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer(); i used this instead of declaring namespace . – Ema_wat Mar 26 '18 at 07:55
  • i dont have error .but the file is not auto saving.i saved the file first and typed again.whatever i typed didnt get save automatically after 5 sec – Ema_wat Mar 26 '18 at 08:13
  • can u pls share me the code where i have to call the timer. am still getting errors. – Ema_wat Mar 26 '18 at 08:28
  • without assigning the path.isnt it possible to auto save it to the file where i have already saved it.how do i do that.where do i have to call the method.please explain clearly – Ema_wat Mar 26 '18 at 08:59
  • Just follow my code,that's all ...It shows u all u need – Software Dev Mar 26 '18 at 09:07
  • May I know why we are creating button( use of button)? – Ema_wat Mar 26 '18 at 09:50