using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using
namespace Notepad
{
public partial class Form1 : Form
{ /// <summary>
/// 必需的设计器变量。
/// </summary>
private compon = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (compon != null))
{
compon.Dispose();
}
base.Dispose(disposing);
}
// Windows 窗体设计器生成的代码
public System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem 文件ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 打开ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 保存ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 另存为ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 退出XToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 编辑ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 格式ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 查看ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 帮助HToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 新建ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 撤销ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 剪切TToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 复制CToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 粘贴VToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 删除DToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 全选ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 日期时间F5ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 帮助主题ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 关于记事本ToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
private System.Windows.Forms.ToolStripMenuItem 自动换行OToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 字体ToolStripMenuItem;
internal System.Windows.Forms.StatusStrip StatusStrip1;
internal System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
internal System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel2;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.ToolStripMenuItem 状态栏SToolStripMenuItem;
private System.Windows.Forms.ColorDialog colorDialog1;
private System.Windows.Forms.FontDialog fontDialog1;
private System.Windows.Forms.ToolStripMenuItem 字体颜色ToolStripMenuItem;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool b = false;
public string str_old;
public string file_old;
public string file;
private void textBox1_TextChanged(object sender, EventArgs e)
{
b = true;
}
private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
{
new Form1().Show();
}
private void 打开SToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.Filter = "文本文档(*.txt)|*.txt";
if (textBox1.Text == str_old)
{
if (of.ShowDialog() == DialogResult.OK)
{
file = of.FileName;
StreamReader sr = new StreamReader(file, Encoding.GetEncoding("GB2312"));
textBox1.Text = sr.ReadToEnd().ToString();
sr.Dispose();
}
}
else
{
DialogResult = MessageBox.Show(this.Owner, file_old, "记事本", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); switch (DialogResult)
{
case DialogResult.Yes:
SaveFileDialog sf = new SaveFileDialog();
sf.Filter = "文本文档(*.txt)|*.txt";
if (sf.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = File.AppendText(sf.FileName);
sw.Write(textBox1.Text);
sw.Dispose();
}
break;
case DialogResult.No:
if (of.ShowDialog() == DialogResult.OK)
{
file = of.FileName;
StreamReader sr = new StreamReader(file, Encoding.GetEncoding("GB2312"));
textBox1.Text = str_old + sr.ReadToEnd().ToString();
}
break;
case DialogResult.Cancel:
return;
default:
return;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
str_old = textBox1.Text;
file_old = "文件" + Application.ExecutablePath.Remove(Application.ExecutablePath.Length - 4, 4) + ".txt " + "的文字已经改变。\n\n 是否保存?";
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
if (b)
{
if (MessageBox.Show("是否保存文档?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
保存SToolStripMenuItem_Click(sender, e);
this.Close();
}
else
{
this.Close();//退出程序
}
}
else
{
this.Close();//退出程序
}
}
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
StreamWriter sw = File.AppendText(Application.ExecutablePath);
sw.Write(textBox1.Text);
sw.Dispose();
}
catch
{
SaveFileDialog sf = new SaveFileDialog();
sf.DefaultExt = "*.txt";
sf.Filter = "文本文档(.txt)|*.txt";
if (sf.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = File.AppendText(sf.FileName);
sw.Write(textBox1.Text);
sw.Dispose();
}
}
}
private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sf = new SaveFileDialog();
sf.DefaultExt = "*.txt";
sf.Filter = "文本文档(.txt)|*.txt";
if (sf.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = File.AppendText(sf.FileName);
sw.Write(textBox1.Text);
sw.Dispose();
}
new System.Windows.Forms.ToolStripSeparator();
}
private void 撤销ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Undo();
}
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetText(textBox1.SelectedText);
}
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - textBox1.SelectedText.Length, textBox1.SelectedText.Length);
textBox1.Select(textBox1.Text.Length, 0);
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetDataObject(textBox1.SelectedText);
textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart, textBox1.SelectionLength);
textBox1.Select(textBox1.Text.Length, 0);
}
private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text += Clipboard.GetText();
textBox1.Select(textBox1.Text.Length, 0);
}
private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.SelectAll();
}
private void 日期时间ToolStripMenuItem_Click(object sender, EventArgs e)
{
int count = textBox1.Text.IndexOf(textBox1.SelectedText, textBox1.SelectionStart);
textBox1.Text = textBox1.Text.Remove(textBox1.Text.IndexOf(textBox1.SelectedText, 0), textBox1.SelectedText.Length);
textBox1.Select(count, 0);
textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, DateTime.Now.ToString());
textBox1.Select(textBox1.Text.IndexOf(DateTime.Now.ToString(), 0) + DateTime.Now.ToString().Length, 0);
}
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
自动换行ToolStripMenuItem.Checked = !自动换行ToolStripMenuItem.Checked;
if (自动换行ToolStripMenuItem.Checked)
{
textBox1.WordWrap = false;
}
else
{
textBox1.WordWrap = true;
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
textBox1.Font = fd.Font;
}
}
private void 字体颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.Reset();
colorDialog1.AllowFullOpen = true;
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.ForeColor = colorDialog1.Color;
}
}
private void 状态栏ToolStripMenuItem_Click(object sender, EventArgs e)
{
状态栏ToolStripMenuItem.Checked = !状态栏ToolStripMenuItem.Checked;
if (状态栏ToolStripMenuItem.Checked)
{
StatusStrip1.Visible = true;
}
else
{
statusStrip1.Visible = false;
}
}
private void 帮助主题ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("找不到帮助主题!", "提示");
}
private void 关于记事本ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("联系记事本制作者 :1194755213@ ", "关于记事本");
}
private void timer1_Tick(object sender, EventArgs e)
{
MessageBox.Show("联系记事本制作者 :1194755213@ ", "关于记事本");
}
}
}
警告
1
类型 Form1 由同一文件中的几个分部类构成。
0
0