C#的文本编辑器我已经写好了咯!
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using
namespace WindowsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int flag = 0,strlen=0;
string filepath = "",str="";
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult r;
StreamReader sr;
if (richTextBox1.Text != string.Empty)
保存SToolStripMenuItem_Click(sender, e);
else
richTextBox1.Clear();
r=openFileDialog1.ShowDialog();
if (r != DialogResult.Cancel)
{
sr = new StreamReader(openFileDialog1.FileName,Encoding.GetEncoding("GB2312"));
filepath = openFileDialog1.FileName;
while(sr.Peek() != -1)
{
richTextBox1.Text += sr.ReadLine()+Environment.NewLine;
}
sr.Close();
flag = 1;
}
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
if (richTextBox1.Text == "")
{
MessageBox.Show("There is nothing to save!", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Error);
richTextBox1.Focus();
}
else if (flag == 0)
{
DialogResult r;
StreamWriter sw;
r = saveFileDialog1.ShowDialog();
if (r != DialogResult.Cancel)
{
sw = new StreamWriter(saveFileDialog1.FileName);
string[] sp ={ "\n" };
string[] s = richTextBox1.Text.Split(sp, StringSplitOptions.RemoveEmptyEntries);
for(int i=0;i<s.Length;i++)
sw.WriteLine(s[i]);
sw.Close();
}
richTextBox1.Clear();
flag = 0;
}
else
{
StreamWriter sw = new StreamWriter(filepath);
string[] sp ={ "\n" };
string[] s = richTextBox1.Text.Split(sp, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < s.Length; i++)
sw.WriteLine(s[i]);
sw.Close();
richTextBox1.Clear();
flag = 0;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (flag==1||richTextBox1.Text != "")
{
DialogResult r;
r = MessageBox.Show("Do you want to save file?", "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (r == DialogResult.Yes)
保存SToolStripMenuItem_Click(sender, e);
else
{
richTextBox1.Clear();
}
}
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
}
private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
{
str = richTextBox1.SelectedText;
richTextBox1.Cut(); ;
}
private void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
{
str = richTextBox1.SelectedText;
richTextBox1.Copy();
}
private void 删除DToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText = "";
}
private void 颜色CToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
richTextBox1.SelectionColor = colorDialog1.Color;
}
private void 字体FToolStripMenuItem_Click(object sender, EventArgs e)
{
fontDialog1.ShowDialog();
richTextBox1.SelectionFont =fontDialog1.Font;
}
private void 字数LToolStripMenuItem_Click(object sender, EventArgs e)
{
string s = richTextBox1.SelectedText;
int j = 0;
for (int k = 0; k < s.Length; k++)
{
if (s[k] == '\n' || s[k] == ' ')
j++;
}
int i=richTextBox1.SelectionLength;
strlen = i - j;
MessageBox.Show("Total:" + strlen.ToString(), "SelectionLength", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
private void 关于NToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 ab = new AboutBox1();
ab.Show();
}
private void 撤消ZToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Undo();
}
private void 重复RToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Redo();
}
private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
{
richTextBox1.Paste();
}
private void Form1_Resize(object sender, EventArgs e)
{
richTextBox1.Width =ActiveForm.Width;
richTextBox1.Height =ActiveForm.Height;
}
private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
Point p = MousePosition;
if(e.Button==MouseButtons.Right)
contextMenuStrip1.Show(p.X, p.Y);
}
}
}
[
本帖最后由 qq1023569223 于 2011-3-22 11:21 编辑 ]