Home > Enterprise >  SaveFile and LoadFile issue
SaveFile and LoadFile issue

Time:10-30

I'm trying to create a text box where you can save the text and load it as a .txt file. However, it keeps telling me that the textbox does not have a definition for those 2. Here's the code of the program I'm trying to write:

 private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                textBox1.SaveFile(saveFileDialog1.FileName);
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                textBox1.LoadFile(openFileDialog1.FileName);
        }
    }

CodePudding user response:

for save file:

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.ShowDialog();
File.WriteAllText(saveFileDialog1.FileName, textBox1.Text);

for load file to textBox:

OpenFileDialog openFileDialog1 = new OpenFileDialog ();
openFileDialog1.ShowDialog();
textBox1.Text = File.ReadAllText(openFileDialog1.FileName);
  • Related