winforms SaveFile和LoadFile问题

jv4diomz  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(189)

我试图创建一个文本框,你可以在其中保存文本,并将其加载为.txt文件。但是,它一直告诉我,文本框没有定义这2。下面是我试图编写的程序的代码:

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);
        }
    }
hmmo2u0o

hmmo2u0o1#

对于保存文件:

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

对于将文件加载到textBox:

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

相关问题