using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
string filename;
public Form1()
{
InitializeComponent();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
about a = new about();
a.Show();
}
private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
{
状态栏ToolStripMenuItem.Checked = !状态栏ToolStripMenuItem.Checked;
statusStrip1.Visible = 状态栏ToolStripMenuItem.Checked;
}
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog f = new FontDialog();
if (f.ShowDialog() == DialogResult.Cancel) return;
textBox1.Font = f.Font;
}
private void worarapToolStripMenuItem_Click(object sender, EventArgs e)
{
worarapToolStripMenuItem.Checked = !worarapToolStripMenuItem.Checked;
statusStrip1.Visible =! worarapToolStripMenuItem.Checked;
if (worarapToolStripMenuItem.Checked)
{
状态栏ToolStripMenuItem.Enabled = false;
状态栏ToolStripMenuItem.Checked = false;
textBox1.WordWrap = true;
}
else
{
状态栏ToolStripMenuItem.Enabled = true;
textBox1.WordWrap = false;
}
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Copy();
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Paste();
}
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
this.textBox1.SelectAll();
}
private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Cut();
}
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
find fd = new find();
fd.Init(Search);
fd.Show(this);
}
private int pos;
private void Search(string s)
{
int i = textBox1.Text.IndexOf(s,pos);
pos = i + 1;
if (pos <s.Length)
{
MessageBox.Show("已经查找完毕");
}
if (i >=0)
{
textBox1.SelectionLength = s.Length;
textBox1.SelectionStart = i;
textBox1.Focus();
}
}
private void Form1_Load(object sender, EventArgs e)
{
pos = 0;
}
private void saveToSToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog s = new SaveFileDialog();
s.Filter = "*.txt|*.txt";
filename = s.FileName;
if (s.ShowDialog() == DialogResult.Cancel) return;
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filename,false,Encoding .GetEncoding ("gb2132")))
{
sw.Write(textBox1.Text);
sw.Close();
}
}
private void saveSToolStripMenuItem_Click(object sender, EventArgs e)
{
if (filename == null || filename == "")
{
SaveFileDialog s = new SaveFileDialog();
s.Filter = "*.txt|*.txt";
if (s.ShowDialog() == DialogResult.Cancel) return;
filename = s.FileName;
this.Text = filename + "--记事本";
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filename, false, Encoding.GetEncoding("gb2312")))
{
sw.Write(textBox1.Text);
sw.Close();
}
textBox1.Modified = false;
}
}
private void openOToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "" && textBox1.Modified)
{
DialogResult dr = MessageBox.Show("文本内容已经改变,是否保存?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
switch (dr)
{
case DialogResult.Yes:
saveSToolStripMenuItem_Click(sender, e);
break;
case DialogResult.No:
textBox1.Clear();
break;
case DialogResult.Cancel:
return;
}
}
OpenFileDialog o = new OpenFileDialog();
o.Filter = "*.txt|*.txt";
if (o.ShowDialog() == DialogResult.Cancel) return;
filename = o.FileName;
this.Text = filename + "--记事本";
using (System.IO.StreamReader sr = new System.IO.StreamReader(filename, Encoding.GetEncoding("gb2312")))
{
textBox1.Text = sr.ReadToEnd();
sr.Close();
}
}
private void createCToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.textBox1.Modified && textBox1.Text != "")
{
DialogResult dr = MessageBox.Show("文本内容已经改变,是否保存?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
switch (dr)
{
case DialogResult.Yes:
saveSToolStripMenuItem_Click(sender, e);
this.textBox1.Clear();
this.Text = "新建--记事本";
break;
case DialogResult.No:
textBox1.Clear();
this.Text = "新建--记事本";
break;
case DialogResult.Cancel:
return;
}
}
else
{
textBox1.Clear();
this.Text = "新建--记事本";
}
}
private void exitEToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.textBox1.Modified && textBox1.Text != "")
{
DialogResult dr = MessageBox.Show("文本内容已经改变,是否保存?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
switch (dr)
{
case DialogResult.Yes:
saveSToolStripMenuItem_Click(sender, e);
this.Close();
break;
case DialogResult.No:
this.Close();
break;
case DialogResult.Cancel:
return;
}
}
else
this.Close();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (textBox1.Modified)
{
DialogResult dr = MessageBox.Show("文本内容已经改变,是否保存?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
switch (dr)
{
case DialogResult.Yes:
saveSToolStripMenuItem_Click(sender, e);
break;
case DialogResult.No:
break;
case DialogResult.Cancel:
return;
}
}
}
private void replaceToolStripMenuItem_Click(object sender, EventArgs e)
{
DateTime dt = DateTime.Now;
this.textBox1.SelectedText = dt.ToString();
}
}
}
在find.cs中的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public delegate void SrhText(string s);
public partial class find : Form
{
SrhText st;
public find()
{
InitializeComponent();
}
public void Init(SrhText s)
{
st = s;
}
private void button1_Click(object sender, EventArgs e)
{
if (st != null)
st(textBox1.Text);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
this.Dispose();
}
}
}