Tilo Jung

Tilo Jung

  • 1.7k
  • 21
  • 4.4k

editor saving file in c#

Aug 25 2021 3:59 PM

Hello, how can I save an editor file in windows forms? I allways get the message: can't find the name "tb" in the context (line 52).

tb is the

TextBox tb = new Textbox().

I think the eventmethod "onSave" can't see this declaration.

private void OnSave(object sender, System.EventArgs e)
    {
        string fileName = @"files\ntext28.txt";
        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
        string textToAdd = tb.text;//was ist los alter?
        FileStream fs = null;
        fs = new FileStream(fileName, FileMode.CreateNew);
        //dispose fuer das Objekt streamwriter aufrufen
        using (StreamWriter writer = new StreamWriter(fs))
        {
            writer.Write(textToAdd);
        }
        Console.WriteLine("done");
    }