c#记事本 源代码

using System;
using System.Drawing;
using System.Collections;
using https://www.360docs.net/doc/8216623354.html,ponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace NotePad
{


public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItem10;
private System.Windows.Forms.MenuItem menuItem14;
private System.Windows.Forms.MenuItem menuItem15;
private System.Windows.Forms.MenuItem menuItem16;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.FontDialog fontDialog1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;

private https://www.360docs.net/doc/8216623354.html,ponentModel.Container components = null;

public Form1()
{

InitializeComponent();


}


protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.menuItem14 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem15 = new System.Windows.Forms.MenuItem();
this.menuItem16 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.fontDialog1 = new System.Windows.Forms.FontDialog();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();

this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();


this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem3,
this.menuItem4});

this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem6,
this.menuItem7,
this.menuItem8,
this.menuItem9,
this.menuItem10,
this.menuItem14});
this.menuItem1.Text = "文件(&F)";

this.menuItem6.Index = 0;
this.menuItem6.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
this.menuItem6.Text = "新建(&N)";
this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);

this.menuItem7.Index = 1;
this.menuItem7.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
this.menuItem7.Text = "打开(&O)...";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);

this.menuItem8.Index = 2;
this.menuItem8.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
this.menuItem8.Text = "保存(&S)";
this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);

this.menuItem9.Index = 3;
this.menuItem9.Text = "另存为(&A)...";
this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click);

this.menuItem10.Index = 4;
this.menuItem10.Text = "-";

this.menuItem14.Index = 5;
this.menuItem14.Text = "退出(&X)";
this.menuItem14.Click += new System.EventHandler(this.menuItem14_Click);

this.menuItem3.Index = 1;
this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem15,
this.menuItem16});
this.menuItem3.Text = "格式(&O)";

this.menuItem15.Index = 0;
this.menuItem15.Text = "自动换行(&W)";
this.menuItem15.Click += new System.EventHandler(this.menuItem15_Click);

this.menuItem16.Index = 1;
this.menuItem16.Text = "字体(&F)...";
this.menuItem16.Click += new System.EventHandler(this.menuItem16_Click);

this.menuItem4.Index = 2;
this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem5});
this.menuItem4.Text = "帮助(&H)";

this.menuItem5.Index = 0;
this.menuItem5.Text

= "关于记事本(&A)";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);

this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
https://www.360docs.net/doc/8216623354.html, = "richTextBox1";
this.richTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
this.richTextBox1.Size = new System.Drawing.Size(480, 311);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
this.richTextBox1.WordWrap = false;
this.richTextBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.richTextBox1_KeyPress);

this.saveFileDialog1.FileName = "doc1";

this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
this.ClientSize = new System.Drawing.Size(480, 311);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.richTextBox1});
this.Menu = this.mainMenu1;
https://www.360docs.net/doc/8216623354.html, = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "记事本";
this.Closing += new https://www.360docs.net/doc/8216623354.html,ponentModel.CancelEventHandler(this.Form1_Closing);
this.ResumeLayout(false);

}
#endregion


[STAThread]
static void Main()
{
Application.Run(new Form1());
}



private bool dirty = false;
private string filepath = "";

private void MySaving()
{
if (dirty)
{
if (MessageBox.Show("需要保存文件吗?", "提示框", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
dirty = false;
if (filepath.Length == 0)
{
filepath = saveFileDialog1.FileName;
}
StreamWriter sTmp = new StreamWriter(filepath);
sTmp.Write(richTextBox1.Text);
sTmp.Flush();
sTmp.Close();
}
}
}
}
private void menuItem14_Click(object sender, System.EventArgs e)
{
MySaving();
if (dirty == false)
Close();
}
private void Form1_Closing(object sender, https://www.360docs.net/doc/8216623354.html,ponentModel.CancelEventArgs e)
{
MySaving();
if (dirty == false)
e.Cancel = false;
}
private void menuItem15_Click(object sender, System.EventArgs e)
{
if (richTextBox1.WordWrap == true)
{
richTextBox1.WordWrap = false;

mainMenu1.MenuItems[2].MenuItems[0].Checked = false;
}
else
{
richTextBox1.WordWrap = true;
mainMenu1.MenuItems[2].MenuItems[0].Checked = true;
}
}
private void menuItem16_Click(object sender, System.EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.Font = fontDialog1.Font;
}
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
MessageBox.Show("版权所有盗用必究,信管0801专用", "关于记事本");
}
private void richTextBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
dirty = true;
}
private void menuItem6_Click(object sender, System.EventArgs e)
{
MySaving();
dirty = false;
richTextBox1.Text = "";
}
private void menuItem7_Click(object sender, System.EventArgs e)
{
MySaving();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
dirty = false;
StreamReader sTmp = new StreamReader(openFileDialog1.FileName, System.Text.Encoding.ASCII);
filepath = openFileDialog1.FileName;
richTextBox1.Text = "";
richTextBox1.Text = sTmp.ReadToEnd();
}
}
private void menuItem8_Click(object sender, System.EventArgs e)
{
MySaving();
}
private void menuItem9_Click(object sender, System.EventArgs e)
{
MySaving();
}
}
}

相关文档
最新文档