private void SaveMyFile_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile1 = new SaveFileDialog();
saveFile1.DefaultExt = "*.rtf";
saveFile1.Filter = "RTF Files|*.rtf";
if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
saveFile1.FileName.Length > 0)
{
richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText);
}
}
任何帮助pleaseeee
答案 0 :(得分:0)
你是说你不应该提出对话?如果是这样,只需保存文件名并使用以下内容保存:
richTextBox1.SaveFile(_filename, RichTextBoxStreamType.PlainText);
答案 1 :(得分:0)
只需在打开文件时保存文件路径..然后丢失所有SaveFileDialog内容,只需调用richTextBox1.SaveFile(the_path_you_saved_when_you_opened_the_file);
答案 2 :(得分:0)
只需将原始路径保存到读入文本框的文件即可。当用户单击要保存的按钮时,请使用以下代码:
richTextBox1.SaveFile(filename, RichTextBoxStreamType.PlainText);
其中filename
是变量,您存储了打开文件的原始路径。
答案 3 :(得分:0)
这是你想要的代码:
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 = "*.rtf";
saveFile1.Filter = "RTF Files|*.rtf";
if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
saveFile1.FileName.Length > 0)
{
fileLocation = saveFile1.FileName;
return true;
}
return false;
}
答案 4 :(得分:0)
您应该尝试添加从中加载富文本的文件名或FileInfo。如果文件尚未保存,请提示保存文件。否则,保存到缓存的文件信息。
partial class YourForm : Form
{
string filePath;
private void SaveMyFile_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(filePath))
{
SaveFileDialog saveFile1 = new SaveFileDialog();
saveFile1.DefaultExt = "*.rtf";
saveFile1.Filter = "RTF Files|*.rtf";
if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
saveFile1.FileName.Length > 0)
{
filePath = saveFile1.FileName;
}
else return;
}
try
{
richTextBox1.SaveFile(filePath, RichTextBoxStreamType.PlainText);
}
catch (Exception ee)
{
// Put exception handling code here
}
}
}
根据下面的评论,如果您想要另存为按钮,可以尝试以下操作:
partial class YourForm : Form
{
Button saveFileAsButton; // Add this using the Forms Designer
private void saveFileAsButton_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile1 = new SaveFileDialog();
saveFile1.DefaultExt = "*.rtf";
saveFile1.Filter = "RTF Files|*.rtf";
if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
saveFile1.FileName.Length > 0)
{
try
{
richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText);
filePath = saveFile1.FileName;
}
catch (Exception ee)
{
// Put exception handling code here (e.g. error saying file cannot be saved)
}
}
}
}
请注意filePath
的设置如何在try块中。如果保存失败,您不希望丢失原始文件路径。
如果您的表单有MenuStrip
,我建议您将保存功能移到菜单中。
(顺便说一下,过滤器中RTF的类型名称会更好“Rich Text Document
”。)