using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace 文本文件夹
{
public partial class form1 : Form
{
string fileName = "Untitle";
public form1()
{
InitializeComponent();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "Text Documents(*.txt)|*.txt|All Files|*.*";
openFile.FilterIndex = 2;
if (openFile.ShowDialog() == DialogResult.OK)
{
fileName = openFile.FileName;
open();
}
}
protected void open()
{
try
{
textBox1.Clear();
textBox1.Text = File.ReadAllText(fileName );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误警告!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "(*.txt)|*.txt|(*.doc)|.doc";
saveFile.FilterIndex = 1;
if (saveFile.ShowDialog() == DialogResult.OK)
{
fileName = saveFile.FileName;
save();
}
}
protected void save()
{
try
{
File.WriteAllText(fileName, textBox1.Text );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误警告!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "(*.txt)|*.txt|(*.doc)|.doc";
saveFile.FilterIndex = 1;
if (saveFile.ShowDialog() == DialogResult.OK)
{
if (fileName == "Untitle")
{
saveAsToolStripMenuItem_Click(sender, e);
}
else
{
save();
}
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog font = new FontDialog();
if(font.ShowDialog ()==DialogResult .OK )
{
textBox1.Font = font.Font;
}
}
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog color = new ColorDialog();
if (color.ShowDialog() == DialogResult.OK)
{
textBox1.ForeColor = color.Color;
}
}
private void 说明ToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 Abox = new AboutBox1();
Abox.ShowDialog();
}
private void timeofEditToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text + DateTime.Now.ToString();
}
private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.SelectAll();
}
private void 复制ToolStripMenuItem1_Click(object sender, EventArgs e)
{
try
{
textBox1.SelectAll();
System.Windows.Forms.Clipboard.SetText(this.textBox1.SelectedText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "粘贴错误警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void 剪贴ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
textBox1.SelectAll();
System.Windows.Forms.Clipboard.SetText(this.textBox1.SelectedText);
textBox1.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "粘贴错误警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void tie_Click(object sender, EventArgs e)
{
try
{
System.Windows.Forms.IDataObject data = System.Windows.Forms.Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Text))
{
textBox1.SelectedText = data.GetData(DataFormats.Text).ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "粘贴错误警告", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
沒有留言:
張貼留言