[英]clear richtextbox and overwrite previous entry
I have a richtextbox that will overwrite a file that has already been saved before. 我有一个Richtextbox,它将覆盖之前已保存的文件。
If I need to start a new file and then save it as a new file, it just overwrites the first file that was saved. 如果我需要启动一个新文件,然后将其另存为新文件,它将覆盖已保存的第一个文件。
How can this be done? 如何才能做到这一点?
String fileLocation;
private void SaveMyFile_Click(object sender, EventArgs e)
{
var performSave = true;
if (String.IsNullOrEmpty(fileLocation))
{
performSave = SetFileLocation();
}
if (performSave)
richTextBox1.SaveFile(fileLocation, RichTextBoxStreamType.PlainText);
}
private bool SetFileLocation()
{
SaveFileDialog saveFile1 = new SaveFileDialog();
saveFile1.DefaultExt = "*.txt";
saveFile1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*|RTF Files|*.rtf";
if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
saveFile1.FileName.Length > 0)
{
fileLocation = saveFile1.FileName;
return true;
}
return false;
}
第一次保存后的FileLocation变量(SetLocation)不为null或为空,这就是为什么当您再次保存(单击SaveMyFile按钮)时,它不会输入SetFileLocation方法,而只是执行SaveFile
When you "start a new file", just set fileLocation
to null and you should get the SaveFileDialog
to show up again to allow you to enter a new file location. 当“启动新文件”时,只需将
fileLocation
设置为null,您应该使SaveFileDialog
再次出现,以允许您输入新文件位置。 The code below is the crucial bit: 下面的代码至关重要:
if (String.IsNullOrEmpty(fileLocation))
{
performSave = SetFileLocation();
}
The SetFileLocation()
method won't be called unless fileLocation
is null or the empty string. 除非
fileLocation
为null或空字符串,否则不会调用SetFileLocation()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.