我制作了一个小型文本编辑器,可以将文本从 a 保存RichTextBox
到.rtf
文件中,但是每当我单击打开菜单选项时,它都会打开保存文件对话框。我不明白为什么有人可以帮助我?
以下是菜单对话框项的名称。
MenuStrip: menuStrip1
保存: saveMenu
打开: openMenu
RichTextBox: richTextBox1
这是我在下面处理的代码...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//save the file
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
// Text from the rich textbox rtfMain
string str = richTextBox1.Text;
// Create a new SaveFileDialog object
using (SaveFileDialog dlgSave = new SaveFileDialog())
try
{
// Available file extensions
dlgSave.Filter = "Rich Text Format (.rtf)|*.rtf";
// SaveFileDialog title
dlgSave.Title = "Save";
// Show SaveFileDialog
if (dlgSave.ShowDialog() == DialogResult.OK && dlgSave.FileName.Length > 0)
{
richTextBox1.SaveFile(dlgSave.FileName);
}
}
catch (Exception errorMsg)
{
MessageBox.Show(errorMsg.Message);
}
}
private void openMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
using (var of = new OpenFileDialog())
{
of.DefaultExt = "*.rtf";
of.Filter = "RTF Files|*.rtf";
if (of.ShowDialog() == DialogResult.OK)
richTextBox1.Rtf = System.IO.File.ReadAllText(of.FileName);
}
}
}
}