基本信息
源码名称:C# 文档编辑器
源码大小:0.18M
文件格式:.zip
开发语言:C#
更新时间:2015-11-17
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
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;
using System.IO;
namespace text
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected void SaveFile(string fileName)
{
try
{
Stream stream = File.OpenRead(fileName); //需引用 using system.io
using (StreamWriter writer = new StreamWriter(stream))
{
writer.Write(richTextBox1.Text);
}
}
catch (IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
//private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
//{
// if (this.richTextBox1.Text == "")
// return;
// saveFileDialog1.DefaultExt = "txt";
// saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
// if (this.saveFileDialog1.ShowDialog() == DialogResult.Cancel)
// return;
// string FileName = this.saveFileDialog1.FileName;
// if (saveFileDialog1.ShowDialog() == DialogResult.OK && FileName.Length > 0)
// {
// // Save the contents of the RichTextBox into the file.
// richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
// MessageBox.Show("文件已成功保存");
// }
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.richTextBox1.Text == "")
return;
saveFileDialog1.DefaultExt = "txt";
saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
//if (this.saveFileDialog1.ShowDialog() == DialogResult.Cancel)
// return;
string FileName = this.saveFileDialog1.FileName;
if (saveFileDialog1.ShowDialog() == DialogResult.OK && FileName.Length > 0)
{
// Save the contents of the RichTextBox into the file.
richTextBox1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
MessageBox.Show("文件已成功保存");
}
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != "")
{
MessageBox.Show("是否需要先保存?");
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text Document(*.txt)|*.txt|All Files|*.*|我¨°要°a显?示º?的Ì?文?件t类¤¨¤型¨ª(*.exe)|*.exe";
if (sfd.ShowDialog() == DialogResult.OK)
{
string fileName = sfd.FileName;
SaveFile(fileName); //该函数为自定义函数,代码在后
richTextBox1.Text = "";
}
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
//protected void OpenFile(string fileName)
//{
// try
// {
// Stream stream = File.OpenRead(fileName);
// using (StreamReader reader = new StreamReader(stream))
// {
// String s;
// while ((s = reader.ReadLine()) != null)
// richTextBox1.Text = s;
// }
// }
// catch (IOException ex)
// {
// MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// }
//}
protected void OpenFile(string fileName)
{
try
{
var gb18030 = Encoding.GetEncoding("GB18030");
StreamReader reader;
using (reader = new StreamReader(fileName, gb18030, true))
{
String s;
while ((s = reader.ReadLine()) != null)
{
richTextBox1.Text = s;
richTextBox1.Text = "\n";
}
}
}
catch (IOException ex)
{
MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text != "")
{
MessageBox.Show("是否需要先保存内容?");
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text Document(*.txt)|*.txt|All Files|*.*|我¨°要°a显?示º?的Ì?文?件t类¤¨¤型¨ª(*.exe)|*.exe";
if (sfd.ShowDialog() == DialogResult.OK)
{
string fileName = sfd.FileName;
SaveFile(fileName); //该函数为自定义函数,代码在后
richTextBox1.Text = "";
}
}
OpenFileDialog of = new OpenFileDialog();
of.Filter = "Text Document(*.txt)|*.txt|All Files|*.*|我¨°要°a显?示º?的Ì?文?件t类¤¨¤型¨ª(*.exe)|*.exe";
if (of.ShowDialog() == DialogResult.OK)
{
string fileName = of.FileName;
OpenFile(fileName);
StreamReader reader = new StreamReader(fileName, System.Text.Encoding.Default);
}
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "文本文件(*.txt)|*.txt|全部文件(*.*)|*.*";
sfd.InitialDirectory = "D:\\";
sfd.FilterIndex = 0;
if (sfd.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(sfd.FileName);
sw.Write(richTextBox1.Text);
sw.Close();
}
else
return;
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
this.richTextBox1.Font = new Font("宋体", 12, FontStyle.Bold);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (richTextBox1.Text == "")//写if(richTextBox1.Text==null)时直接执行else下面的语句了。
{
}
else
{
DialogResult result;
result = MessageBox.Show("是否保存?", "舞文 1.0.0", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "文本文件(*.txt)|*.txt|全部文件(*.*)|*.*";
sfd.InitialDirectory = "D:\\";
sfd.FilterIndex = 0;
if (sfd.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(sfd.FileName);
sw.Write(richTextBox1.Text);
sw.Close();
}
else
return;
}
else if (result == DialogResult.No)
{
//this.Close();//什么也不做最好。
}
else if (result == DialogResult.Cancel)
{
e.Cancel = true;//若要取消窗体的关闭操作,请将传递给事件处理程序的 FormClosingEventArgs 的 Cancel 属性设置为 true。
//*********以上程序写于2007-11-13-20:59*********
}
}
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength > 0)
// 复制文本到剪贴板
richTextBox1.Copy();
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 判断剪贴板中是否有文本
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
// 判断文本框中是否有文本选定了
if (richTextBox1.SelectionLength > 0)
{
// 询问是否覆盖选定的文本
if (MessageBox.Show("你想覆盖选定的文本吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
// 移动选定文本的位置,即之前选定文本的起始 选定文本的长度
richTextBox1.SelectionStart = richTextBox1.SelectionStart richTextBox1.SelectionLength;
}
// 将剪贴板中的文本粘贴至文本框
richTextBox1.Paste();
}
}
private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
{
//if (richTextBox1.CanUndo == true)
//{
// // 撤销最后的操作
// richTextBox1.Undo();
// // 从该文本框的撤消缓冲区中清除关于最近操作的信息。
// richTextBox1.Redo();
//}
richTextBox1.Undo();
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectedText != "")
// 剪切选定的文本至剪贴板
richTextBox1.Cut();
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectedText.Length > 0)
{
richTextBox1.SelectedText = "";
}
}
private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
}
}
}