Sha*_*fiz 4 c# wpf notepad richtextbox savefiledialog
我需要实现类似于Notepads的保存选项.假设我有一个按钮放在a旁边RichTextBox,我想要的是,当点击这个按钮时,会打开一个对话框,它看起来类似于单击另存为时出现的对话框.我想通过在" 保存对话框"中输入文件名来以文本格式保存RichTextBox的内容.
private void Save_As_Click(object sender, EventArgs e)
{
SaveFileDialog _SD = new SaveFileDialog();
_SD.Filter = "Text File (*.txt)|*.txt|Show All Files (*.*)|*.*";
_SD.FileName = "Untitled";
_SD.Title = "Save As";
if (__SD.ShowDialog() == DialogResult.OK)
{
RTBox1.SaveFile(__SD.FileName, RichTextBoxStreamType.UnicodePlainText);
}
}
Run Code Online (Sandbox Code Playgroud)
对于WPF,您应该使用此SaveFileDialog.
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.Filter = "Rich Text File (*.rtf)|*.rtf|All Files (*.*)|*.*";
dialog.FileName = "Filename.rtf"; //set initial filename
if (dialog.ShowDialog() == true)
{
using (var stream = dialog.OpenFile())
{
var range = new TextRange(myRichTextBox.Document.ContentStart,
myRichTextBox.Document.ContentEnd);
range.Save(stream, DataFormats.Rtf);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18711 次 |
| 最近记录: |